Hello!
From integration “Deutscher Wetterdienst (DWD)” I got a sensor “sun_duration” with data values (sunshine forcast in minutes) for each hour like this:
Data:
- datetime: '2023-05-12T17:00:00.000Z'
value: 1560
- datetime: '2023-05-12T18:00:00.000Z'
value: 1320
- datetime: '2023-05-12T19:00:00.000Z'
value: 780
- datetime: '2023-05-12T20:00:00.000Z'
value: 300
- datetime: '2023-05-12T21:00:00.000Z'
value: 0
Within an custom:apexcharts-card
I display the data (in minutes) like this example (in hours) for the sun duration for a given timeslot (configured in the card):
- entity: sensor.sun_duration_location
data_generator: |
return entity.attributes.data.map(
(item) => {
return [new Date(item.datetime).getTime(), (item.value/60)];
});
Now I want to use these sensor values to calculate the sun duration of a specific time slot like 11:00 AM until 01:00 PM with an new template sensor for using in an automation. But I’ve no idea how to handle these data values in a template sensor.
I’ve found these example, but it’s not usable for me:
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 }}
Can anyone help me with this?
Thank you very much!
Greetings, Holger