Automation use trigger entity_id as part of the action

Hi,

I’ve been struggling with an actionable notification to turn off a specific light.

I have three bathroom lights using Shelly switches, and have the following script:

alias: Bathroom Lights Left On
description: >-
  Notify and offer to turn off any of the bathroom lights left on longer than
  20min
trigger:
  - platform: state
    entity_id: >-
      switch.shelly_shsw_pm_d8bfc019b60d, switch.shelly_shsw_pm_8caab574c685,
      switch.shelly_shsw_pm_98cdac1e2737
    from: 'off'
    to: 'on'
    for:
      hours: 0
      minutes: 0
      seconds: 2
condition: []
action:
  - data_template:
      message: '{{ trigger.to_state.name }} has been left on'
      title: Bathroom light left on!
      data:
        actions:
          - action: turn_off_bathroom_light
            title: Turn off {{ trigger.to_state.name }}
    service: notify.mobile_app_nick
  - wait_for_trigger:
      - platform: event
        event_type: ios.notification_action_fired
        event_data:
          actionName: turn_off_bathroom_light
    continue_on_timeout: false
  - service: switch.turn_off
    data: {}
    target: 
      - entity_id: '{{ trigger.to_state.name }}'
mode: single

I get the correct friendly_name in my notification, but I’m then wondering how to turn off the light that triggered the automation?

Currently I’m getting a ‘Message malformed: expected a dictionary for dictionary value @ data[‘action’][2][‘target’]’ error but not sure how to address it.

Can anyone advise?

Looking at the docs you want trigger.entity_id not trigger.to_state.name.

You can’t ever use the name/friendly name to act on an entity, since you could call every entity Whale if you wanted to.

Thanks, I’m sure I wrote entity_id but I’ve tried a few things, so who knows…

Anyway I tried the following but still getting the error. Have I written the entity_id in the right place?

 - service: switch.turn_off
    data: {}
    target: 
      - entity_id: '{{ trigger.entity_id }}'

Got it!

alias: Bathroom Lights Left On
description: >-
  Notify and offer to turn off any of the bathroom lights left on longer than
  20min
trigger:
  - platform: state
    entity_id: >-
      switch.shelly_shsw_pm_d8bfc019b60d, switch.shelly_shsw_pm_8caab574c685,
      switch.shelly_shsw_pm_98cdac1e2737
    from: 'off'
    to: 'on'
    for:
      hours: 0
      minutes: 0
      seconds: 2
condition: []
action:
  - data_template:
      message: '{{ trigger.to_state.name }} has been left on'
      title: Bathroom light left on!
      data:
        actions:
          - action: turn_off_bathroom_light
            title: Turn off {{ trigger.to_state.name }}
    service: notify.mobile_app_nick
  - wait_for_trigger:
      - platform: event
        event_type: ios.notification_action_fired
        event_data:
          actionName: turn_off_bathroom_light
    continue_on_timeout: false
  - service: switch.turn_off
    target: 
      entity_id: "{{ trigger.entity_id }}"
mode: single