Changing the States displayed on the frontend of a entity

I have seen this request a few times in the forums. Putting a post together so I can link to it rather than find and paste the text each time.

This example uses a device_tracker entity which normally displays home or not_home for the state. This makes sense for a person, but for a computer or device online or offline may make more sense. Here is how to make that change. Its a bit of a round about way of doing it and not the most efficient option, but it works and is the only option I am aware of right now.


To do this we will use a template sensor:

To change my device_tracker.000c0e0f00ed from home/not_home to online/offline the code looks like like the following:

sensor:
  - platform: template
    sensors:
      desktop:
        value_template: '{% if is_state("device_tracker.000c0e0f00ed", "home") %}Online{% else %}Offline{% endif %}'
        friendly_name: 'Desktop' 

Note: This code will only work with a entity that has two states. In the example above if device_tracker.000c0e0f00ed had a state of ā€œWorkā€ the state of our new sensor would be ā€œofflineā€

Also, keep the sensor name (desktop in this example) lower case. I have seen issues when capital options are used.


Here is the same setup, using generic examples:

sensor:
  - platform: template
    sensors:
      sensor_name:
        value_template: '{% if is_state("entity_ID", "State 1") %}New State 1{% else %}New State 2{% endif %}'
        friendly_name: 'Name'

Now in my example I have a new entity after a restart of ā€˜sensor.desktopā€™. I would add this to my front end and remove ā€˜device_tracker.000c0e0f00edā€™. The result is as shown below.

8 Likes

Great minds think alike, eh? This was part of my tutorial on the SNMP project.

Hereā€™s the thing I donā€™t understand though; in cases where the sensor class is already defined, for example a binary_sensor for doors/windows, why is this necessary? If the sensor class is already defined as ā€˜openingā€™, shouldnā€™t the values be ā€˜openā€™ and ā€˜closedā€™ instead of ā€˜onā€™ and ā€˜offā€™.

Or maybe Iā€™m missing the point of sensor classes?

That may be where I originally saw this code, so credit goes to you on this, I certainly didnā€™t come up with this, just wanted to post it somewhere for easy reference.

Not sure, havenā€™t looked into sensor classes yet.

Bah! I probably got it from someone else too. And itā€™s a good idea to document it. I was commenting more on the use case; we both did it to monitor our local devices the same way!

Hereā€™s the page on sensor classes; Iā€™d be interested in your take after you read it (very quick read) and to see if Iā€™m missing the point.

I certainly donā€™t see a use other than what you specified. However, you think each example would have the two options for the state specified afterwards. I think I used this in my motion sensor and specified motion but it still is on/off.

Iā€™m going to do a little more research and see what I can come up with, but it seems to me that if HA recognizes a binary_sensor is an ā€œopeningā€ class sensor, it should automatically display the on/off values as open/close. Thanks for looking at it; I just wanted to make sure I wasnā€™t reading something into it that wasnā€™t there.

Sorry to ā€œhijackā€ this conversation but Iā€™m trying to figure out how to how to do this but with a ā€œfakeā€ device_tracker or a template sensor as youā€™ve mentioned aboveā€¦ what Iā€™d like to see in the front-end though is an entity picture with ā€œhomeā€ or ā€œawayā€ status in the orange bar:

With a template sensor method as mentioned above works very well but when itā€™s not in a group and displayed at the top of HomeAssistant, you do not get the status in the ā€œorangeā€ barā€¦ which is what Iā€™m trying to achieve.

The reason I mention a ā€œfakeā€ device_tracker is because Iā€™m using multiple methods per person to track their whereabouts:

When any of these change state it updates an Input_boolean (e.g. input_boolean.mike_home) which in turn triggers my Home and Away scripts (light on/off, switches on/off, nest home/away, alarm system on/off etc)

- alias: 'Mike Home'
  condition:
    condition: state
    entity_id: input_boolean.mike_home
    state: 'off'
  trigger:
    - platform: state
      entity_id: device_tracker.pi_mikesiphone
      state: 'home' 
    - platform: state
      entity_id: sensor.rfid
      state: '__REDACTED_ (RFID TAG NUMBER)'_ 
    - platform: state
      entity_id: device_tracker.mikes_iphone
      state: 'home' 
    - platform: state
      entity_id: sensor.mikes_keys
      state: 'happy-bubbles-ble' 
  action:
    - service: homeassistant.turn_on
      entity_id: input_boolean.mike_home
    
