Strange behavior of a Template Sensor

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() }}

image

Why don’t you have a value for your else?

You are likely not triggering on the “reset” trigger because it is already running due to the time pattern trigger. In that case, you are causing the template to fail due to the lack of value for the else. If you just want it to return the previous value use {{ this.state }}.

I think you’re over complicating this. I’m trying to follow what the sensor is supposed to do, but the output in all cases doesn’t make sense. What’s the goal here with this sensor?

Thank You!

I was thinking if i write nothing it will do nothing.
I’ll add it to my script.

I want to visual the Sunshine over a day.
Additional it show me length of night and day too.

One column corresponds to a 24 hour day.
Yellow = hours with sunshine
Light blue = day without sunshine
Blue = night (hours with sun below the horizon)

The sunshine duration comes from Geosphere Austria.
There is an entity that returns the sunshine duration in seconds for the last 10 minutes every 10 minutes.

With the sensor Daily Sunshine Duration, these values ​​are added up over the course of the day.

Then I have the sensor Tageslänge in Sekunden that I use to calculate the length of the day. Although that is misleading since I am only calculating the time from sunrise to sunset.

The sensor actually only serves as a basis for calculating the next sensors.
I took the attributes with me so that they could be displayed in the sensor during development.

In an other Sensor I calculate the night hours, i.e. the time between sunset and sunrise.

And finally, the sensor that takes the sunshine duration every 10 minutes and subtracts it from the “day length”.
In the chart, the yellow bar grows every 10 minutes when there is sunshine, and the light blue bar decreases by the same amount.

Theoretically I could let the sensor run all the time, but since the sun doesn’t shine at night I want to avoid unnecessary data because the value no longer changes but a data record is still written to the database every 10 minutes.

That’s why I use the if is_state('sun.sun', 'above_horizon') condition.

It is complicated but, for me, it is the easiest way I could think of.
I hope my explanation isn’t to confusing because english is not my native language.

Thank you for this hint!

Now it’s running fine.

Summary
#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 past 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 %}
                      {{ states('sensor.tagesdauer_ohne_sonnenschein_1h_refresh', 'this.state')}}
                    {% endif %}
                {% endif %}