Running automation periodically until conditions are met

Hi guys,

I’m trying to setup an automation that will change the light colour once the sun rises. The lights could be on or off when the sun rises, so I want the automation to run either when the lights are already on or when I turn them on and it’s past sunrise.

I’m having trouble setting up a trigger that will account for this. I’ve tried a few different ways of accomplishing this but I can’t seem to find one that will work lol.

I’m new to home assistant so I could easily be missing something but based on what I know the automation below should be running every 5 minutes, and then change the light colour if the device is on and it’s past sunrise.

Does anyone have any idea what I’m doing wrong.?

alias: day time lights (living room)
description: ''
trigger:
  - platform: time_pattern
    minutes: /5
condition:
  - condition: device
    type: is_on
    device_id: f3bc4b2bb0dd6b8cc80a9bf3c7dde656
    entity_id: light.ebcb9aaec7e0681113lpgh
    domain: light
  - condition: numeric_state
    entity_id: sun.sun
    value_template: '"{{ state_attr(''sun.sun'', ''elevation'') }}"'
    above: '0'
action:
  - scene: scene.daytime_lights
mode: single

Confirm that you don’t want the lights to turn on if they’re off? So what we’re saying is that when the sun goes above the horizon, check if the lights are on. If so, proceed. If not, wait for the lights to be turned on THEN proceed?

If so then that’s not the logic you’ve applied in your automation, it should be

alias: day time lights (living room)
trigger:
  platform: state
  entity_id: sun.sun
  to: 'above_horizon' 
action:
  - wait_template: "{{ is_state('light.ebcb9aaec7e0681113lpgh', 'on') }}"
  - service: homeassistant.turn_on 
    entity_id: scene.daytime_lights

wow, that’s very different than what I had haha. The logic you described is correct. I’ll give that a go and report back.

1 Like

Getting the logic right is the key to it, then the code is easy :wink:

In this case the trigger (the thing that says ‘I need to do something’) is the change of state of the sun, not every 5 minutes.

Then what do we want to do when the sun rises - wait for the light to be on, and apply the scene.

Sometimes the battle is simplifying the logic down, sometimes its worth getting a pencil and paper and drawing a flow chart, then seeing if you can simplify the flow chart any, and then build the automation based on the final flow chart.

Anyway, let me know how you get on :+1:

Initially my logic was a bit simpler but when it didn’t work I moved on to this. I didn’t realize you could set a wait function in the actions, so that’s super useful to know. Thanks for the tips!

I just made the changes in the automation page but when I go back and looks at the YAML it seems to be automatically changing some of the quotes.

Below is the YAML that it reverts to automatically one I save the YAML. it seems to drop the single quotes around the ‘below_horizon’ text, the double quotes in the wait_template are changed to single quotes, and there are two single quotes placed around light.ebb3786a62e94e1459oepy and on.

Are those changes a problem? If so, do I need to make the changes in the automations.yaml file to prevent them?

- id: '1605825033036'
  alias: night time lights (bedroom) (Duplicate)
  description: ''
  trigger:
  - platform: state
    entity_id: sun.sun
    to: below_horizon
  condition: []
  action:
  - wait_template: '{{ is_state(''light.ebb3786a62e94e1459oepy'', ''on'') }}'
  - service: homeassistant.turn_on
    data: {}
    entity_id: scene.night_time_lights
  mode: single

As an update, I’ve tried with the code below but added a trigger for time to test. The automation is running but it does not honour the wait_template.

alias: a time testing
description: ''
trigger:
  - platform: state
    entity_id: sun.sun
    to: above_horizon
  - platform: time
    at: '21:09:00'
condition: []
action:
  - wait_template: '{{ is_state(''light.ebcb9aaec7e0681113lpgh'', ''on'') }}'
  - service: homeassistant.turn_on
    entity_id: scene.daytime_lights
mode: single

editing my additional update lol:

It looks like it wasn’t an issue with the wait template, it’s an issue with the tuya device state not changing when it should. It seems to be always set to on, which meant the wait template was always bypassed. Anyways, i changed the wait template to reference a different switch and it’s working now.

1 Like