Template Sensor has unexpected value at startup

Hello,

I have the following template binary sensor defined:

- binary_sensor:
    - name: "Occupancy Sensor Debounced"
      unique_id: "occupancy_sensor_debounced"
      state: >
        {{
          is_state('binary_sensor.some_contact_sensor', 'on')
        }}
      delay_on:
        minutes: 2
      delay_off:
        minutes: 5
      device_class: occupancy

After restarting home assistant, this sensor turns on, i.e. occupancy is detected. I believe this is because the delay_off is set to 5 minutes; I have another very similar sensor, using the same contact sensor as source, without any delay_on or delay_off, and that one at startup works properly.

My source contact sensor at startup goes first unknown, then transitions to off; I would expect my template sensor to never turn on.

Can anyone help me in making this work? For me, it is acceptable to play around with the delays if it helps, or to provide some default state if possible.

Thanks for your help!

Cheers,
V

Does this work?

availability: "{{ states('binary_sensor.some_contact_sensor') in ('on', 'off') }}"

Thanks Troon, it did the trick. I still wonder what precedence is applied when both the delay_on/delay_off options are set, but I guess in general it is better to have sanitised template sensor.

1 Like