Context.id in an automation

I’m working on finally updating some old actionable notification automations that broke with past updates.

Reading through the updated documentation about actionable notifications, they use “context.id” in a variable.

The documentation appears to be using scripts, not automations. When trying to use a similar variable in an automation, I get errors that state that context is undefined.

The section I’m talking about is:

# inside a automation actions or script sequence
- alias: "Set up variables for the actions"  
  variables:    
  # Including an id in the action allows us to identify this script run    
  # and not accidentally trigger for other notification actions
    action_open: "{{ 'OPEN_' ~ context.id }}"
    action_close: "{{ 'CLOSE_' ~ context.id }}"

Is there a way to do something similar in an automation?

1 Like

action_open/action_close are just unique identifiers meant to link the notification and the resulting action.
You might as well use gnerated UUID’s, here, or any unique string, really.

I’m having the same issue, the error I get is:

Stopped because of unknown reason "null" at November 17, 2022 at 07:02:14 (runtime: 0.00 seconds)

The docs still show this as the right way to build actionable notifications, so it would be nice to understand why it’s not working.

Here’s the relevant portions of my automation yaml:

alias: Notify on Garage Door Left Open
trigger:
...
condition: []
action:
  - service: notify.notify
    data:
      title: Garage door open!
      data:
        actions:
          - action: "{{ action_close_door }}"
            title: Close Door
            icon: sfsymbols:door.garage.closed
        push:
          interruption-level: time-sensitive
  - wait_for_trigger:
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{ action_close_door }}"
    timeout:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
    continue_on_timeout: false
  - service: cover.close_cover
    data: {}
    target:
      entity_id: "cover.garage"
variables:
  action_close_door: "{{ 'CLOSE_DOOR_' ~ context.id }}"
mode: single

Variables should be defined under action
try something like this

alias: Notify on Garage Door Left Open
trigger:
...
condition: []
action:
  - variables:
      action_close_door: "{{ 'CLOSE_DOOR_' ~ context.id }}"
  - service: notify.notify
    data:
      title: Garage door open!
      data:
        actions:
          - action: "{{ action_close_door }}"
            title: Close Door
            icon: sfsymbols:door.garage.closed
        push:
          interruption-level: time-sensitive
  - wait_for_trigger:
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{ action_close_door }}"
    timeout:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
    continue_on_timeout: false
  - service: cover.close_cover
    data: {}
    target:
      entity_id: "cover.garage"
mode: single