Calculate sun hours forecast

is there a way to calculate the sum of sun hours from now until the end of the day?
Currently I use weather.dark_sky for the weather forecast, if it doesn’t work with that is there a more suitable weather integration?
My goal is to decide on this basis whether I block the central heating because I can assume that the sun hours are sufficient to charge the heat pump.
Thx and best Regards, Jörg

1 Like
{{ (as_timestamp(state_attr("sun.sun", "next_dusk")) - as_timestamp(now())) / (60 * 60) }}

EDIT: Not the proper response to the actual question

as I understand right this is the start of the next dusk not the total sum of the sunny hours?

It does what you asked. By definition, there are no more “sunny hours” after dusk :wink:

ahh okay ‘dusk’ is the end of the day :wink: (my bad english).
But what is about the cloudy hours before dusk?

Oh, sorry. You actually meant “hours where sun is shining”.
That’s not the proper way, then

It’d help to know where you are, as many forecast providers are local / national. Your profile doesn’t say, and it’s no longer politically-correct to guess Germany / Austria / Switzerland based on your name :grin:.

:smiley: Germany / Hessen

Here is a sample templte that uses the “hourly” mode of OpenWeatherMap

{% set sunH = namespace(value=0) %}
{% set nextDuskE = as_timestamp(state_attr("sun.sun", "next_dusk")) * 1000 %}
{% for h_forecast in state_attr("weather.owm_one_hourly", "forecast") %}
	{% if h_forecast.condition == "sunny" and h_forecast.datetime < nextDuskE %}
		{% set sunH.value = sunH.value + 1 %}
	{% endif %}
{% endfor %}
{{ sunH.value }}
1 Like

Thx a lot, it look like exact what I’am looking for :slight_smile: !

I’d like to update/extend on the great example given above by @koying in case someone else needs it and ends up in this topic.
The changes I did:

  • Count the hours until sun setting, not dusk since my usecase is to calculate the expected heat generated by a thermal solar system (DE: Solarthermie) that is enough to replace the heatpump if it’s sunny
  • Also count the hours partly that are “partlycloudy” since these also return enough energy/heat from the roof
template:
  - sensor:
    - name: "Sun hours today"
      unit_of_measurement: "h"
      state: >
        {% set sunH = namespace(value=0) %}
        {% set nextSunSetting = as_timestamp(state_attr("sun.sun", "next_setting")) %}
        {% for h_forecast in state_attr("weather.openweathermaphourly", "forecast") %}
          {% if h_forecast.condition == "sunny" and as_timestamp(h_forecast.datetime) < nextSunSetting %}
            {% set sunH.value = sunH.value + 1 %}
          {% endif %}
          {% if h_forecast.condition == "partlycloudy" and as_timestamp(h_forecast.datetime) < nextSunSetting %}
            {% set sunH.value = sunH.value + (100-h_forecast.clouds)/100 %}
          {% endif %}
        {% endfor %}
        {{ sunH.value }}

I’m using it in an automation which always run at 6:00 am.
If there are more than 5 hours of sun, then the automation disables the heatpump for the whole day, otherways it stays active with it’s own, proprietary controll system.
It’s 6:00am since that is the timestamp when the energy tariff is switched to the more expensive day fee.

Without this automation I often had the case (especially during spring/autumn) that the heatpump started to heat at 4-6am and continue until 8am since it’s the coldest time of the day/night. And then starting on 9am the sun is available and starts to heat via the solar collector.
Result was a waste of (paid) energy and too hot rooms at the end of the day

2 Likes

This looks pretty neat. Where exactly do you add this template.

I tried adding an automation that triggers this template at at certain time, but without success says somethings like “Noneobject” . Any change you can share a screendump of your implementation?

I don’t think you can add this via the GUI. You need to include that code within configuration.yaml, making sure you only end up with one template: section.

First of all, thank you for sharing your content.

I’m trying to implement the template in my configuration.yaml but to no avail. Where I have to implement the code exactly?
This are my two configuration files:

/config/configuration.yaml


# Loads default set of integrations. Do not remove.
default_config:

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
sensor: !include mqttsensors.yaml #/config/mqttsensors.yaml
# binary_sensors: !include binary_sensors.yaml

mqtt:
  sensor:
  - name: "Solar_Wasserspeicher_oben"
    object_id: "Solar_Wasserspeicher_oben"
    unique_id: "Solar_Wasserspeicher_oben"
    state_topic: "home/keller/solar/ESP_EASY_SOLAR/Solar_Temp_Wasserspeicher_Oben"
    unit_of_measurement: "°C"
    icon: mdi:sun-thermometer-outline
    device_class: temperature
  - name: "Solar_Wasserspeicher_unten"
    object_id: "Solar_Wasserspeicher_unten"
    unique_id: "Solar_Wasserspeicher_unten"
    state_topic: "home/keller/solar/ESP_EASY_SOLAR/Solar_Temp_Wasserspeicher_Unten"
    unit_of_measurement: "°C"
    icon: mdi:sun-thermometer-outline
    device_class: temperature
  - name: "Solar_Kollektor"
    object_id: "Solar_Kollektor"
    unique_id: "Solar_Kollektor"
    state_topic: "home/keller/solar/ESP_EASY_SOLAR/Solar_Temp_Kollektor"
    unit_of_measurement: "°C"
    icon: mdi:sun-thermometer-outline
    device_class: temperature 

/config/mqttsensors.yaml

- platform: openweathermap 
  name: "Sun hours today"
  unit_of_measurement: "h"
  state: >
    {% set sunH = namespace(value=0) %}
    {% set nextSunSetting = as_timestamp(state_attr("sun.sun", "next_setting")) %}
    {% for h_forecast in state_attr("sensor.openweathermaphourly", "forecast") %}
      {% if h_forecast.condition == "sunny" and as_timestamp(h_forecast.datetime) < nextSunSetting %}
        {% set sunH.value = sunH.value + 1 %}
      {% endif %}
      {% if h_forecast.condition == "partlycloudy" and as_timestamp(h_forecast.datetime) < nextSunSetting %}
        {% set sunH.value = sunH.value + (100-h_forecast.clouds)/100 %}
      {% endif %}
    {% endfor %}
    {{ sunH.value }}

Can I use the “sensor:” twice?

Thank you for help me out!