Template Sensor returning 'true' when sensor is not available

Good morning,

I have an ultrasonic sensor measuring the distance to the bottom of my garage, so that when my garage door opens and goes beneath the sensor, I get a notification that the garage door was opened (and vice versa). This is my code:

- alias: "Garagentor öffnet"
  trigger:
  - platform: template
    value_template: "{{ states('sensor.garagentor')|float < 0.2 }}"
    for:
      seconds: 3
  action:
    - service: notify.telegram_alle
      data:
        title: '*Garagentor*'
        message: 'Das Garagentor wurde geöffnet'

That works like a charm, the only problem I have is that when I reboot HA or update the NodeMCU via ESPhome, the sensor sensor.garagentor goes to “not available” and the remplate is rendering as “true” and therefore I receive a notification.

Is there a way I can prevent this from happening? Would it work if instead of saying it has to be smaller than 0.2, it also has to be bigger than 0? If yes, how can I do that?

Thank you and have a nice sunday!

Sascha

Or you could add a condition so that it doesn’t 't execute when the state is “unknown” or “unavailable”.

HA seem to have changed the whole way unknown states are handled in the last 2 versions - causing a bunch of issues like this.

value_template: "{{ states('sensor.garagentor')|float(1.0) < 0.2 }}"

This will default the state to “1.0” if it’s not an actual number, rather than the default default of 0.0.

1 Like

That’s it! Thank you!