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
{{ (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 ![]()
ahh okay ‘dusk’ is the end of the day
(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
.
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 }}
Thx a lot, it look like exact what I’am looking for
!
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
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!
Bringing up this old topic as I found it and looks like it’s spot on for me, planning to use it as a reminder to open the greenhouse doors at the morning if the forecast is sunny.
I have however two questions:
- Is this code still valid/working?
- Is it possible to use the native weather integration instead of OpenWeatherMap?
If yes, what needs to change?
Thanks.
The code above won’t work any longer directly, as the weather integrations no longer have the forecasts stored as an attribute. You’ll need to use the get_forecasts action to pull it into a sensor:
That first example with the addition of these lines:
attributes:
forecast: "{{ hourly['weather.home'].forecast }}"
will give you a sensor with the hourly forecast as an attribute, which you can then use in a revision of the old posts.
Thanks for the quick response. That sounds promising.
I will try it as soon as I have taken care of yesterdays multiple hard drive failure in my home lab server…
FYI, this is what I’m using in my template.yaml file that is working perfectly: (in my case I’m not counting partly cloudy)
- trigger:
- platform: time
at: "06:00:00" # Run once per morning before your automations
action:
- service: weather.get_forecasts
target:
entity_id: weather.openweathermap
data:
type: hourly
response_variable: hourly
- variables:
sunny_hours: >-
{% set count = namespace(value=0) %}
{% set today = now().date() %}
{% set next_sunset = as_timestamp(state_attr("sun.sun", "next_setting")) %}
{% for h in hourly['weather.openweathermap'].forecast %}
{% set ts = as_timestamp(h.datetime) %}
{% if ts < next_sunset and as_datetime(h.datetime).date() == today %}
{% if h.condition == "sunny" %}
{% set count.value = count.value + 1 %}
{% endif %}
{% endif %}
{% endfor %}
{{ count.value }}
sensor:
- name: "sunny_hours_today"
unique_id: "sunny_hours_today_uid"
state: "{{ sunny_hours }}"
unit_of_measurement: "h"