Ignore device in automations if unavailable or not found

I have been having the same problem with unavailable devices and automations and after a bunch of tinkering come to the conclusion that:

  • Unavailable or unknown entities in the trigger section is fine
  • Unavailable or unknown entities in the condition section (at least using “state” conditions) makes the automation never fire

I fixed this by using “template” conditions instead, where you can check if an entity is ‘unavailable’. Your first condition would become:

   - condition: template
     value_template: "{{ states('input_boolean.garage_light_timer_override') in ['off', 'unavailable'] }}"

and your conditions that must have been in the ‘off’ state for a certain amount of time would become:

   - condition: template
     value_template: "{{ states('binary_sensor.contactgaragehouseentrydoor') in ['off', 'unavailable'] and (now() - states.binary_sensor.contactgaragehouseentrydoor.last_changed).seconds > 7 * 60 }}"

or perhaps (since it is probably not important that the sensor has been un the unavailable state for 7 minutes):

   - condition: template
     value_template: "{{ states('binary_sensor.contactgaragehouseentrydoor') == 'unavailable' or (states('binary_sensor.contactgaragehouseentrydoor') == 'off' and (now() - states.binary_sensor.contactgaragehouseentrydoor.last_changed).seconds > 7 * 60) }}"

when using the template conditions. You can also check for ‘unknown’ which is the state returned if the entity isn’t known to Home Assistant at all.

I don’t use ‘for’ in the conditions of my automations, so I haven’t tested that last piece of code much, but I think it should work. The template part of the Development Tools is very handy for testing the value templates.

Hope that helps!

3 Likes