calculate how long is the day
… and see how to add features to the Home Assistant display
An interest in solar energy is a possible reason to record the sun’s azimuth and the time of sunset. Home Assistant can track this and more. I wanted to know the number of hours between sunrise and sunset, and I’ll show this calculation below.
To monitor weather services – see Home Assistant integrations.

- In Home Assistant click Configurator in the left menu. Open configuration.yaml – the file which holds the features which are set manually. Enter the following code using yaml syntax. Do not to add a section heading (e.g. sensor: ) which already exists.
- Look for error messages and save the configuration.yaml.
- Now click the menu item: Configuration > Server control > Check configuration > Restart server
# This integration tracks the sun at your location
sun:
# Don't duplicate sun: or sensor.
# credit for this code goes to xxx
sensor:
platform: template
sensors:
nextsunrise:
friendly_name: '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: 'sunset'
value_template: >
{{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom(' %I:%M%p') | replace(" 0", "") }}
icon_template: mdi:weather-sunset-down
sunhours:
friendly_name: 'sun hours'
value_template: >
{% set nrise = as_timestamp(state_attr('sun.sun','next_rising')) %}
{% set nset = as_timestamp(state_attr('sun.sun','next_setting')) %}
{% if nrise > nset %}
{% set nrise = nrise - 606024 %}
{% endif %}
{{ (nset - nrise)|timestamp_custom('%H:%M',false) }}
icon_template: mdi:weather-sunny
solar_angle:
friendly_name: "sun elevation"
unit_of_measurement: 'degrees'
value_template: "{{ state_attr('sun.sun', 'elevation') }}"
icon_template: mdi:angle-acute
sunazimuth:
friendly_name: 'sun azimuth'
value_template: "{{ state_attr ('sun.sun', 'azimuth') | int }}"
icon_template: mdi:angle-acute
unit_of_measurement: 'degrees'
# THIS GETS MINUTES AS AN INTEGER
sunminutes:
friendly_name: 'sun mins'
value_template: >
{{ (states.sensor.sunhours.state.split(':')[0]|int * 60) + (states.sensor.sunhours.state.split(':')[1] | int) }}
icon_template: mdi:weather-sunny


To display the feature/s just added, we must restart Home Assistant. When Home Assistant is live again, go to the Overview tab. Click Configure UI > + choose a ‘card’ and add a history graph. This is the code inside the card. Save the card and you’ll see the beginning of the graphs below (bottom of this page).
# DAY LENGTH GAUGE - the MIN 8 hours and MAX 16 hours are typical UK values entity: sensor.sunminutes max: 1000 min: 458 name: sunrise to sunset severity: green: 400 red: 800 yellow: 600 theme: default type: gauge unit: min


Look up where you live and follow the sun’s path at suncalc.org
Thanks for the write up. I can recommend looking at the compass card, so you can track the sun’s azimuth on a compass display, and even add a picture of your property to it to show how the sun is oriented compared to your garden or solar panels.
https://github.com/tomvanswam/compass-card
Thank you. Gosh yes you are so right, the compass card is just the thing.
I think I’ll be adding it soon.