As titled, I would like to trigger an automation if an entity disappears, or doesn’t exist in HA anymore, is this possible?
Trigger on “unknown” state
There is no state per say, the entity doesn’t exist, it only returns after a reboot.
Yes but the mechanism depends a bit on how the entity is defined.
If the entity is in the registry (has a unique ID, can be put in an area, name can be changed from the UI, etc.) then in order for it to be deleted the entity registry would need to be updated. An event is fired when this happens, it has type entity_registry_updated
. You can listen for it and trigger off of it.
If the entity is not in the registry then there’s really no event you can specifically listen for to know when this happens. It’s not a state change or anything, maybe the particular integration you’re using fires an event when it removes the entity but you’d have to tell us more about the use case to know. Most likely the best you can do is a time pattern trigger. Basically just decide how much time between when the entity disappears and the action executes is acceptable to you and set it accordingly.
Pick a trigger then the rest of the automation is the same:
trigger:
(whatever you picked)
variables:
disappearing_entity: entity.some_id
condition: >-
{{ states | selectattr('entity_id', 'eq', disappearing_entity) | list | count == 0 }}
action:
(handle the entity being missing)
Thanks for the detailed response, but I am not sure how to setup the trigger for entity_registry_updated
, any pointers.
Would it work like this?
trigger:
- platform: event
event_type: entity_registry_updated
condition:
- condition: template
value_template: "{{trigger.event.data.entity_id == 'sensor.missing_entity' }}"
So far, I created a template sensor which changes to unknown
when the entity disappears.
You got the trigger correct but you seem to have changed the condition. To my knowledge the entity_registry_updated
event has no data, it just lets any interested parties know the registry updated. So I’m pretty sure this:
"{{trigger.event.data.entity_id == 'sensor.missing_entity' }}"
Isn’t going to work.
What was wrong with the condition I laid out?
condition: >-
{{ states | selectattr('entity_id', 'eq', disappearing_entity) | list | count == 0 }}
My bad, I was just a little overwhelmed by your response, and unintentionally overlooked that.
So, like this?
trigger:
- platform: event
event_type: entity_registry_updated
condition: >-
{{ states | selectattr('entity_id', 'eq', sensor.missing_entity) | list | count == 0 }}
action:
- service: notify.notify
data:
message: Missing Entity Disappeared
Yep. And you’re certain the disappearing sensor is in the registry right? Like when isn’t missing you can edit its name and ID from the UI, add it to an area, etc.?
Not really, it shows the following when I select the entity in developer tools.
This entity ('sensor.missing_entity') does not have a unique ID, therefore its settings cannot be managed from the UI.
Right that means it’s not in the registry. So that type of trigger won’t work, you’ll need to use a time pattern trigger instead.
That’s what I was saying in the post above. If its in the registry you can listen for the registry. If it’s not then the best you can do is a time pattern trigger.
Ok, would the following work?, maybe not if the entity doesn’t even exist?
trigger:
- platform: state
entity_id: sensor.missing_entity
to: 'null'
No that wont work. An entity being deleted does not fire a state change event so you can’t listen for it that way.
Ok thanks, I’ll stick with the template sensor for now.
I just want to mention jazzyisj’s work: Unknown / unavailable entity monitoring. An example automation can be found in posting #212.
Just out of curiosity, what kind of entity do you have that is completely disappearing from the states object and not simply going into an unknown or unavailable state. The only time I’ve ever come across this is a persistent notification that has been dismissed.