Automation trigger at sunset, but latest at 20:00

I tested your suggestion and it doesn’t work because the moment it turns itself off, that’s the end of everything. The automation does not proceed to execute the delay because it just turned itself off.

I used this to test it:

- alias: control self
  trigger:
  - platform: state
    entity_id: input_boolean.test
    to: 'on'
  action:
  - service: notify.persistent_notification
    data:
      title: Control Self
      message: Turn self off
  - service: automation.turn_off
    target:
      entity_id: automation.control_self
  - delay: '00:00:15'
  - service: automation.turn_on
    target:
      entity_id: automation.control_self
  - service: notify.persistent_notification
    data:
      title: Control Self
      message: Turn self on

It gets as far as sending the first notification and turning itself off.

Here’s another way. You needn’t turn off/on the automation so there’s no risk of restarting HA midway.

trigger:
  - platform: sun
    event: sunset
  - platform: time
    at: '20:00:00'
action:
- choose:
  - conditions:
    #If the next sunset is before 20:00
    - condition: template
      value_template: "{{state_attr('sun.sun', 'next_setting')[11:13]|int <= 19}}"
    sequence:
    - your actions here
  #else (if the next sunset is after 20:00)
  default:
  - wait_template: "{{is_state('sensor.time', '20:00')}}"
  - your actions here

I used to do that in my early stages of HA like a year ago and it did work, I’m 100% sure about that… that’s strange

Try the simple automation I posted. Perhaps my test procedure is flawed but it seems fairly straightforward (tested with 2021.6.0).

What you are saying is (and I got that already the first sight) that I should’ve written “I only want the automation to execute its actions just once and not twice after each other by both triggers”* instead of “I only want the automation to run once”. Apologies for this vague articulation, but I think almost everyone understood what I was after…

alias: sunset example
trigger:
  - platform: sun
    event: sunset
  - platform: time
    at: '20:00:00'
condition: "{{as_local(states.automation.sunset_example.attributes.last_triggered).day != now().day }}"
action:
 ... etc ..

The Template Condition simply checks if the last_triggered day is not the current day. If false it means it already executed today and it doesn’t proceed to execute it again. In other words, only one of the two triggers serves to execute the action on any given day. If sunset comes before 20:00, it will execute the action and when it’s 20:00 it will not execute the action again. Everything gets reset when the date changes the next day.

1 Like

To all: koying provided a solution what I am certain will work and I’ve already set up the automation based on that. Will report back how it will work out.

this looks cool too @123 ! Many thanks!

I, for my part perfer KODI ;). I fully agree on the for life part though :smiley:

Btw, nice to see you around @koying, hope you are well. And there are a couple more team mates in here, like @DarrenHill or @tadly (he joined after you guys retired, so you likely don’t know him).

PS: Sorry Olympia for hijacking your thread

1 Like