Using wait_template with time. Will this rule work?

Will this use of wait_template work? I’m trying to create a 1x catch all rule that fires on sunset, then checks if it’s after 17:45. If it’s not after 17:45 then the rule waits until it is before finally firing the actions.

Automation rule

- id: random_lights_on_weekdays
  alias: 'Random Lights On - Weekdays'
  trigger:
    - platform: sun
      event: sunset
      offset: "-00:45:00"
  condition:
  - condition: time
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
  action:
    - wait_template: "{{ sensor.time == '17:45' }}" 
    - delay: '00:{{ (range(0, 15) | random) }}:00'
    - service: homeassistant.turn_on
      entity_id: group.living_room_lights_card

Extract from my sensors.yaml

- platform: time_date
  display_options:
    - 'time'
    - 'date'
    - 'date_time'
    - 'time_date'
    - 'time_utc'
    - 'beat'

I think you could achieve this more simply if you had two triggers and two conditions. The two triggers would be sunset offset and time, and the two conditions would be the sunset offset and time. That way it will only run once both conditions are met, regardless of which comes first.

I always thought all conditions of a trigger had to be true in order for the rule to fire.

But as noted in post #2 in this thread, matching any trigger condition will cause a rule to fire. Then any additional filtering could be performed with conditions.

For clarity here’s the final rule:

  trigger:
    - platform: sun
      event: sunset
      offset: '-00:45:00'
    - platform: time
      at: '17:45:00'
  condition:
    - condition: time
      after: '17:45:00'
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
    - condition: sun
      after: sunset
      after_offset: '-00:45:00'
  action:
    - delay: '00:{{ (range(0, 15) | random) }}:00'
    - service: homeassistant.turn_on
      entity_id: group.lights_card

After a few days of testing this rule still doesn’t appear to be working as expected.

The lights are switching on at close to sunset which instead of after 17:45. Its as though the rule conditions of after 17:45 are just being ignored.

What’s the best way to evaluate the rule?

EDIT: You need to use an “and” condition as detailed in the docs.

See below for an additional recommendation regarding this automation…

In your conditions, I would “offset” them by one minute from your trigger. That is, make your condition after: '17:44:00' and after_offset: '-0:46:00'

The reason I recommend this is because say 17:45 is after sunset. Once the automation fires at 17:45, it will fail the condition, because it’s at 17:45, not after 17:45. I’m not sure if this is how it would behave in practice, but it’s my best guess.

Thanks for advice. I always thought that conditions all had to evaluate to being true in order for the rule to perform it’s actions. So I’ve updated the rule and will report back if it works as expected.

  trigger:
    - platform: sun
      event: sunset
      offset: '-00:45:00'
    - platform: time
      at: '17:45:00'
  condition:
    condition: and
    conditions:
    - condition: time
      after: '17:44:00' #offset by 1 min from trigger just in case
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
    - condition: sun
      after: sunset
      after_offset: '-00:46:00' #offset by 1 min from trigger just in case
  action:
    - delay: '00:{{ (range(0, 15) | random) }}:00'
    - service: homeassistant.turn_on
      entity_id: group.living_room_lights_card
2 Likes