Catching None-event after waiting for trigger

Hi,

I have a few automations that give me an actionable push message/notification to an Apple device. The idea is that when motion sensors don’t detect any activity in a room, but the lights are still on (or the TV is still on) that it gives an actionable notification. (action can be “Leave on” or “switch off” in a nutshell.

This all works fine except for one point.
Since I don’t want the automations to run forever I have a time-out and I want to send another message that the time-out has reached and that it didn’t do anything.

Technically and functionally I got that working, but I get the following error messages. I know why it fails, but I can’t figure out a way to prevent these error messages to appear.

2022-11-04 15:08:02.289 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'event' when rendering '{{ wait.trigger.event.data.action == action_uitzetten }}'
2022-11-04 15:08:02.291 WARNING (MainThread) [homeassistant.helpers.script] Error in 'choose[0]' evaluation: In 'template' condition: UndefinedError: 'None' has no attribute 'event'
2022-11-04 15:08:02.293 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'event' when rendering '{{ wait.trigger.event.data.action == action_aanlaten }}'
2022-11-04 15:08:02.294 WARNING (MainThread) [homeassistant.helpers.script] Error in 'choose[0]' evaluation: In 'template' condition: UndefinedError: 'None' has no attribute 'event'
2022-11-04 15:08:02.296 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'event' when rendering '{{ wait.trigger.event.data.action == "None" or wait.trigger == "none" }}'
2022-11-04 15:08:02.297 WARNING (MainThread) [homeassistant.helpers.script] Error in 'choose[0]' evaluation: In 'template' condition: UndefinedError: 'None' has no attribute 'event'
My Automation looks as follows:
alias: NAME lichten staan nog aan
description: Stuurt een bericht als de NAME d'r lichten nog aan staan.
trigger:
  - platform: state
    entity_id:
      - binary_sensor.beweging_NAME_presence
    to: "off"
    for:
      hours: 0
      minutes: 9
      seconds: 0
  - platform: state
    entity_id:
      - binary_sensor.aanwezig_NAME
    to: "off"
condition:
  - condition: state
    entity_id: binary_sensor.aanwezig_NAME
    state: "off"
  - condition: or
    conditions:
      - condition: state
        entity_id: light.NAME_kast_lampen
        state: "on"
      - condition: state
        entity_id: light.NAME_plafond_lampen
        state: "on"
  - condition: time
    after: "06:30:00"
    before: "20:45:00"
action:
  - variables:
      action_uitzetten: "{{ 'UITZETTEN_' ~ context.id|default('None') }}"
      action_aanlaten: "{{ 'AANLATEN_' ~ context.id|default('None') }}"
      action_tag: "{{ 'TAG_' ~ context.id|default('None') }}"
  - service: notify.mobile_app_iphone_van_MYPHONE
    data:
      message: De lichten in NAME d'r kamer staan nog aan en er is niemand.
      title: Lichten in NAME d'r kamer
      data:
        actions:
          - action: "{{ action_uitzetten }}"
            title: Licht uitzetten
          - action: "{{ action_aanlaten }}"
            title: Licht aanlaten
        tag: "{{ action_tag }}"
  - service: notify.mobile_app_iphone_van_HERPHPONE
    data:
      message: De lichten in NAME d'r kamer staan nog aan en er is niemand.
      title: Lichten in NAME d'r kamer
      data:
        actions:
          - action: "{{ action_uitzetten }}"
            title: Licht uitzetten
          - action: "{{ action_aanlaten }}"
            title: Licht aanlaten
        tag: "{{ action_tag }}"
  - wait_for_trigger:
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{ action_uitzetten }}"
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{ action_aanlaten }}"
    timeout:
      hours: 0
      minutes: 15
      seconds: 0
      milliseconds: 0
    continue_on_timeout: true
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ wait.trigger.event.data.action == action_uitzetten }}"
        sequence:
          - service: light.turn_off
            data:
              transition: 2
            target:
              device_id:
                - f4c40675b8XXXXXXXXXXXXXXXX16ed9b
                - e9739867e7XXXXXXXXXXXXXXXX6fe8d7
          - service: notify.mobile_app_iphone_van_MYPHONE
            data:
              message: clear_notification
              data:
                tag: "{{ action_tag }}"
          - service: notify.mobile_app_iphone_van_HERPHPONE
            data:
              message: clear_notification
              data:
                tag: "{{ action_tag }}"
      - conditions:
          - condition: template
            value_template: "{{ wait.trigger.event.data.action == action_aanlaten }}"
        sequence:
          - service: notify.mobile_app_iphone_van_MYPHONE
            data:
              message: clear_notification
              data:
                tag: "{{ action_tag }}"
          - service: notify.mobile_app_iphone_van_HERPHPONE
            data:
              message: clear_notification
              data:
                tag: "{{ action_tag }}"
      - conditions:
          - condition: template
            value_template: >-
              {{ wait.trigger.event.data.action == "None" or wait.trigger ==
              "none" }}
        sequence:
          - service: notify.mobile_app_iphone_van_MYPHONE
            data:
              message: Geen reactie - De lichten in NAME d'r kamer blijven aan
              title: Lichten in NAME d'r kamer
              data:
                tag: "{{ action_tag }}"
          - service: notify.mobile_app_iphone_van_HERPHPONE
            data:
              message: Geen reactie -  De lichten in NAME d'r kamer blijven aan
              title: Lichten in NAME d'r kamer
              data:
                tag: "{{ action_tag }}"
    default:
      - service: notify.mobile_app_iphone_van_MYPHONE
        data:
          message: Geen reactie - De lichten in NAME d'r kamer blijven aan
          title: Lichten in NAME d'r kamer
          data:
            tag: "{{ action_tag }}"
      - service: notify.mobile_app_iphone_van_HERPHPONE
        data:
          message: Geen reactie -  De lichten in NAME d'r kamer blijven aan
          title: Lichten in NAME d'r kamer
          data:
            tag: "{{ action_tag }}"
