Template Binary Sensor doesn't update

Hey all, I’ve found a workaround by using Template Sensor instead of Template Binary Sensor but for whatever reason when I set up the Template Binary Sensor this way, it doesn’t update…

binary_sensor:
  - platform: template
    sensors:
      driveway_motion:
            friendly_name: "Driveway Motion"
            entity_id: 
                - sensor.zooz_unknown_type_0001_id_0005_burglar
            value_template: >-
              {{ states.sensor.zooz_unknown_type_0001_id_0005_burglar | int > 0 }}

Instead I’m doing this which updates and works totally as expected:

sensor:
  - platform: template
    sensors:
        driveway_motion:
           friendly_name: "Driveway Motion"
           entity_id: 
               - sensor.zooz_unknown_type_0001_id_0005_burglar
           value_template: "{% if states('sensor.zooz_unknown_type_0001_id_0005_burglar') | int > 0 %} on {% else %} off {% endif %}"  

Not sure what to make of that, but thought I’d share in case anyone can tell me what I did wrong.

Long time lurker, first time poster :grin: Thanks!

Your first template doesn’t return a state. It’s returning the whole state object. The second example returns the state, so it works. To make the first example work, you need to add .state at the end of your states object:

{{ states.sensor.zooz_unknown_type_0001_id_0005_burglar.state | int > 0 }}

But to be honest, your second template is safer because the method will always return a value where the state object may not return a value if the object doesn’t exist. Stick with what you got. Use the states() method when you can, avoid state objects directly unless coming from trigger state object.

2 Likes

Oh yes good catch! I actually did correct that at one point and it still didn’t work, tried many variations. But yeah I’ll go with what I got, thank you for your reply!