- alias: 'Mike Away'
  condition:
    condition: state
    entity_id: input_boolean.mike_home
    state: 'on'
  trigger:
    - platform: state
      entity_id: device_tracker.pi_mikesiphone
      state: 'not_home'  
    - platform: state
      entity_id: device_tracker.mikes_iphone
      state: 'not_home'
    - platform: state
      entity_id: sensor.mikes_keys
      state: 'away'
  action:
    - service: homeassistant.turn_off
      entity_id: input_boolean.mike_home

To summarise, Iā€™m trying to get this:

To change from ā€œhomeā€ and ā€œawayā€ based on the input booleans aboveā€¦ purely for aesthetics, and it could even replace the input booleans!

Hoping someone can help!

Thanks :slight_smile:

Did you get this to work @badgerhome ?

Stupid question from me ā€¦ I was able to implement what silvrr described, but it seems as if you have to configure ā€œtrack: trueā€ in known_devices.yaml, right? Iā€™ve set it to ā€œtrack: falseā€ and the status was not set to online/offline using the template sensor from above.
The problem setting tracking to true is that it ā€œbreaksā€ the real home / not_home story, doesnā€™t it? Lets assume that I want to see if my server or printer is online ā€¦ they might be running while I (IOW my mobile phone) am not at home so that the presence state will never switch to ā€œnot_homeā€ while my server/printer is still online?
Or am I missing something?

Although my last question was not answered yet this might be the perfect solution: https://home-assistant.io/components/binary_sensor.ping/ Have not tried it yet because it was just released and I have not yet upgraded HA.

@badgerhome This is exactly what I am trying to achieve! Did you ever find a solution? Does anyone have any ideas?

Thanks,

Tom

1 Like

@eBoon unfortunately I did not find a solution. Iā€™m still using the multiple input Booleans

Have looked at the Custom UI: Badges in state cards?

2 Likes

Thank you this is so helpful for me

@silvrr

When i want muliple sensors, should it be:

sensor:
  - platform: template
    sensors:
      desktop:
        value_template: '{% if is_state("device_tracker.000c0e0f00ed", "home") %}Online{% else %}Offline{% endif %}'
        friendly_name: 'Desktop' 
    sensors:
      laptop:
        value_template: '{% if is_state("device_tracker.000c0e0f00ed", "home") %}Online{% else %}Offline{% endif %}'
        friendly_name: 'laptop'

or

sensor:
  - platform: template
    sensors:
      desktop:
        value_template: '{% if is_state("device_tracker.000c0e0f00ed", "home") %}Online{% else %}Offline{% endif %}'
        friendly_name: 'Desktop' 
      laptop:
        value_template: '{% if is_state("device_tracker.000c0e0f00ed", "home") %}Online{% else %}Offline{% endif %}'
        friendly_name: 'laptop'

Option #2. You are making a list of the sensors. Im pretty sure the first option would error out.

Since you are having issues with nmap you may want to look at the ping binary sensor.

It reports connected/disconnected on the front end. Rather than create a device tracker and convert it to a sensor that ping sensor may be easier.

Also a bit less traffic on your network. Nmap is by no means a lot of bandwith but it scans 100 ports each scan and you have it set for 4 times a minute so 400 ports being scanned multiplied by the number of hosts you select each minute.

Edit: keep in mind some devices (nest thermostat, amazon echo for example) donā€™t respond to ping requests. I have found these donā€™t seem to work with the nmap component either.

Thanks. Option 2 works.

Ping was even worseā€¦ It didnā€™t report them not_home. Ill change the 4 min to longer when im done testing. No need for 4 times a minute.

Maby my windows pcā€™s are setup wrong, i dont know wats wrong.

Hey @badgerhome how did you set up your rfid ? Have you got the esp8266 arduino libraries and code? I have a pn532 and cannot set it up with my esp8266.

Can you please help?

Thanks.

This is how I set mine up: DIY Poor man's Alarm Control Panel

1 Like

Hi,
I have been doing the template change from home to online for a while now, but it is getting a bit boring.
Has anybody found a way to automate this?
Generic Template and a big loop?
MQTT sensor similar to the battery alert?
Maybe Lovelace filter cards?