I have a problem that is driving me crazy.
I created the following template sensor.
#Sensor Tageslänge abzüglich Sonnenscheindauer 10 Minütlich aktualisiert
- trigger:
- platform: time_pattern
minutes: "/10"
- platform: time
at: "00:00:05"
id: init
- platform: time # reset at just at midnight
at: "00:00:00"
id: reset
sensor:
- name: "Tagesdauer ohne Sonnenschein 1h-Refresh"
unit_of_measurement: Sekunden
state: |
{% if trigger.id == 'reset' %}
0
{% elif trigger.id == 'init' %}
{% set t = states('sensor.tageslange_in_sekunden') %}
{% set s = states('sensor.daily_sunshine_duration') %}
{{(t|int) - (s|int)}}
{% else %}
{% if is_state('sun.sun', 'above_horizon') %}
{% set t = states('sensor.tageslange_in_sekunden') %}
{% set s = states('sensor.daily_sunshine_duration') %}
{{(t|int) - (s|int)}}
{% else %}
{% endif %}
{% endif %}
The Sensor is working fine after Restarting Home Assistant until Midnight.
Then the IF clause if is_state('sun.sun', 'above_horizon')
no longer work.
If I restart Home Assistant again The Sensor is working again until Midnight.
I tried an additional Trigger which is executed fine
#Sensor Tageslänge abzüglich Sonnenscheindauer 10 Minütlich aktualisiert
- trigger:
- platform: time_pattern
minutes: "/10"
- platform: time
at: "00:00:05"
id: init
- platform: time # reset at just at midnight
at: "00:00:00"
id: reset
- platform: time
at: "12:00:00"
id: calculate
sensor:
- name: "Tagesdauer ohne Sonnenschein 1h-Refresh"
unit_of_measurement: Sekunden
state: |
{% if trigger.id == 'reset' %}
0
{% elif trigger.id == 'init' %}
{% set t = states('sensor.tageslange_in_sekunden') %}
{% set s = states('sensor.daily_sunshine_duration') %}
{{(t|int) - (s|int)}}
{% elif trigger.id == 'calculate' %}
{% set t = states('sensor.tageslange_in_sekunden') %}
{% set s = states('sensor.daily_sunshine_duration') %}
{{(t|int) - (s|int)}}
{% else %}
{% if is_state('sun.sun', 'above_horizon') %}
{% set t = states('sensor.tageslange_in_sekunden') %}
{% set s = states('sensor.daily_sunshine_duration') %}
{{(t|int) - (s|int)}}
{% else %}
{% endif %}
{% endif %}
The Trigger at 12:00:00 is executed so the Problem have to be within the sun Integration.
History and Log looking OK for me.
So I don’t know where to look for the error anymore.
That’s why I hope that maybe one of you sees something that I’m missing.
Thank you!
In Case you want to see the Code of the other 2 Sensors I use in my calculation:
Summary
#Sonnenschein der letzten 10 Minuten
- trigger:
- platform: time_pattern
minutes: "/10" # update every 10 minutes
seconds: "0"
- platform: time # reset at just past midnight
at: "00:00:01"
id: reset
sensor:
- name: "Daily Sunshine Duration"
unit_of_measurement: Sekunden
state: >
{% if trigger.id == 'reset' %}
0
{% else %}
{{ states('sensor.daily_sunshine_duration')|float(0) +
states('sensor.wien_hohe_warte_sun_last_10_minutes')|float(0) }}
{% endif %}
#Tageslänge in Sekunden
- trigger:
- platform: time
at: "00:00:02"
- platform: time # reset at just past midnight
at: "00:00:00"
id: reset
sensor:
- name: "Tageslänge in Sekunden"
unit_of_measurement: Sekunden
state: >
{% if trigger.id == 'reset' %}
0
{% else %}
{% set sunrise = state_attr('sun.sun', 'next_rising') | as_datetime %}
{% set sunset = state_attr('sun.sun', 'next_setting') | as_datetime %}
{{ (sunset - sunrise).seconds }}
{% endif %}
attributes:
sunrise: >
{{ (state_attr('sun.sun', 'next_rising') | as_datetime | as_local).isoformat() }}
sunset: >
{{ (state_attr('sun.sun', 'next_setting') | as_datetime | as_local).isoformat() }}