Automation help needed - template variable warning

Hi
I need help to resolve a template variable warning:
* Template variable warning: 'action_wait' is undefined when rendering '{{action_wait}}'

I already added this line after the wait trigger but that didn’t resolve the issue:
| default('none') }}

I’m also not sure about clear_notification, is this the correct use?
when nobody is responding to the notification, it should remove/clear that message.

this is how my automation looks like:

any help is appreciated

alias: Garage
description: ""
trigger:
  - platform: state
    entity_id:
      - cover.garage_door_1
    to: open
    for:
      hours: 1
      minutes: 0
      seconds: 0
condition: []
action:
  - variables:
      action_close: "{{ \"GARAGE_CLOSE\" ~ context.id }}"
      action_wait: "{{ \"GARAGE_WAIT\" ~ context.id }}"
      action_do_nothing: "{{ \"DO_NOTHING\" ~ context.id }}"
  - service: notify.family
    metadata: {}
    data:
      message: Garage is open for a hour!
      data:
        actions:
          - action: "{{action_close}}"
            title: Close it
            tag: garageclose
          - action: "{{action_wait}}"
            title: Wait a hour
            tag: garagewait
          - action: "{{action_do_nothing}}"
            title: Do nothing
            tag: donothing
      title: Garage
  - wait_for_trigger:
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{action_close}}"
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{action_wait}}"
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{action_do_nothing}}"
        context: {}
    timeout:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{ wait.trigger.event.data.action == action_close | default('none') }}
        sequence:
          - service: cover.close_cover
            metadata: {}
            data: {}
            target:
              entity_id: cover.garage_door_1
          - service: notify.family
            metadata: {}
            data:
              message: clear_notification
              data:
                tag: garageclose
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{ wait.trigger.event.data.action == action_wait | default('none') }}
        sequence:
          - service: cover.close_cover
            metadata: {}
            data: {}
            target:
              entity_id: cover.garage_door_1
          - service: notify.family
            metadata: {}
            data:
              message: clear_notification
              data:
                tag: garagewait
  - service: notify.family
    metadata: {}
    data:
      title: Garage
      message: |
        {% if wait.trigger.event.data.action == action_close %}
          Garage will close now
        {% elif wait.trigger.event.data.action == action_wait %}
          Garage will close in a hour
        {% else %}
          No action taken, garage will stay open
        {% endif %}
mode: single

There are a few issues:

  1. tag is not a valid key for actions.
  2. The initial notification is missing its tag ID. In order for the clearing notification to work it must have the same tag ID as the initial notification.
  3. All the options should be under the same Choose action and a default option should be added to clear the initial notification for the “do nothing” and timeout cases.
  4. The “action_wait” sequence doesn’t currently wait or start a timer…
alias: Garage
description: ""
trigger:
  - platform: state
    entity_id:
      - cover.garage_door_1
    to: open
    for:
      hours: 1
      minutes: 0
      seconds: 0
condition: []
action:
  - variables:
      action_close: "{{  'GARAGE_CLOSE' ~ context.id }}"
      action_wait: "{{ 'GARAGE_WAIT' ~ context.id }}"
      action_do_nothing: "{{ 'DO_NOTHING' ~ context.id }}"
  - service: notify.family
    metadata: {}
    data:
      message: Garage is open for a hour!
      data:
        tag: garage_notifications
        actions:
          - action: "{{action_close}}"
            title: Close it
          - action: "{{action_wait}}"
            title: Wait a hour
          - action: "{{action_do_nothing}}"
            title: Do nothing
      title: Garage
  - wait_for_trigger:
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{action_close}}"
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{action_wait}}"
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{action_do_nothing}}"
    timeout:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - variables:
      w_action: "{{ 'timeout' if wait.trigger.event is undefined else wait.trigger.event.data.action }}"
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ w_action == action_close }}"
        sequence:
          - service: cover.close_cover
            metadata: {}
            data: {}
            target:
              entity_id: cover.garage_door_1
      - conditions:
          - condition: template
            value_template: "{{ w_action == action_wait }}"
        sequence:
          - service: cover.close_cover
            metadata: {}
            data: {}
            target:
              entity_id: cover.garage_door_1
  - service: notify.family
    metadata: {}
    data:
      message: clear_notification
      data:
        tag: garage_notifications
  - delay: 1
  - service: notify.family
    metadata: {}
    data:
      title: Garage
      message: |
        {% if w_action == action_close %}
          Garage will close now
        {% elif w_action == action_wait %}
          Garage will close in a hour
        {% else %}
          No action taken, garage will stay open
        {% endif %}
mode: single

Note: This automation will not wait when the “Wait an hour” action is chosen. You need to decide which method you want to use. I would probably use a Timer helper, but there are a number of ways to do it.

EDIT:

  • Fixed tag ID issue.
  • Removed tag from action definitions.
  • Fixed variable definition.
1 Like

Thanks for thinking along! hm I didn’t know it had so many issues in it. A question:

  1. Is there a reason why you added the delay two times? Before and after the notify step.
  2. you define the do_nothing here, but does this needs to be action_do_nothing instead? to define the variable
      w_action: "{{ wait.trigger.event.data.action | default('do_nothing', 1) }}"

so like this

      w_action: "{{ wait.trigger.event.data.action | default('action_do_nothing', 1) }}"

The first one can probably be left out, and you can test reducing or removing the second.

There isn’t a condition that explicitly requires the value of action_do_nothing, the default is just providing a value for the case of timeout when there isn’t a trigger… However, in reviewing it I realize that it will not work properly. I have updated the post above.

1 Like

Hi @Didgeridrew

I’ve got this warning multiple times a day. I didn’t changed the automation as mentioned here (the latest).

Template variable warning: 'action_close' is undefined when rendering '{{action_close}}'
strange because the variable is stored in the actions section and used afterwards right?