Sample timer with logic to specify duration

I wanted a timer that would lock my front door after it was opened, but for different intervals depending upon if it is dark or light outside. Using the next_dusk and next_dawn attributes of the sun object and the data template allows me to accomplish this by using logic to set the duration. Since next_dusk and next_dawn increases about 24 hours after dusk and dawn occurrs respectively, if we are in daylight hours then next_dusk will happen sooner than next_dawn, thus next_dusk - next_dawn is less than 0. Once dusk occurs, next_dusk gets updates and then next_dawn happens before next_dusk so next_dusk - next_dawn is greater than 0. Use automation.trigger to start this automation to force the timer to stop and start with the logic based duration.

- alias: Front door custom timer
  trigger:
  action: 
    - service: timer.cancel
      entity_id:  timer.front_door
    - service: timer.start
      data_template:    # if next_dusk happens sooner than next_dawn, then (next_dusk - next_dawn) < 0
         entity_id: timer.front_door
         duration: >
          {% if ((((as_timestamp(states.sun.sun.attributes.next_dusk)) - (as_timestamp(states.sun.sun.attributes.next_dawn)))) < 0) %}
            00:15:00
          {% else %}
            00:05:00
          {% endif %}

I’m assuming you’re just sharing what you did and not looking for help???

In any case, why not just use the state of sun.sun? During the day (between sunrise and sunset) it’s state will be above_horizon, and at night (after sunset and before sunrise) it’s state will be below_horizon. Isn’t that much simpler?

Yes, just sharing. And I like to be precise :smiley: