Sun trigger automation with template offset

Hi,

I have a template that returns number which means offset hours and I want to use it in Sun trigger automation. Is possible to use a template as offset?
I have try with this but does not work.
{{(states('sensor.templatesunset_offset') | float * 3600) | timestamp_custom("%H:%M:%S", 0) }}
With regards,

Sun trigger does not accept templates, you will need to use a Template trigger.

trigger:
  - platform: template
    value_template: |
      {{ now() >= state_attr('sun.sun', 'next_rising') | as_datetime + 
      timedelta(hours= states('sensor.templatesunset_offset') | float(0) ) }}

Thanks for your reply. I think that this is not going to work for me because my offset is for sunset and I think that when sunset is finished the 'next_setting' attribute will be the sunset of tomorrow.

With regards,

Sorry, I should have noticed the “sunset” in the sensor’s ID… For that situation there are a couple options.

  1. Ignore the couple minutes difference in sunset time from one day to the next, and alter the template to compensate by 1 day:
trigger:
  - platform: template
    value_template: |
      {{ now() >= state_attr('sun.sun', 'next_setting') | as_datetime - timedelta(days=1) + 
      timedelta(hours= states('sensor.templatesunset_offset') | float(0) ) }}
  1. Install the Sun2 custom integration. It’s sensors are set up a bit differently, so you have access to the values for today, yesterday, and tomorrow as attributes:
trigger:
  - platform: template
    value_template: |
      {{ now() >= state_attr('sensor.sunset', 'today') + 
      timedelta(hours= states('sensor.templatesunset_offset') | float(0) ) }}
  1. Create your own stable “today’s sunset” sensor using a trigger-based template sensor that triggers at midnight and takes the value of next_setting.

I have found another option. Use the last_changed attribute of next_setting.

{{ now() >= states.sensor.sun_next_setting.last_changed | as_local + 
   timedelta(hours = states('sensor.templatesunset_offset') | float(0))
}}

I want to use a template for offset because in winter the sunset is earlier so I have created a template to add more offset if sunset hour is lower than 20h

With regards

Just be careful, last_changed does not survive restart.

Oh, I didn’t kow it, I will try your options. Thanks so much!