Change Icon when state changes on Binary Sensor

Hi

Thanks for all your help. I have it working now with the following code if anyone else needs it.

binary_sensor: 
 - platform: template
sensors:
   living_room_tv_power:
    friendly_name: LG TV (Living Room)
    icon_template: >
      {% if is_state('binary_sensor.living_room_tv_status','on') %} mdi:television
      {% else %} mdi:television-off
      {% endif %}
    device_class: power
    value_template: >-
      {% if not is_state('binary_sensor.living_room_tv_status', 'off') and not 
      is_state('binary_sensor.living_room_tv_status','unavailable') %}
        true
      {% endif %}
      
- platform: ping
    host: 192.168.0.10
    scan_interval: 30
    name: living_room_tv_status

Only thing is I now have 2 binary sensors for the same thing. I have tried hiding the living_room_tv_status but still 2 appear in my list.

Minor detail but would be great if I can just have 1

image

Thanks again to all

Martyn

3 Likes

how exactly did you do that?
it’s always great to complement statement like that with your code if you would like the community to help as it’s just guesswork otherwise.

that’s exactly what I tried to convey earlier, you are creating a(n unnecessary ) template sensor for the exact same entity as the binary_sensor ping.
The picture you’re showing has 2 different Tv’s though, you’d best show us your group ?card definition

1 Like

Hi

I remember you saying about 2 sensors this way but Im happy its working but now I have it working, if I can get it to 1 sensor per device, then thats even better

This is my card on the image below

below is the actual code that I am using from the configuration.yaml file. The code is just duplicated and changed for the 2nd TV.

    binary_sensor: 
  - platform: template
    sensors:
       living_room_tv_power:
        friendly_name: LG TV (Living Room)
        icon_template: >
          {% if is_state('binary_sensor.living_room_tv_status','on') %} mdi:television
          {% else %} mdi:television-off
          {% endif %}
        device_class: power
        value_template: >-
          {% if not is_state('binary_sensor.living_room_tv_status', 'off') and not is_state('binary_sensor.living_room_tv_status','unavailable') %}
            true
          {% endif %}
          
  - platform: ping
    host: 192.168.0.10
    scan_interval: 30
    name: living_room_tv_status        

Thanks again to all

Martyn

Hi,

Im taking that is a compliment, it sounds like it, so cheers if it is.

Martyn

Yes, it would have been if you included your hiding code as well.

can only repeat my words: customize the binary_sensor, and you’re set. I’ve already provided you with a template example.
no need for a template sensor, no need to hide anything in that case.

        icon_template: >
          {% if is_state('binary_sensor.lg_tv_power','on') %} mdi:television
          {% else %} mdi:television-off
          {% endif %}

would become:

homeassistant:
  customize:
    binary_sensor.lg_tv_power':
      if (state === 'on') return 'mdi:television';
      return 'mdi:television-off';

that’s it.
btw, your spacing of the word sensors: and the rest is off, it should be:

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

you still have the unavailable test in your template. Given this is a binary sensor, either being on or off, why is that? Have you ever seen in being unavailable?

1 Like

Hi

Sorry, so close but yet so far.

This is what I had in my customize.yaml

binary_sensor.living_room_tv_status:
hidden: true

I advise you to enclose any HA code between a pair of 3 backquotes to prevent formatting.

Yes, it should do the trick and actually I can see no that binary_sensor on your screenshots.
If you provide all the relevant config and screenshots in one post, I’m pretty sure even you yourself will be able to solve the puzzle.
Let’s do it :wink:

Apologies for the delay but not been near a computer for a few days.

I tried adding your template for the binary sensor but it always errored out, so went back to this and currently I have double the sensors.

Below is my configuration.yaml. Hopefully you can see where I have gone wrong. I have nothing in customize.yaml

homeassistant:
  # Name of the location where Home Assistant is running
  name: Home
  # Location required to calculate the time the sun rises and sets
  latitude: DELETED
  longitude: DELETED
  # Impacts weather/sunrise data (altitude above sea level in meters)
  elevation: 0
  # metric for Metric, imperial for Imperial
  unit_system: metric
  # Pick yours from here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
  time_zone: Europe/London
  # Customization file
  customize: !include customize.yaml

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Show the introduction message on startup.
#introduction:

# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
#   base_url: example.duckdns.org:8123

# Discover some devices automatically
discovery:

# Sensors
sensor:

  # Sunrise and sunset time badges
  
  - platform: template
    sensors:
      sunset_time:
        value_template: '{% set timestamp = as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom("%I:%M") %} {{ timestamp.lstrip("") }}'
        friendly_name: "Sunset"

  - platform: template
    sensors:
      sunrise_time:
        value_template: '{% set timestamp = as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom("%I:%M") %} {{ timestamp.lstrip("") }}'
        friendly_name: "Sunrise"

