Binary_Sensor Template for Smoking/Not Smoking Troublshooting

Door sensor where open means Not Smoking and closed means Smoking.

I attempted to use a binary_sensor template for the smoking status but I only get on and off.

Any help is appreciated.

  - platform: template
    sensors:
      timsmoke_status:
        friendly_name: "Tim Status"
        entity_id:
          - binary_sensor.stairwell_door_contact
        value_template: >
          {% if is_state('binary_sensor.stairwell_door_contact', 'on') %}
            Not_Smoking
          {% else %}
            Smoking
          {% endif %}
        icon_template: >
          {% if is_state('binary_sensor.stairwell_door_contact', 'on') %}
            mdi:smoking-off
          {% else %}
            mdi:smoking
          {% endif %}

The icon change works great! Just the text won’t change.
2019-09-06_22h03_22

So just the icon changes:

image

hmm…

Binary sensors only have two states, on and off (corresponding to true / false in a template binary sensor). Even if you apply a device class to change the way it is displayed in the front end, the state of the binary sensor is still on / off.

If you configure a template sensor (as opposed to a template binary_sensor) you can have any states you want. All you have to do is move your code to the sensor: section instead of the binary_sensor: section of your configuration.yaml file (or included files if using them).

1 Like

Thanks that did it!