Ignore device in automations if unavailable or not found

So the latest changes with the MyQ integration did some weird things… first it broke my automations… I have the light set to turn on with the garage door and off after a few minutes when it’s closed… This has not been functioning at all for a week or so.

I finally got into my configs to see what happened and it appears my MyQ device has changed names… the old device named “cover.garage_door_opener” does not exist anymore… now I find it listed as “cover.garage_door_opener_2”

I’m not sure why this happened… but that’s not my immediate concern… my main problem is the light hasn’t been turning off automatically because the automation is looking for the state of “cover.garage_door_opener” to be “closed” but since the device doesn’t exist… it never turns off anymore…

Is there a way to configure an automation to ignore an entity that is either “unavailable” or just doesn’t exist anymore for some reason?

Here’s what my automation looks like now:

  - alias: a_garage_inside_light_timer
    initial_state: True
    trigger:
      - platform: state
        entity_id: cover.garage_door_opener_2
        to: 'closed'
        for:
          minutes: 7
      - platform: state
        entity_id: binary_sensor.contactgaragesidedoor, binary_sensor.contactgaragehouseentrydoor
        to: 'off'
        for:
          minutes: 7
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: input_boolean.garage_light_timer_override
          state: 'off'
        - condition: state
          entity_id: binary_sensor.contactgaragesidedoor
          state: 'off'
          for:
            minutes: 7
        - condition: state
          entity_id: binary_sensor.contactgaragehouseentrydoor
          state: 'off'
          for:
            minutes: 7
        - condition: state
          entity_id: cover.garage_door_opener_2
          state: 'closed'
          for:
            minutes: 7
    action:
      service: homeassistant.turn_off
      entity_id: switch.garagemainlight

Huh, I’m having a similar problem 6 months later and I came upon my own question, still without a solution… anyone have any suggestions?

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

Thanks so much! This is helpful. I ended up doing something similar and just adding the ‘unavailable’ state in addition to ‘on’ or ‘off’… but I think your way of doing a template should work better than what I’ve been doing as it appears to add some additional flexibility.

I have a similar problem

Is there a way to have a sensor with only 2 states in Home assitant ? Not Binary sensor

I have a ESP home sensor that detects Power outage. Logically it should say ON or OFF in other words Running on Power or Running on Generator.

But i see it also goes to 3rd state in the following cases.

Case 1: “Unavailable” either during switchover of Generator to Power or vise versa.

Case 2: “Unavailable” when there is a wifi disconnection & reconnect.

In both cases (1 & 2) it’s a false alarm.

Hence i need to create a sensor which can have the following

ON to Unavailabe to ON = ON State only (NO change in sensor state, retain the state before unavailable & dont trigger any automation)

OFF to Unavailabe to OFF = ON State only (NO change in sensor stateretain the state before unavailable & dont trigger any automation)

ON to Unavailabe to OFF = OFF state (State before unavailable & after unavailable are diffrent hence change the sensor state to latest (OFF) & trigger a set of automation)

OFF to Unavailabe to ON= ON State (State before unavailable & after unavailable are diffrent hence change the sensor state to latest (ON) & trigger a set of automation)

Is this possible?

can you explain what {{ states('input_boolean.garage_light_timer_override') in ['off', 'unavailable'] }}" syntax means?? if the state of input_boolean.garage_light_timer_override is unavilable assume it as off?? Is my understanding right??

Thanks in advance

I believe in triggers you can actually specify transitional states… So what you are trying to achieve should be possible but I am not super familiar with the methodology myself, sorry.

To clarify, I mean you should be able to trigger only when it transitions from on to unavailable to off, for example… Though as I am thinking about it… That might not be possible…

You could probably achieve it by keeping track of the previous states with a text_input or something though.

That is correct. I am assuming here that off and unavailable are the same thing. Probably won’t work for a lot of automations but it works for my needs.