Wait on condition automation or stop if trigger is re-set

Hello,

I ran into a situation where I would like for an automation to wait for a certain time (condition) to trigger. To further explain, I have an automation that goes off when there is no motion inside the house with a time delay of 30 mins. At the same time, I would like for this automation to only go off if the time is between 8am and 9pm.

What happens is that sometimes, people leave the house earlier than 8 am and the automation triggers but the condition is not met and it never runs. So, I want to make sure that if the automation triggers before the given time, it waits until the condition is met, at the same time, if the trigger is reset the automation should be stopped.

I came up with this template for the wait on template trigger, but I believe is not working properly nor it will reset if the trigger is reset (people are inside the house)

{% set b = now()|as_local %}
{% if b >= today_at("08:00") and b <= today_at("21:00") %} on
{% endif %}

Here is the full automation

alias: HA_Auto_Idle_Set_AC_Off
description: >-
  This automation will turn off the lights and A/C when tenants are not in the
  house
trigger:
  - platform: state
    entity_id: group.motion_sensors
    from: 'on'
    to: 'off'
    for:
      hours: 0
      minutes: 30
      seconds: 0
condition:
  - condition: state
    entity_id: group.rental_control_calendars
    state: 'on'
  - condition: state
    entity_id: group.backyard_motion_sensors
    state: 'off'
    for:
      hours: 0
      minutes: 20
      seconds: 0
  - condition: state
    entity_id: group.motion_sensors
    state: 'off'
    for:
      hours: 0
      minutes: 30
      seconds: 0
action:
  - wait_template: |-
      {% set b = now()|as_local %}
      {% if b >= today_at("08:00") and b <= today_at("21:00") %} on
      {% endif %}
    continue_on_timeout: false
  - service: homeassistant.turn_on
    data: {}
    target:
      entity_id: input_boolean.home_away
  - service: climate.turn_off
    data: {}
    target:
      device_id: 2f04d7d0379ee7a2a45106f222365230
  - service: homeassistant.turn_off
    data: {}
    target:
      entity_id: group.house_lights
  - service: flo.set_away_mode
    data: {}
    target:
      entity_id: switch.shutoff_valve
mode: single

Thanks in advance.

I guess I can do something like this where I place a condition below the “wait for template” action.

  - condition: state
    entity_id: group.motion_sensors
    for:
      hours: 0
      minutes: 30
      seconds: 0
    state: 'off'

Instead of relying on a wait that can lock up the automation for hours, add a time trigger and condition…

alias: HA_Auto_Idle_Set_AC_Off
description: >-
  This automation will turn off the lights and A/C when tenants are not in the
  house
trigger:
  - platform: state
    entity_id: group.motion_sensors
    from: 'on'
    to: 'off'
    for:
      hours: 0
      minutes: 30
      seconds: 0
  - platform: time
    at: "08:01:00"
condition:
  - condition: state
    entity_id: group.rental_control_calendars
    state: 'on'
  - condition: state
    entity_id: group.backyard_motion_sensors
    state: 'off'
    for:
      hours: 0
      minutes: 20
      seconds: 0
  - condition: state
    entity_id: group.motion_sensors
    state: 'off'
    for:
      hours: 0
      minutes: 30
      seconds: 0
  - condition: time
    after: "08:00:00"
    before: "21:00:00" 
action:
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.home_away
  - service: climate.turn_off
    data: {}
    target:
      device_id: 2f04d7d0379ee7a2a45106f222365230
  - service: homeassistant.turn_off
    data: {}
    target:
      entity_id: group.house_lights
  - service: flo.set_away_mode
    data: {}
    target:
      entity_id: switch.shutoff_valve
mode: single
1 Like

@Didgeridrew

I like that a lot. Idk why I didn’t think of that before.

Thank you!

btw,

This is the third time you help me solve my problem. Thank you.

I was wondering if you can recommend a good YAML training for noobies like me. I would like to learn YAML.

Once again Thank you!

1 Like

Also, if you do find that you need to use a wait but also need to be able to re-trigger the automation you will need to switch the automation mode to restart instead of single. Using restart allows you be break out of waits and loops.

Will do thank you