Trigger automation from Script with variable automation name

I followed a few guides here to get Persistent notification with buttons for delay on my phone.
I’m running into a weird issue that I’m not sure is syntax, or just impossible to do with the scripts.
I have a thermostat running in my garage, i want it to notify (persistent) with buttons for turn off, delay 20min, All ok, when the compressor has been running over 20 minutes.
I have a working solution with a dedicated script. but would like the script to be automation agnostic, in case I call it from a different automation. Currently I can only get it to work if I call the “automation.trigger” with hardcoded entity_id.
Any time I do the same, but with the entity_id variable, the script fails.

EDIT: I was an idiot and didn’t test the entire script. just selected “run” on the automation.trigger service in gui, which succeeded.
When running the entire script, even the hardcoded automation.trigger does not succeed…

My automation is this:

alias: GarageThermostatTest
description: ""
trigger:
  - platform: device
    type: turned_on
    device_id: 7a315795d9c9edb21c23001289cc62cf
    entity_id: 947a731813fbc75a590dfb2f159bf729
    domain: switch
    for:
      hours: 0
      minutes: 20
      seconds: 0
condition: []
action:
  - service: notify.mobile_app_rusty_phone
    metadata: {}
    data:
      title: Thermostat Notification
      message: Garage AC on more than 20 Minutes
      data:
        actions:
          - action: TURNOFF
            title: Turn AC OFF
          - action: SNOOZE_20_MIN
            title: Ignore 20 Min
          - action: IGNORE
            title: All OK
        persistent: true
        tag: garageTherm
  - wait_for_trigger:
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: TURNOFF
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: SNOOZE_20_MIN
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: IGNORE
    continue_on_timeout: true
    timeout: "0:05"
  - choose:
      - conditions:
          - condition: template
            value_template: "{{wait.trigger.event.data.action == \"TURNOFF\"}}"
            enabled: true
        sequence:
          - service: climate.set_hvac_mode
            metadata: {}
            data:
              hvac_mode: "off"
            target:
              area_id: garage
          - service: notify.mobile_app_rusty_phone
            data:
              message: clear_notification
              data:
                tag: garageTherm
      - conditions:
          - condition: template
            value_template: "{{wait.remaining == 0}}"
            enabled: true
        sequence:
          - service: climate.set_hvac_mode
            metadata: {}
            data:
              hvac_mode: "off"
            target:
              area_id: garage
          - service: notify.mobile_app_rusty_phone
            data:
              message: clear_notification
              data:
                tag: garageTherm
      - conditions:
          - condition: template
            value_template: "{{wait.trigger.event.data.action == \"SNOOZE_20_MIN\"}}"
            enabled: true
        sequence:
          - service: script.snooze_notifications_retrigger
            data:
              snooze_time_mins: 20
              automation_name: automation.garagethermostattest
      - conditions:
          - condition: template
            value_template: "{{ wait.trigger.event.data.action == \"IGNORE\"}}"
            enabled: true
        sequence:
          - service: notify.mobile_app_rusty_phone
            data:
              message: clear_notification
              data:
                tag: garageTherm
mode: restart

My Script is this: (this one fails and doesn’t retrigger the automation, but does re-enable it)

alias: Snooze Notifications Retrigger
sequence:
  - service: automation.turn_off
    target:
      entity_id: "{{ automation_name }}"
    data:
      stop_actions: false
  - delay:
      minutes: "{{ snooze_time_mins }}"
  - service: automation.turn_on
    target:
      entity_id: "{{ automation_name }}"
  - delay:
      seconds: 1
  - service: automation.trigger
    metadata: {}
    data:
      skip_condition: true
    target:
      entity_id: "{{ automation_name }}"
mode: single
description: snooze Notifications, then fire again after snooze

This one succeeds, but is obviously not automation agnostic.

alias: Snooze Notifications Retrigger
sequence:
  - service: automation.turn_off
    target:
      entity_id: "{{ automation_name }}"
    data:
      stop_actions: false
  - delay:
      minutes: "{{ snooze_time_mins }}"
  - service: automation.turn_on
    target:
      entity_id: "{{ automation_name }}"
  - delay:
      seconds: 1
  - service: automation.trigger
    metadata: {}
    data:
      skip_condition: true
    target:
      entity_id: automation.garagethermostattest
