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       
After you’ve entered the code save it and go to Configuration > Server Control > Check configuration > Restart server
An easily configured gauge display from Home Assistant

To display the feature/s just added, we must restart Home Assistant. When Home Assistant is live again, go to the frontend or Overview. Click Configure UI > + choose a ‘card’ and add this code

# 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 
It will be obvious to some why the sun’s elevation varies between 20 and minus 50 degrees. They will also notice that the sun’s azimuth goes from 0 degrees at midnight through to 360 degrees at the next midnight. The graphic below shows how much of a sun rotation is our day light. I’m at last getting to understand how to align solar panels – making these graphs has been key to this understanding.

Look up where you live and follow the sun’s path at suncalc.org

Leave a Reply

Your email address will not be published. Required fields are marked *