Count sun hours per day

Hi, I want to irregate my garden depending on how much sun there has been for the last 24 hours, as I can see the weather integratin is displaying if it is sunny or not, but how can I use that information to calculate the ammount of hours there has been sun the last 24 hours?

I think you can do it in two steps. First, set up a binary template sensor that just shows whether it’s sunny or not, based on your weather integration.

Once you’ve got that, read up on using thestatistics sensor, to calculate how long your sunny sensor was true over a day.

1 Like

Thank you, I will get back in a day or two when I have had the time to set it all up :slight_smile:

Well I tried and it does not do what I want it to do, this is in my configuration.yaml:

# Sun counter binary sensor
  - platform: template
    sensors:
      sun_up:
        friendly_name: "Sunny"
        value_template: >-
          {{ state_attr('weather.lovkulla', 'Sunny')|float > 0 }}

And:

# enable the recorder integration (optional)
recorder:

# Example configuration.yaml entry
sensor:
  - platform: statistics
    entity_id: binary_sensor.sun_up
    name: Sunny_hours
    sampling_size: 96
    max_age:
      days: 2

It does show both the binary sensor and the statistical, the statistical does not count up even thou I can see that the weather data shows 2 hours of sun today.

I also found out a small error in my thinking, apparenlty the weather integration shows the weather as “sunny” even after sunset… so I will need to do a condition to only count in the hours that are sunny if the sun is up.

Hi, I have now after doing some more reading come further down and need some basic help again, asking for:

states(‘weather.lovkulla’), returns the current state , sunny, cloudy and so on, now I need to be able to compare the current state to a condition, how do I do that?

E.g. I want to check for how long “states(‘weather.lovkulla’)” is “=sunny” on top of that I want to check that only if the the condition “sun = above horison” is true

Any help for a beginner in python coding would be a great help.

For those still following, I have managed to do this:

# Sun counter binary sensor
  - platform: template
    sensors:
      sunny:
        friendly_name: "Sunny"
        value_template: >-
          {{states('weather.lovkulla')|string == 'sunny'}}
          {{states('sun.sun')|string != 'below_horizon'}} 

This results in an answer giving me true, true for when the conditions are sunny and the sun is up. I will look into how to make that into a single “true” code, but this is the best for now, looks like it might function as is now as well.

Well it did not take long:

# Sun counter binary sensor
  - platform: template
    sensors:
      sunny:
        friendly_name: "Sunny"
        value_template: >-
          {{is_state('weather.lovkulla', 'sunny')
            and is_state('sun.sun', 'above_horizon')}}

This give the wanted reply

This was very helpfull: https://www.home-assistant.io/integrations/binary_sensor.template/

Finnal version, lets hope it works looks like this:

# Sun counter binary sensor
  - platform: template
    sensors:
      sunny:
        friendly_name: "Sunny"
        value_template: >-
          {{is_state('weather.lovkulla', 'sunny')
            and is_state('sun.sun', 'above_horizon')}}
            
# Rain counter binary sensor
  - platform: template
    sensors:
      rainy:
        friendly_name: "Rainy"
        value_template: >-
          {{is_state('weather.lovkulla', 'rainy')
            or is_state('weather.lovkulla', 'pouring')}}
          
          
# enable the recorder integration
recorder:

# stats for sun hours
sensor:
  - platform: statistics
    entity_id: binary_sensor.sunny
    name: Sunny_hours
    sampling_size: 96
    max_age:
      days: 2
      
# stats for rain hours
  - platform: statistics
    entity_id: binary_sensor.rainy
    name: Rainy_hours
    sampling_size: 96
    max_age:
      days: 2

I have not verified all the results yet, but this seems to yield the result I want when testing just the code.

For those who want to do something similar, here is the final result that is working for me:

# Sun counter binary sensor
  - platform: template
    sensors:
      sunny:
        friendly_name: "Sunny"
        value_template: >-
          {{(is_state('weather.lovkulla','sunny')
            or is_state('weather.lovkulla','partlycloudy'))
            and is_state('sun.sun', 'above_horizon')}}
            
# Rain counter binary sensor
  - platform: template
    sensors:
      rainy:
        friendly_name: "Rainy"
        value_template: >-
          {{is_state('weather.lovkulla', 'rainy')
            or is_state('weather.lovkulla', 'pouring')}}
1 Like