Moving maximum of sensor value with nightly reset

Hello,
I’m having a problem with the readout of an solar Inverter, as the network is not very stable and sometimes the value can not be read, so I configured it to return 0. However this is messing up some other parts of the energy dashboard, so I would like to keep the last value if the value comes back as undefined.

At midnight I want it to reset to hero, as it is a daily value.

I think this should do it, but somehow it does not work:

template:
- sensor:
    - name: "pv_energy_today_filtered"
      unit_of_measurement: 'kWh'
      device_class: "energy"
      state_class: "total_increasing"
      state: >-
          {% if is_state('sensor.pv_energy_today', '0') %}
            {{ states('sensor.pv_energy_today_filtered') }}
          {% elif now().hour == 0 %}
            {{ 0 | float(0) }}
          {% else %}
            {{ states('sensor.pv_energy_today') }}
          {% endif %}

command_line:
  - sensor:
      name: "pv_gartenhuette_energy_today"
      #command: "cat test | grep \"var webdata_today_e\" | cut -d'\"' -f 2"
      command: "curl -s -u admin:PW http://192.168.0.71/status.html | grep \"var webdata_today_e\" | cut -d'\"' -f 2"
      unit_of_measurement: 'kWh'
      device_class: "energy"
      state_class: "total_increasing"
      scan_interval: 60
      value_template: '{{ value | float(0) }}'

Somehow it does not work, does anybody has an idea what I have done wrongly

I am not sure about the mistake in your code however you can try diferent pieces of it in the Developer->Templates screen for testing… but my main contribution to you here is instead of setting the value to zero in your code when an invalid value is received, just set it to show either - 1. a valid value is recevied or 2. what the value was last reported as (whenever an invald value is returned), or actually 0 if it is the time you specify. You can do this by setting up a helper and store the value in it each time (only when a valid value is received or you have reset to zero). Then in your template when the invalid value is received, just instead use the value stored in the helper.

So if you have to restart HA you have lost the tem plate sensor value as well, correct? Input helpers survive restarts - that may help too -

If you do not get to the bottom of it you can also have an automation that would run occassionally to reset the values, but we have to always take into account that there are alot of calculations hidden under the covers for the default energy dashboard that cannot be controlled directly, having to make those corrections in HA manually under Developer Tools → Statistics (find the sensor then click on the little graph on the right side of that line and you can check and reset values reported - “outliers” is a handy link to show the values to you that may seem out of whack that you might want to adjust)