mode: single

Thanks up front!! :slight_smile:

Just figured I’d post my own workaround.
When the time-out is reached a variable is set in accordance to: https://www.home-assistant.io/docs/scripts/#wait-timeout

It should return with:
 wait:
   remaining: 0
   trigger: none

At the moment of writing however there is a bug around this. As it returns: trigger: null in stead of trigger: none. (I created a bug for this in Github)

So with this is mind I conjured up the following. I hope it is of help for someone :slight_smile:

My fix:
if:
  - condition: template
    value_template: "{{ wait.trigger != null }}"
then:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ wait.trigger.event.data.action == action_uitzetten }}"
        sequence:
          - service: light.turn_off
            data:
              transition: 2
            target:
              device_id:
                - d9XXXXXXXXXXXXXXXXXXXXXXXXX70
                - acXXXXXXXXXXXXXXXXXXXXXXXXX48
          - service: notify.gezin
            data:
              message: clear_notification
              data:
                tag: "{{ action_tag }}"
      - conditions:
          - condition: template
            value_template: "{{ wait.trigger.event.data.action == action_aanlaten }}"
        sequence:
          - service: notify.gezin
            data:
              message: clear_notification
              data:
                tag: "{{ action_tag }}"
else:
  - service: notify.gezin
    data:
      message: Geen reactie - De lichten in de woonkamer blijven aan
      title: Lichten in Woonkamer
      data:
        tag: "{{ action_tag }}"

I noticed the same thing about wait.trigger and so I tried the same thing that you did above. But it’s not working. I actually just rigged up a simple automation to test it since I was using it in a more complex setting. And sure enough, it passes the test with wait.trigger != null even though it seemingly should not. So, I tried this approach and it seems to be working …

if:
  - condition: template
    value_template: '{{ wait.trigger.idx is defined }}'
then:
  <actions here>
2 Likes