Wait_templates within automatons

Hi all. New to the forum and to the whole platform, but already impressed by some of the community answers and examples. I haven’t, however, found one to address my current little project:

I would like an automation to turn on a light with an offset from sunset. That bit is fine. In my example it’s 2 hours prior.
Then, within this automation, I would like to try and use a wait_template to, well, wait until a fixed time, eg. 11PM. Sample code:

- id: '9827657219565'
  alias: Timed Light Testing
  trigger:
  - platform: sun
    event: sunset
    offset: -02:00:00
  action:
    - service: homeassistant.turn_on
      entity_id:
      - switch.nightlights_entryway
#   Some sort of "Wait until 23:00 instruction here"
    - service: homeassistant.turn_off
      entity_id:
      - switch.nightlights_entryway

For the commented line, I have tried referencing the time_date and the worldclock sensors. I just can’t seem to get the config to check out. This is an example from another forum post that I’ve not had any luck with:

- wait_template: "{{ states.sensor.time.state == '23:00' }}"

Any pointers would be appreciated, cheers. Hass.io 0.86.3 as an image on a Pi.

Why do you need a wait template to do something at a fixed time? This is just a simple automation to do that. You could still combine it into one automation if that is your goal…

You can try something like this: insert a random delay before executing your actions:
- delay: '00:{{ (range(0, 5)|random|int) }}:{{ (range(5, 55)|random|int) }}'

Sorry, I read too fast.
As far as I know you need two automations with yaml: one for turning on and another one for turning off

@DavidFW1960 is right, you’d simply have to add a condition in the action part and evaluate that for true, to continue with the next service.

having said that, in the docs there’s no conditional time template for an ‘at 2300’

but this will probably work:

- condition: template
  value_template: >
    {{states('sensor.time') == '23:00' }} 

ive tested it locally and saw it True. :wink:

try:

- id: '9827657219565'
  alias: Timed Light Testing
  trigger:
    platform: sun
    event: sunset
    offset: -02:00:00
  action:
    - service: switch.turn_on
      entity_id: switch.nightlights_entryway
    - condition: template
      value_template: >
        {{states('sensor.time') == '23:00' }} 
    - service: switch.turn_off
      entity_id: switch.nightlights_entryway

but this will fail and simply stop the automation. Enter your wait template:

- id: '9827657219565'
  alias: Timed Light Testing
  trigger:
    platform: sun
    event: sunset
    offset: -02:00:00
  action:
    - service: switch.turn_on
      entity_id: switch.nightlights_entryway
    - wait_template: >
        {{states('sensor.time') == '23:00' }} 
    - service: switch.turn_off
      entity_id: switch.nightlights_entryway

btw please check the correct spacings in Yaml, its very critical, and probably why your config check didn’t work out.

1 Like

Hmm, cheers, I was hoping to avoid that but it’s how I’m currently doing it. The thing is, I’ve heard that scripts are essentially the same as Actions, and I’ve had a bit of success there with a “wait until state change”. Maybe as an interim step I will try making a script to handle the waiting, then call that. Seems sorta clumsy.

I don’t want to need separate “on” and “off” actions for things that usually follow fixed patterns and aren’t interrelated to other actions, just trying to avoid Automation Sprawl really :slight_smile:

Ha, You don’t say!

Now THAT is looking really good! The “>” may be the only difference between that and some of my earlier attempts. Hmm.
Yes, I’m completely new to YAML and have quickly picked up on the importance of correct indentations and formatting. Not having a history with it is a challenge, and a lot of the examples I’ve seen for things are from older versions of HA or are formatted subtly differently to one another, so I’m still piecing together any sort of understanding at all.
With a dummy trigger:time instead of a sunset, your example is behaving perfectly. Thanks for the time and brainspace!

You need to fix the typo for the service otherwise someone somewhere down the track…

1 Like

Sorry…consider it done