Changing Binary Sensor Icon Based on Value

I’ve created the following Binary Switch:

- platform: template
  sensors:
    tiago_sleep:
        value_template: >
            {{ (states.sensor.mi_note_10_sleep_confidence.state|float) > 95}}
        icon_template: >-
            {% if state('sensor.mi_note_10_sleep_confidence')|float > 95}
              mdi:sleep
            {% else %}
              mdi:sleep-off
            {% endif %}

Returns True when (my phone thinks that) I’m asleep and False if not. That part works.

The problem is that I’d like the sensor icon to switch based on it being True or False but I’m kinda stuck.

Help?

You should read about the states in the Templating docs and then take a look at the example for the icon_template.

EDIT: Sorry, the docs use the new template format, but take a look at the if, else, endif syntax.

1 Like

Thanks! Found what I needed.

Actually you haven’t created a binary sensor nor a binary switch.

you created a sensor (not binary) that returns the states ‘true’ & ‘false’.

Even tho they look similar because of the code you used they are different.

Binary sensors can only ever have the state of ‘on’ or ‘off’ while a sensor (not binary) can be pretty much anything you want it to be - ‘true’/‘false’, ‘0’/‘1’, ‘0’/‘2’/‘4’, ‘yellow’/'blue/‘purple’/‘red’/‘etc’.

you may already know that but from the title of the thread and the words in the OP I wasn’t sure.

Yeah sorry. I tried to create a binary sensor but through trial, this solution was the one I managed to get working.