mode: single
description: snooze Notifications, then fire again after snooze

So the real hangup is, Why does the automation.turn_on succeed with variable as entity_id, While the automation.trigger fails… (unless somehow I’ve got bad syntax. which is very possible) (See edit before code)

  1. Don’t use device triggers, use entity state triggers.
  2. Don’t design your logic to involve turning automations on and off — that’s not a good idea. Use conditions within the automation to prevent the action from running.

Explain a little more succinctly what you’re trying to achieve and I’m sure we can help.

What I want:
a notification on my phone when the Garage AC runs for 20 minutes solid or more (in case the custom esphome thermostat screws up).
this notification should then give me 3 buttons, “Turn AC OFF”, “Ignore 20 Min” , “All OK”. If no button is pressed in 5 minutes then default to turning the thermostat off and notification cleared replaced with sticky notification that AC was turned off.
When AC OFF, just turn thermostat off, When ALL OK, then do nothing, when Ignore 20 Min then it should reset and wait another 20 min then notify again (essentially loop the entire automation as if triggered after a 20 min wait)

I can get most my desired functionality with a small rewrite of the automation.
The only thing I haven’t figured out how to do is “loop” indefinitely when the “ignore 20min” is selected.

As for the entity state trigger, that’s where I can find from the ESPHome Thermostat whether the control for the compressor is on. not just what state the thermostat wants to be in. thus using it as the “safety” warning.

Rewritten automation without loop here:

alias: GarageThermostatNotification
description: ""
trigger:
  - platform: device
    type: turned_on
    device_id: 7a315795d9c9edb21c23001289cc62cf
    entity_id: 947a731813fbc75a590dfb2f159bf729
    domain: switch
    for:
      hours: 0
      minutes: 20
      seconds: 0
condition: []
action:
  - service: notify.mobile_app_rusty_phone
    metadata: {}
    data:
      title: Thermostat Notification
      message: Garage AC on more than 20 Minutes
      data:
        actions:
          - action: TURNOFF
            title: Turn AC OFF
          - action: SNOOZE_20_MIN
            title: Ignore 20 Min
          - action: IGNORE
            title: All OK
        persistent: true
        tag: garageTherm
  - wait_for_trigger:
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: TURNOFF
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: SNOOZE_20_MIN
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: IGNORE
    continue_on_timeout: true
    timeout: "0:05"
  - choose:
      - conditions:
          - condition: template
            value_template: "{{wait.trigger.event.data.action == \"TURNOFF\"}}"
            enabled: true
        sequence:
          - service: climate.set_hvac_mode
            metadata: {}
            data:
              hvac_mode: "off"
            target:
              area_id: garage
      - conditions:
          - condition: template
            value_template: "{{wait.remaining == 0}}"
            enabled: true
        sequence:
          - service: climate.set_hvac_mode
            metadata: {}
            data:
              hvac_mode: "off"
            target:
              area_id: garage
          - service: notify.mobile_app_rusty_phone
            data:
              message: clear_notification
              data:
                tag: garageTherm
          - service: notify.mobile_app_rusty_phone
            metadata: {}
            data:
              title: Thermostat Notification
              message: Garage AC Forced OFF
              data:
                sticky: true
      - conditions:
          - condition: template
            value_template: "{{wait.trigger.event.data.action == \"SNOOZE_20_MIN\"}}"
            enabled: true
        sequence:
          - delay:
              hours: 0
              minutes: 20
              seconds: 0
              milliseconds: 0
          - service: climate.set_hvac_mode
            metadata: {}
            data:
              hvac_mode: "off"
            target:
              area_id: garage
      - conditions:
          - condition: template
            value_template: "{{ wait.trigger.event.data.action == \"IGNORE\"}}"
            enabled: true
        sequence: null
mode: restart

Set up a Timer helper. Start the timer when the “ignore 20min” action is selected. Add a second trigger to the automation for when the Timer finishes. You will probably want to mirror your original switch trigger as a condition to recheck its state when the timer-based trigger is what fires.

1 Like

Thank you!!
That did it.

Obviously I’m quite new at this, especially yaml.
And the documentation isn’t very clear on yaml syntax whatsoever.

When I started out with HA (when there wasn’t a UI Automation Editor), I found this very helpful: YAML for Non-programmers

1 Like