Change Icon when state changes on Binary Sensor

Ugh here we go again… and I thought I was “done”. :rofl:

Ah, OK. didn’t realize that.

I thought (and did…) the same until Marius mentioned in a few other threads that some of the stuff still worked.

I thought andrey stopped developing it when Lovelace came around.

Yes, those are really good tools. I really hated that there were things that stopped working in the custom-ui until I saw he had implemented functional replacements for the things I was missing. And for that reason I decided to just not use the custom-ui any more and stick with custom cards/tools. One less thing to have to maintain and update when HA changes.

that’s true.

I believe that in case of Lovelace it’s not necessary.

One of the reasons I use the custom-template card so I can avoid creating a lot more sensors.

Plenty of examples here in this thread 📝 100% Templatable Lovelace Configurations

I don’t have an example and I’m not going to write one from scratch. There are examples in that thread that do exactly what you are looking for with the state. I, just like you, have to work through it and ask questions specific to your needs if you get stuck. That’s what I did and you learn a lot more that way instead of being spoon fed.

Hi David, thanks.
Gees you have a point there. Trying to be as ‘thin’ as possible is always a good thing.
Though you must admit the configurations with this card are hardly simpler than a small customization in the backend…:wink:

my biggest worry with using the config template card would be that one has to do so in each location in the frontend configuration files. It might even go straight against the design philosophy of Lovelace, aimed at separating the front end from the back end better than with regular HA. Customizing an entity makes that separation a lot easier. And makes those cards a lot easier too, end less error prone. You must admit, some of these examples are quite the works…

Of course, creating a state dependent front-end representation is dearly needed in Lovelace, and I am still hoping it will be made available in regular jinja templates anytime soon. Or Javascript. I dread having to ‘learn’ yet another templating syntax, each custom cards seems to develop techniques of their own (markdown , custom compact header, config template card, etc etc)

Also, won’t repeat it here but still: 📝 100% Templatable Lovelace Configurations - #65 by Mariusthvdb

I’m experiencing rather a sad time, having invested some serious development in the regular Tiles card., which now is slowly or abruptly being deprecated, and stripped of their functionality. I don’t want to experience that again, investing in setting up the frontend with the config template card, when Lovelace might just integrate that natively, and render the custom card useless…

Having said that, I have a few items the card may just be the right thing for now, so if only for reasons of keeping up, I will add it to the config.

Ha! I never said it was easy lol! However after doing the one I did for MQTT, it was a doddle doing the one for my iPxxx devices and also my ISP usage meter one… but yes… not easy at first. That’s also why I posted my finished code as there wasn’t much to go off when I started using it.

not sure what you mean by that…

yep!

Seems that is unlikely to ever happen but you never know…

I agree but it is what it is!

Understood and it seems to be holding you back from the latest versions of HA… Lots of things now will only work with later versions (iOS app v2 for instance). I get your grief but it’s best to try and keep up and adjust as you go.

I’m concerned about that as well… One card I use - the dev has just disappeared - at least someone else has picked it up but it’s an ever present danger a dev will just lose interest and move on to something else. The more of that custom stuff that finds it’s way into core the better but a lot of things seem to be at odds with the devs vision for core…

Good call I think.

1 Like

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?