Change Icon when state changes on Binary Sensor

Hi

First time post, apologies if this is easy. I have 2 TV’s that I am using the binary sensor for a PING and the device_class is set to power in my customized.yaml file along with icon: MDI:television

Below is whats in my configuration.yaml

What I would like to know is 2 things. How do I make a template for these and how can I make the icon change between on and off state. In the off state, I would like to use the mdi:televsion-off icon. Currently the icon changes colour when on and off

    binary_sensor:

  - platform: ping
    host: 192.168.0.10
    scan_interval: 30
    name: lg_tv_status
    
  - platform: ping
    host: 192.168.0.11
    scan_interval: 30
    name: bed_tv_status

Thanks in advance

Martyn

2 Likes

you can’t do that using the built-in functions because the icons don’t accept a template.

you can change it using a custom-ui developed by another user. but it needs to be installed.

see here:

Can’t he just create his own binary sensor using the state of the ping binary sensor and his own template to get the effect he wants?

  - platform: template
    sensors:
       lg_tv_status_sensor:
        friendly_name: LG TV Status Sensor
        icon_template: >
          {% if is_state('binary_sensor.lg_tv_status_sensor','on') %} mdi:television
          {% else %} mdi:television-off
          {% endif %}
        device_class: power
        value_template: >-
          {% if not states('binary_sensor.lg_tv_status') == 'unknown' and is_state('binary_sensor.lg_tv_status', 'on') %}
             true
          {% endif %}

This should give you a sensor using the state of the ping sensor, the displayed state of the power device class, using the television icons. I think. Not at home so I can’t test.

3 Likes

Hi

I can test that but which file do I put it in configuration.yaml or customized.yaml as I noticed it doesnt have the ip address?

Also, would I have to duplicate this for the 2nd tv ?

Thanks

Martyn

yeah, he could do that. good idea!

Put it in your configuration.yaml near where you have the ping sensors set up.

Yes you have to duplicate it for the second TV. These sensors are really just a customized duplicate of the original ping sensors you created. Use these new sensors in your front end.

You can add hidden: true in your customize.yaml to the ping sensor configurations if you wish to “hide” the now duplicate ping sensors from home assistant.

Thanks. I tried copying that in but got the error

Validate your configuration if you recently made some changes to your configuration and want to make sure that it is all valid

Configuration invalidCHECK CONFIG

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected ‘)’) for dictionary value @ data[‘sensors’][‘lg_tv_status_sensor’][‘value_template’]. Got “{% if not states(‘binary_sensor.lg_tv_status’) == ‘unknown’ and ‘binary_sensor.lg_tv_status’,‘on’) %}\n true\n{% endif %}” value is not allowed for dictionary value @ data[‘sensors’][‘lg_tv_status_sensor’][‘device_class’]. Got ‘power’. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/

Thanks

Martyn

Yeah there’s a typo in there. Typed it in a hurry.

Good exercise for you tho - you’re going to have to learn how to decipher those errors!

Third last line should be.

{% if not states('binary_sensor.lg_tv_status') == 'unknown' and is_state('binary_sensor.lg_tv_status', 'on') %}

2 Likes

Go over this page here to understand how that was put together.

1 Like

Thanks

Got rid of the errors and reloaded but icons not changed.

I noticed television-off is not available on this but changed off to mdi-power and nothing

Will read tomorrow. bed now

Thanks again

Perhaps look at this custom-card

What do you mean “not available on this”?

And do I get it right that you want to have a changeable (based on your ping sensor) icon for your TV that changes its colour depending on state (On/Off)?

Hi

I saw that mdi:television icon was available as an icon but then I saw mdi:television-off icon was available too as an icon on a webpage I was looking at but looking now, I cant see mdi:television-off.

I might use mdi:mdi-power for off and the mdi:television for on when I get the code working.

Cant access my hassio at the moment but will look more into suggestions people make and I thank all the replies so far.

Regards

Martyn

I use this page to find my icons. mdi:television-off is available. If it’s not showing up then something else is wrong.

https://cdn.materialdesignicons.com/3.4.93/

Out of curiosity, what kind of device are you pinging for your TV. Are you sure that even in the off state that the device is not pingable? For example, I use Chromecasts. Even when a Chromecast is “off” I can ping it. In my case, if I used a ping sensor to test for power state it will never show my TV as off (unless the Chromecast is actually unplugged).

Instead I can use the media player entity for the Chromecast. Now not only can I see the state of the TV, I can control it with the same media_player entity.

If you really need to have a separate sensor of the power state of the TV (and there are case uses for that) you can create a template binary sensor that reports the state of the media player, rather than trying to ping the device.

 - platform: template
    sensors:
       lg_tv_power:
        friendly_name: LG TV Power
        icon_template: >
          {% if is_state('binary_sensor.lg_tv_power','on') %} mdi:television
          {% else %} mdi:television-off
          {% endif %}
        device_class: power
        value_template: >-
          {% if not is_state('media_player.lg_tv', 'off') and not is_state('media_player.lg_tv','unavailable') %}
            true
          {% endif %}

