Template Binary Sensor will NOT update

I have a monoprice Z-Wave recessed door sensor. The binary sensor that gets setup in discovery doesn’t work. But I do have a lot of other sensors that I can use.

sensor.door_1_alarm reports 0 or 255 when the door is closed and open. This is very consistent. So I created a binary template sensor:

binary_sensor:
  - platform: template
    sensors:
      pool_door_open:
        friendly_name: "Pool Door"
        device_class: door
        value_template: >
         {{ is_state('sensor.door_1_alarm', '255')}}
          #{{ states.sensor.door_1_alarm|int > 0 }}
          #{{ states('sensor.door_1_alarm')|int > 0 }}

I’ve tried all three lines above. And I have yet to ever get HA to update that sensor.

What is it that I’m missing here?

You need to specifically declare the entity_id the template uses so HA knows what to monitor for changes. Try this:

binary_sensor:
  - platform: template
    sensors:
      pool_door_open:
        friendly_name: "Pool Door"
        device_class: door
        entity_id: sensor.door_1_alarm #### <---------- Add this
        value_template: >
         {{ is_state('sensor.door_1_alarm', '255')}}
          #{{ states.sensor.door_1_alarm|int > 0 }}
          #{{ states('sensor.door_1_alarm')|int > 0 }}
1 Like

Thank you for replying,. I’ve added that, restarted, and while the sensor ‘badge’ on the states page will show the alarm change from 0 to 255 when opening the door, the binary sensor “pool door” shows no change since startup.
image
image

binary_sensor:
  - platform: template
    sensors:
      pool_door_open:
        friendly_name: "Pool Door"
        device_class: door
        entity_id: sensor.door_1_alarm
        value_template: >
         {{ is_state('sensor.door_1_alarm', '255')}}
          #{{ states.sensor.door_1_alarm|int > 0 }}
          #{{ states('sensor.door_1_alarm')|int > 0 }}

Check the development tools (lower left, sidebar).

There’s a states page (<> button) where you can see that actual sensor entity id and state. Is 255 actually the state or an attribute of the sensor? Is the id correct?

There is also a template editor where you can test templates.

It’s definitely the sensor value. From the states/overview page and each line was tested in template editor. It gave true/false readings in template editor and would change, but after I added a space or updated the template text. But the binary sensor still doesn’t move

How many times does this appear in your configuration:

binary_sensor:

That’s normal.

Also, try without the quotes for the value:

{{ is_state('sensor.door_1_alarm', 255)}}

Just the once. In all *.yaml.

Ok good. Just making sure.

Ah. Remove the commented out templates!

Comments are interpreted in templates. That has trapped me before.

1 Like

That was it!!

Thank you so much.

2 Likes

Just created an account to say thanks for this!

I was going crazy trying to figure out why the sensor wouldn’t update, and of course the more it wouldn’t work the more commented out attempts went in there!
One less mystery.

This should be in big bold text somewhere…

@tom_l and toast,
If playing in the template area the commented lines all need to be at the end AND they need to start very near or preferably at the first column, otherwise the interpreter tries to read them as template
You can also use jinja comments eg {# ‘hello world’ #}

2 Likes

I had some commented out template statements in the actual configuration file. Once deleted, the sensor worked as expected!