Resetting a sensor at midnight

I do have a template that looks like this:

- platform: template
  sensors:
    molen_gemiddelde_vandaag:
      friendly_name: "Gemiddelde molensnelheid (vandaag)"
      unique_id: molen_gemiddelde_vandaag
      unit_of_measurement: "Enden/min"
      value_template: >
        {% set t = now() %}
        {% set s = states('sensor.gemiddelde_molensnelheid_vandaag') | float(0) %}
        {% if t.hour == 0 and t.minute == 0 %}
          0
        {% else %}
          {{ s }}
        {% endif %}

This works fine, but I would like to reset this sensor to 0 ad midnight

How should I do that?

Adrie de Regt

It needs to be a triggered template.

template:
  - trigger:
      - trigger: time
        at: "00:00:00"
      - trigger: state
        entity_id:
          - sensor.gemiddelde_molensnelheid_vandaag
    sensor: 
      - name: "Gemiddelde molensnelheid (vandaag)"
        unique_id: molen_gemiddelde_vandaag
        unit_of_measurement: "Enden/min"
        state: >
          {% set t = now() %}
          {% set s = states('sensor.gemiddelde_molensnelheid_vandaag') | float(0) %}
          {% if t.hour == 0 and t.minute == 0 %}
            0
          {% else %}
            {{ s }}
          {% endif %}

Thanks for your reply.
I am not sure how this fits in my sensors.yaml
When I put this in my sensors.yaml I see lots of erros

Adrie de Regt

It doesn’t.
It needs to be in configuration.yaml or possibly templates.yaml if that too is split up.

OK, and I should remove the code I showed above from sensors.yaml?

Adrie de Regt

Yes, it doesn’t belong there

I’m not sure this will help, because the original version contains now() if should evaluate every minute anyway.

What exactly do you mean by reset? The sensor that you had is 0 at every first minute of the day, and the average you had every other moment of the day. The template sensor does not affect any other value during the day. The trigger based one does pretty much the same, but it stays 0 until a new average sensor value comes in.

I think what you need is a statistics sensor that calculates the average of the thing that is being averaged. In that sensor you can specify what the period to be averaged is. If you already have an average, it is unclear what the time scope of that average is. You cannot remove information from how that was calculated.

You need to specify a template for datetime value min and max, and set it to keep the last recorded sample, because it might otherwise be undefined at midnight…

OK, I’ve done that and so far it looks OK. I will check this out and report back here the results.
Thank you for now

Adrie de Regt

I do see my sensor go to 0 at midnight, see pict1


But shortly after it returns to the original value.

Let me explain what I am trying to achieve, This installation of HA is used in an old-dutch windmill. We use it to measure the speed of the mill when it is running. In such a mill we express the speed in enden/min (ends/min) An end is a blade passing by. If you divide it by 4, you get the revs/min. This works fine. Now we would like to see the average speed during a day, so evry midnight we start at 0 enden/min. And also if we stop the mill during the day for a few minutes (or longer) we donot want that to effect the average speed.
I do have:a sensor.‘endenteller’ that gives me the speed in enden/min

I created an extra sensor: molen_draai_snelheid_actief

    molen_draai_snelheid_actief:
      friendly_name: "Molen draai snelheid actief"
      unit_of_measurement: "Enden/min"
      value_template: >
        {% set snelheid = states('sensor.endenteller') | float(0) %}
        {% if snelheid > 0 %}
          {{ snelheid }}
        {% else %}
          unknown
        {% endif %}

This sensor takes care of stopping the mill preventig the avg speed to decrease.

The avg speed is given by this sensor::

- platform: statistics
  name: "Gemiddelde molensnelheid vandaag"
  entity_id: sensor.molen_draai_snelheid_actief
  state_characteristic: mean
  max_age:
    hours: 12

And here it’s going wrong. I would like this sensor to reset to 0 at midnight.
So I put this in yesterday:

template:
  - trigger:
      - trigger: time
        at: "00:00:00"
      - trigger: state
        entity_id:
          - sensor.gemiddelde_molensnelheid_vandaag
    sensor: 
      - name: "Gemiddelde molensnelheid (vandaag)"
        unique_id: molen_gemiddelde_vandaag
        unit_of_measurement: "Enden/min"
        state: >
          {% set t = now() %}
          {% set s = states('sensor.gemiddelde_molensnelheid_vandaag') | float(0) %}
          {% if t.hour == 0 and t.minute == 0 %}
            0
          {% else %}
            {{ s }}
          {% endif %}

with the result as shown in pict1.

pict2 shows what I want. The orange line show the actul speed. the blue line shows the average speed. The blue line going to 0 at app 11:20u is when I iomplemented your code yesterday.
What I want is what you see in pict2, but with the blue line going to 0 at midnight and staying there. (unless we start the mill again during the rest of the day

Suggestions?

Adrie de Regt

Since we did not know anything about your sensor then we can only answer you based on what you ask for.
And naturally if {{ s }} is not zero then it will just skip back up to the same value again.
I’m not sure I understand it all, but it seems as you need a utility sensor.
Utility Meter - Home Assistant

Or base the template on sensor.molen_draai_snelheid_actief and not the other sensor (sensor.gemiddelde_molensnelheid_vandaag).