Template code help

Hello - I need some help with template code. I want to set a timer based on sunrise. I have an automation that triggers some lighting based on a motion sensor. Within that automation, I use a timer to specify exactly how long to keep the lights on. My time calculations aren’t working and that’s where I need some help. Below is what I want to start with:

IF current time is within 90 minutes of sunrise
set the timer to 120 minutes
else
set the timer to 40 minutes

Can someone help get me started?

//Brew

  duration: >
    {{ '02:00:00' 
      if states('sensor.sun_next_rising') | as_datetime - now() < timedelta(minutes=90)
      else '00:40:00' }}

Thank you, I’ll give that code a try!

You can copy-paste it into the Template Editor to confirm it reports the correct value.

Thanks again, worked like a charm!

1 Like