I’m pretty sure that jut because the icon is listed on the mdi website that it doesn’t guarantee that it is included in HA.

HA has to bring in the list of icons from mdi before they are available inside HA. If it’s a new icon it might not be available in HA for a bit until the next update.

this is a remarkable template sensor suggestion…

but firstly: using this template sensor creates an extra sensor in the system.
Using custom-ui merely customizes the existing sensor. Less overhead. Especially when used for more states, an believe me you will use it in more situations once you get the taste for it :wink:

Personally I try to keep my sensors unique, and don’t want a regular and a template sensor for the same device…

about the posted suggestion:
Both device_class:power and an icon_template are used. I would advice one or the other. The whole Idea of the device_class is to automate icons according to the state (and display of state)

the value_template is trying to imitate a binary_sensor template. Why not simply use that platform?Template - Home Assistant If you do that, the value can either be true or false (or any of the other binary duo’s) and the exception isn’t needed in the template.

value_template: >
  {{ is_state('binary_sensor.lg_tv_status_sensor','on') }}

would do it.

correct, there’s always some lag between updates. found out today myself again… still using 84.3 of course :wink: Always test with available icons is a lesson learned early on.

and then, of course there’s the binary_sensor Ping Ping (ICMP) - Home Assistant

might be just the thing OP needs?

As far as I can tell @dicko is just starting out. I was tying to keep it as basic as possible. Does custom:ui actually work with lovelace now? I didn’t think it did so I haven’t used it since I switched over. If it does that could make things interesting! And I’m assuming at this point any new users will jump right into lovelace UI since it is the default now.

device_class:power and an icon_template are used

I included the device_class to get the reported state like you mentioned. I’ve done similar things with many of my binary sensors (used a device class to display the state with my own icons). It seems to work well. Is there a disadvantage to doing it this way?

Re duplicate sensors - I hear ya! That is why I suggested using hidden: true in the customize.yaml for the duplicate sensor earlier in the thread.

I’m not sure what you mean about the binary_sensor.template? That is exactly what I used here? I see I didn’t show that, so this could have been either or. I guess I assumed we were all on the same page using binary sensor since we started with the ping sensor and required on/off state. I suppose I should be specific about that in the future. You are absolutely correct though, in this case I could have dropped the test for unknown, it was pointless. I think I copied the base from one of my regular sensors and didn’t go over the code carefully enough (as evidenced by the typo that was originally in there too!)

of course there’s the binary_sensor Ping

The ping sensor is what we started out with at the top of the thread! I have a feeling that his TV device (whatever it is) is still pingable in the off state like a Chromecast and that is where the problem currently lies. That and I’m not sure why he isn’t using a media player entity for his TV. My only experience with TV’s is using a Chromecast so I could be wrong here.

I’ve only been working with HA for a year or so now myself and am now trying to pay forward some of the help I’ve gotten along the way. So if anything I suggest can be done better or more efficiently lay it on me - I’m all ears!

1 Like

I’m pretty sure that jut because the icon is listed on the mdi website that it doesn’t guarantee that it is included in HA.

True enough. In this case I did test md:television-off since I don’t use it myself to make sure it was available.

heres a bit of custom-ui that still works just fine on 91.2, even in Lovelace (with the exception of the _stateDisplay which was deprecated somewhere in the 88 region, still in my customize because I have another system run-in 84.3):

    binary_sensor.frontdoor_motion_sensor:
      show_last_changed: true
      templates:
        icon_color: >
          if (state === 'on') return 'rgb(192, 39, 30)';
          return 'rgb(54, 95, 140)';
        icon: >
          if (state === 'on') return 'mdi:run-fast';
          return 'mdi:security-home';
        _stateDisplay: >
          if (state === 'on') return 'Alert';
          return 'Clear';
      group:
        group.philips_motion_sensors:
          friendly_name: Frontdoor

one could do this all and more (themes !) with the binary_sensor ping from the first post. provided one installs custom-ui :wink:
could have used device_class: motion here (doesn’t need custom-ui) but I wanted different icons…

Huh. For some reason I didn’t think CustomUI and Lovelace played nice together so I stripped out all the custom ui out of my frontend… ugh. That was months ago too so I doubt I have any of it saved now. I don’t understand why this isn’t more common knowledge, I see questions about state dependant icon colours all the time (why this isn’t a baked in feature of HA I don’t understand.)

Ah here it is. Found one of the threads that caused me to dump Custom-UI. I also got the impression here that supporting CustomUI isn’t a priority for the dev so I didn’t want to invest too much of my UI in it.

But if you’ve got it working I might just give it another go. Really the icon colours is the only feature I’m missing very much. Thomas Lovens plugins took care of much of the rest.

Sorry for hijacking your thread @dicko!