binary_sensor:

  - platform: template
    sensors:
       living_room_tv_power:
        friendly_name: LG TV (Living Room)
        icon_template: >
          {% if is_state('binary_sensor.living_room_tv_status','on') %} mdi:television
          {% else %} mdi:television-off
          {% endif %}
        device_class: power
        value_template: >-
          {% if not is_state('binary_sensor.living_room_tv_status', 'off') and not is_state('binary_sensor.living_room_tv_status','unavailable') %}
            true
          {% endif %}
          
  - platform: ping
    host: 192.168.0.10
    scan_interval: 30
    name: living_room_tv_status
    
  - platform: template
    sensors:
       luis_tv_power:
        friendly_name: JVC TV (Luis Room)
        icon_template: >
          {% if is_state('binary_sensor.luis_tv_status','on') %} mdi:television
          {% else %} mdi:television-off
          {% endif %}
        device_class: power
        value_template: >-
          {% if not is_state('binary_sensor.luis_tv_status', 'off') and not is_state('binary_sensor.luis_tv_status','unavailable') %}
            true
          {% endif %}     
         
  - platform: ping
    host: 192.168.0.11
    scan_interval: 30
    name: luis_tv_status
    
  - platform: template
    sensors:
       formuler_tv_power:
        friendly_name: Formuler Box (Living Room)
        icon_template: >
          {% if is_state('binary_sensor.formuler_tv_status','on') %} mdi:television
          {% else %} mdi:television-off
          {% endif %}
        device_class: power
        value_template: >-
          {% if not is_state('binary_sensor.formuler_tv_status', 'off') and not is_state('binary_sensor.formuler_tv_status','unavailable') %}
            true
          {% endif %}     
         
  - platform: ping
    host: 192.168.0.70
    scan_interval: 30
    name: formuler_tv_status    

# Text to speech
tts:
  - platform: google

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml

notify:
  - name: android
    platform: fcm-android

panel_iframe:
  configurator:
    title: 'Config Editor'
    url: 'http://192.168.0.89:8321'
    icon: mdi:file-tree
  
  tasmoadmin:
    title: 'Tasmo Admin'
    url: 'http://192.168.0.89:9541/login'
    icon: mdi:power-socket-uk    

Thanks again
Martyn

you create a template sensor based on ‘binary_sensor.living_room_tv_status’ but I can’t see that under the ping sensors?

Hi

I tried but it just invalidates my configuration file. Hence why my file is like it is.

Not sure what I am doing wrong.

From my above file. Any chance you can type a template up, to show me for future sensors etc ?

Thanks

Martyn

well, you are building a template sensor on another sensor the doesn’t exist.

Still not got my head around this yet.

Has anyone actually got a working binary sensor that the icon changes on the status of power on and off ?

Thanks

Me here too, fallen in this rabbit hole decided to invest some time to figure out what is available.

The only home assistant only way of doing this seems to be the platform template_sensor however this has the drawback of you just adding another sensor just to have a dynamic icon, this sounds odd to me.

By using community stuff I found a few other options:

Looks like @Mariusthvdb has made a fork of unmaintained custom-ui stuff, this is quite old, I was looking at the code and seems like the js was minified and now it is formatted again. This relies on setting customization attributes that are later parsed on the UI itself by a js module.

Yet another option is the templatable lovelace card this is a row only component for the entities card, which promises to allow to change the icon.

And last, but not least is the config-template-card which seems to be one of the best options, I ended up using this now.

Are there other options?
Do somebody know why icon_template: isn’t supported on all sensors? Or at least a icon_on:, icon_off: for all binary sensors? Seems quite common specially with the mdi icon library having several version of icons with the “off” state.

In the latest version (115.x) the template binary_sensor is reported as deprecated so anyway the old template method is dead.

deprecated? the template binary_sensor? this one: https://www.home-assistant.io/integrations/binary_sensor.template/

why would you say that?

Because the only binary_sensor I have is the one with template and I get the following error:

Logger: homeassistant.components.template.binary_sensor
Source: helpers/config_validation.py:752
Integration: template (documentation, issues)
First occurred: 12:59:52 (1 occurrences)
Last logged: 12:59:52

The 'entity_id' option is deprecated, please remove it from your configuration
Home Assistant עלה!

My code is:

- platform: template
    sensors:
      sun_up:
        entity_id:
          - binary_sensor.aviry_home_rain_alarm
        value_template: >-
          {{ is_state("binary_sensor.aviry_home_rain_alarm", "on") }}
        icon_template: >-
          {% if is_state('binary_sensor.aviry_home_rain_alarm', 'on') %}
            mdi:weather-pouring
          {% else %}
            mdi:weather-sunny
          {% endif %}

That is a change mentioned in the breaking changes for 115: entity_id is no longer required for template sensors to find the states it needs to listen to.

It has never been required for your binary_sensor because it is right there in the template anyway.

So, take out the entity_id line and you will be fine

You’re sure this is the correct template to see if the sun is up :wink:

OK. Thanks. I will check.