Help with variables in actionable notification triggered by automation and executed in script

Hi guys,

I am really struggling to get my automation running.
My plan is to have a trigger which is either a device-tracker entering a zone or a generic alternative (e.g. manually trigger the automation). The latter optionally induces the first actionable notification to ask which car shall be charged.

The second actionable notification triggers a sequence to open the charger-door, wait that the ev-charger is plugged in and then unlock the ev-charging station.

Both actionable notifications are defined in this script:

alias: Prepare Charging
sequence:
  - alias: Set up variables for the actions
    variables:
      action_open: "{{ 'OPEN_' ~ context.id }}"
      action_close: "{{ 'CLOSE_' ~ context.id }}"
      arrived_car: "{{ arrived_car }}"
      action_chargeKIRC: "{{ 'chargeKIRC_' ~ context.id }}"
      action_chargeCarol: "{{ 'chargeCAROL_' ~ context.id }}"
  - alias: Trigger-based or Generic Execution
    choose:
      - conditions: "{{arrived_car == 'generic' }}"
        sequence:
          - service: notify.mobile_app_rusty_s_iphone_2023
            alias: Ask which Car to charge
            data:
              message: Which Car to charge?
              data:
                actions:
                  - action: "{{ action_chargeKIRC }}"
                    title: KIRC
                  - action: "{{ action_chargeCarol }}"
                    title: CAROL
          - alias: Wait for a response
            wait_for_trigger:
              - platform: event
                event_type: mobile_app_notification_action
                event_data:
                  action: "{{ action_chargeKIRC }}"
              - platform: event
                event_type: mobile_app_notification_action
                event_data:
                  action: "{{ action_chargeCarol }}"
            timeout:
              hours: 0
              minutes: 0
              seconds: 30
              milliseconds: 0
          - alias: Perform the action
            choose:
              - conditions: "{{wait.trigger.event.data.action == action_chargeKIRC }}"
                sequence:
                  - variables:
                      charger_door: cover.kirc_charger_door
                      charger_sensor: binary_sensor.kirc_charger
              - conditions: "{{ wait.trigger.event.data.action == action_chargeCarol }}"
                sequence:
                  - variables:
                      charger_door: cover.carol_charger_door
                      charger_sensor: binary_sensor.carol_charger
      - conditions: " {{arrived_car != 'generic' }}"
        sequence: []
  - alias: Ask to prepare Charging
    service: notify.mobile_app_rusty_s_iphone_2023
    data:
      message: Prepare Charging?
      data:
        actions:
          - action: "{{ action_open }}"
            title: Yes, Prepare Charging.
          - action: "{{ action_close }}"
            title: No Charging.
  - alias: Wait for a response
    wait_for_trigger:
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{ action_open }}"
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{ action_close }}"
    timeout:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - alias: Perform the action
    choose:
      - conditions: "{{wait.trigger.event.data.action == action_open }}"
        sequence:
          - service: cover.open_cover
            target:
              entity_id: "{{ charger_door }}"
            data: {}
          - wait_template: "{{ states(charger_sensor) == 'on'}}"
            continue_on_timeout: true
            timeout: "60"
          - service: lock.unlock
            target:
              entity_id: lock.keba_p30_authentication
            data: {}
          - service: notify.mobile_app_rusty_s_iphone_2023
            data:
              message: Succesfully executed Automation.
      - conditions: "{{ wait.trigger.event.data.action == action_close }}"
        sequence:
          - service: notify.mobile_app_rusty_s_iphone_2023
            data:
              message: Automation terminated.
mode: single

That’s the automation:

alias: "Trigger Script: Prepare Charging"
description: ""
trigger:
  - alias: KIRC Arrival
    platform: zone
    entity_id: device_tracker.kirc
    zone: zone.home
    event: enter
    id: KIRC-Arrival
  - alias: CAROL Arrival
    platform: zone
    entity_id: device_tracker.carol
    zone: zone.home
    event: enter
    id: CAROL-Arrival
condition: []
action:
  - service: script.prepare_charging
    data_template:
      arrived_car: |
        {% if states('Trigger', 'entity_id') == 'device_tracker.carol' %}
          cover.carol_charger_door
        {% elif states('Trigger', 'entity_id') == 'device_tracker.kirc' %}
          cover.kirc_charger_door
        {% else %}
          generic
        {% endif %}
mode: single

Unfortunately, the script stops at that point where I want to use the conditional variables ‘charger_door’ .

Can you tell me, where my problem is?

Thanks in advance.

FYI, I edited your post to use proper backticks to format your code. this is a backtick `

You were using some other character that’s not a backtick.

1 Like
   {% if trigger.id == 'KIRC-Arrival' %}
     cover.kirc_charger_door
   {% elif trigger.id == 'CAROL-Arrival' %}
     cover.kirc_charger_door
   {% else %}
     generic
   {% endif %}
1 Like
alias: "Trigger Script: Prepare Charging"
trigger:
  - platform: zone
    entity_id:
      - device_tracker.kirc
      - device_tracker.carol
    zone: zone.home
    event: enter
condition: []
action:
  - service: script.prepare_charging
    data:
      arrived_car: "cover.{{ trigger.to_state.object_id }}_charger_door"
mode: single

I deliberately omitted the case of ‘generic’ because it’s unnecessary.

1 Like

Yes, but I wasn’t sure if he was going to add another trigger

´Thanks guys for helping me out. Appreciate it!
As Petro stated, I wanna keep the opportunity to trigger the script additionally with a generic trigger.
Moreover, within the script the variable: ‘charger_door’ doesn’t apply the value which was taken out of the actionable notification.

Can you guide me out of it?

Thanks you guys.

The automation you posted above can only be triggered by two specific device_trackers (kirc and carol). Until you add more triggers there’s no need to specify an ‘otherwise’ case.

That’s because all variable’s have a scope and that one’s scope is limited to where it was defined, namely within the second conditions of the choose in “Perform the action”. Outside of that, it’s undefined.

References:
Scope of variables

1 Like