Access state information from service_call event automation

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 :slight_smile:

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?

Thanks!

 {{trigger.to_state.state}}

And

 {{trigger.from_state.state}}

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’

Yeah, sorry, you’re right.

Use last_changed… here is my Garage Door/Alarm Set automation…

- id: 'closegaragesetalarm'
  alias: Close Garage and Set Alarm
  trigger:
  - entity_id: input_boolean.alarmgaragedoor
    from: 'off'
    platform: state
    to: 'on'
  action:
  - service: switch.turn_on
    data:
      entity_id: switch.sonoff51083
  - delay: '00:00:10'
  - service: switch.turn_on
    data:
      entity_id: switch.sonoff63719
  - delay: '00:00:10'
  - service: notify.david_ios_notify
    data_template:
      message: >
        {%- if (as_timestamp(now()) - as_timestamp(states.switch.sonoff63719.last_changed)
        < 60 ) and (as_timestamp(now()) - as_timestamp(states.switch.sonoff51083.last_changed) 
        < 60 ) %} Garage closed & Alarm armed Verified {%- else %} Error occurred arming {%- endif %}
      title: 'Home Arming Notification'
  - service: persistent_notification.create
    data_template:
      message: >
        {%- if (as_timestamp(now()) - as_timestamp(states.switch.sonoff63719.last_changed)
        < 60 ) and (as_timestamp(now()) - as_timestamp(states.switch.sonoff51083.last_changed) 
        < 60 ) %} Garage closed & Alarm armed Verified {%- else %} Error occurred arming {%- endif %}

See the Data Template

It’s not 100% clear what you want in the message. But how about something like this:

message: >
  Garage door 2 was {{
    'closed' if trigger.event.data.service[0] == 'c' else 'opened'
  }}.

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.

Thanks for sharing!

1 Like

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'
        }}.
- alias: 'State changed Active'
  id: 'State changed Active'
  initial_state: 'off'
  trigger:
    platform: event
    event_type: state_changed
  condition:
    condition: template
    value_template: >
      {{ trigger.event.data.entity_id in state_attr('group.binary_sensors_active_threshold','entity_id') }}
  action:
    - delay:
        seconds: 2
    - service: python_script.summary

this works alright, never mind the action part, but as an example how you could use the trigger for the platform event. Hope this helps.

Thank you for the ideas and information.

I swear I tried {{states(trigger.event.data.service_data.entity_id)}} but maybe not quite that combination, this worked!.

When I get some time I’ll write another automation with the state_changed event and see what trigger.event.context gives me.