I have an automation that notifies me if the garage open and close services are called. I do this because I don’t want notified when the garage is open/closed from our normal remotes/wall button. I only want know when home assistant is triggering the change. The automation works well but I’d like to include either the current, to, or from state in the message. I don’t see state info available in the call_service event type and trying to derive it isn’t working either.
- alias: Garage Service Called
trigger:
- platform: event
event_type: call_service
event_data:
service_data:
entity_id: cover.garage_door_opener_2
domain: cover
service: close_cover
- platform: event
event_type: call_service
event_data:
service_data:
entity_id: cover.garage_door_opener_2
domain: cover
service: open_cover
action:
service: notify.emailnotify
data_template:
message: 'The garage door service {{ trigger.event.data.service }} was called. Current state is {{states("trigger.event.data.service_data.entity_id")}}'
The above results in this message
Home Assistant / The garage door service open_cover was called. Current state is unknown
Calling {{states(“cover.garage_door_opener_2”)}} works and shows the originating state , but that’s no fun
I’ve looked at even logs when the service is called and things look right to me… Is there any way to pull state info into this service call triggered notify automation?
Thank you for the reply. Based on Automation trigger variables - Home Assistant I don’t think the state values are available for the event platform. I tried this back pre-90 and didn’t have much luck. I threw them on the end of my data_tempalte message to test again and I do not get a notification, but I do get this error:
Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘to_state’
This is an interesting way to detect when ha called the service vs something else. Since it uses the state platform I bet I can access the entity to/from state info.
Yeah sorry about that. I’m simply trying to put either the from state or to state in the message based on the entity the service called.
With the way my automation is setup, I still get notified if I request a close action on a closed door. Because of this I’d like to know the actual to/from state. If I was using the state platform this wouldn’t happen since the state isn’t changing.
You might have better luck using the state_changed event instead of the call_service event, and then look at trigger.event.context. The values in that dictionary might be used to determine if the state changed as a result of something HA did vs the cover being changed manually (i.e., external to HA.) Not sure. I know this is ultimately one of the reasons they added context to everything. I’m just not sure it’s in a state yet where it can support what you want to do.
Or, depending on timing, it’s possible the state that the cover was in before the call_service event happened might still be available via states(trigger.event.data.service_data.entity_id) [or simply states('cover.garage_door_opener_2')] in the automation’s condition & action parts. Have you tried something like:
condition:
condition: template
value_template: >
{% set open = is_state('cover.garage_door_opener_2', 'on') %}
{% set opening = trigger.event.data.service[0] == 'o' %}
{{ not open and opening or open and not opening }}
action:
service: notify.emailnotify
data_template:
message: >
Garage door 2 was {{
'closed' if trigger.event.data.service[0] == 'c' else 'opened'
}}.