Update Time to turn off lights

First of all, thank you to all who have put and continue to put the effort into this project!

My goal is to have a light turn off after sunset based on an offset that is set via an input field.

I have set up an automation that turns off the light with the trigger of sunset + offset and this works. I didn’t see a way to modify the offset but I did see that I can provide the time to turn off via a helper so I created a datetime input helper and a drop down helper to select the offset.

I created a script to change the datetime helper to = sunset + new offset:

set dt_off_light:
  alias: Light off time 1
  sequence:
  - service: input_datetime.set_datetime
    entity_id: input_datetime.light_off_time
    data:
      datetime: >-
        "{% set offset = strptime(states('input_select.light_off_offset')+':00:00','%H:%M:%S') %}"
        "{% set offset = as_timestamp(strptime(offset, '%H:%M:%S')) %}"
        "{% set sunset = as_timestamp(states.sun.sun.attributes.next_setting) %}"
        "{{ (sunset + offset) | timestamp_custom('%H:%M') }}"

I also tried:

set dt_off_light:
  alias: light off time
  sequence:
  - service: input_datetime.set_datetime
    entity_id: input_datetime.light_off_time
    data:
      datetime: "{{((as_timestamp(states.sun.sun.attributes.next_setting)) + (strptime(states('input_select.light_off_offset')+':00:00','%H:%M:%S') | as_timestamp(strptime(offset, '%H:%M:%S')))) | timestamp_custom('%H:%M') }}"

neither works. When I run the script via an automation I get:

So I think I have at least 2 issues:

  1. The changed value sunset + new offset is not what I’m expecting, I think it’s not in the right time zone

  2. When I use the value for next_setting in the script The value is not stored. If I change it to now() the value stored in the datetime helper is changed when I change the selected item in the dropdown helper

Thank you again for all the support offered to those of us just starting out on this journey!

I think I solved it. So far the input_datetime.light_off_time is changing the way I want it to. When I change the value of input_select.light_off_offset, the value is correct based on the offset.

     service: input_datetime.set_datetime
target:
  entity_id: input_datetime.light_off_time
data:
  time: "{{(((states('input_select.light_off_offset') | int) * 3600) + (as_timestamp(states.sun.sun.attributes.next_setting) | int)) | timestamp_custom('%H:%M')}}"