Ad to sunset a value from input datetime in automation

good evening,
I would like to close my shutters at sunset but adding a delay offset.
The offset I would like to be able to modify without having to change the code so I would like to use an input datetime.
The mechanism is clear to me but I can’t concatenate and add the value in input datetime to sunset

I’d suggest doing something like this:

- trigger:
    platform: sun
    event: sunset
  action:
    # Delay configured amount of time.
    - delay: "{{ state_attr('input_datetime.xxx', 'timestamp') }}"
    # Close shutters.
    ...

The State Trigger now accepts templates in its for option. I haven’t tested it but it’s theoretically possible to achieve your goal like this:

- alias: 'Close shutters shortly after sunset'  
  trigger:
    platform: state
    entity_id: sun.sun
    to: 'below_horizon'
    for:
      minutes: "{{ states('input_number.offset_minutes') | int }}"
  action:
    service: cover.close_cover
    # etc

The automation will trigger when the state of sun.sun changes from above_horizon to below_horizon and remains in that state for the amount of minutes set by the entity input_number.offset_minutes. After the specified number of minutes has elapsed, the automation’s action will be executed.

1 Like

Wow. Since I implemented that I guess I should have thought of it! :smile: :blush: Good call!

2 Likes