I have the following configured and it works for displaying sunrise and sunset times.
sensor:
- platform: template
sensors:
nextsunrise:
friendly_name: 'Next Sunrise'
value_template: >
{{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom(' %I:%M%p') | replace(" 0", "") }}
icon_template: mdi:weather-sunset-up
nextsunset:
friendly_name: 'Next Sunset'
value_template: >
{{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom(' %I:%M%p') | replace(" 0", "") }}
icon_template: mdi:weather-sunset-down
I’d like to be able to use these times to display the hours of daylight but I just can’t get the template to do any math. It’s also complicated that after sunrise (and sunset) the next sunrise (sunset) will be for the next day. I can subtract the 2 values from each other and it seems to give a result in seconds (and can be + or - depending on if you are before sunset and after sunrise etc)
Is there any way I can do this? Or is there some component?