Help making a sensor that counts the amount of time it is Sunny each day

I am trying to build a history stats sensor so I can track the amount of time the sun was actually out on a daily basis. I am using weather.home as a source of weather info. It has a state called sunny. I want to track the amount of time it shows sunny. I made it that far however for whatever reason the entity shows sunny sometimes when it is dark outside. So my issue is I am wanting to have this sensor only count the time in sunny status when the sun is above the horizon. I know sun has a state called above horizon. Any help would be appreciated. Thanks.

I need help writing the start time and end time based on sunrise and sunset. Here is what I have now. It starts at midnight.

‘’’

  • platform: history_stats
    name: Sun Time
    entity_id: weather.home
    state: ‘sunny’
    type: time
    start: ‘{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}’
    end: ‘{{ now() }}’
    ‘’’

Create a template binary sensor that evaluates to on (true) when both the sunny sensor is on and the sun is above the horizon. Use this sensor as the source for your history stats sensor.

Thanks! Why didn’t I think of that?! I will report back.

What is the device class of sun?

You don’t really need a device class for your template sensor in this case. It’s really just an intermediate sensor that you’ll likely not need in the front end.

But the answer to your question, the sun entity doesn’t have a device class. It’s the one and only sun, there’s no reason to classify it.

@evisser4 -

template:
  - binary_sensor:
      - name: "Daytime Sun"
        state: >
          {{ is_state('weather.home','sunny')
              and is_state('sun.sun','above_horizon') }}

Thanks! I ended up implementing a modified version of what you provided.