Template sensors, history stats and some weirdness

Ok, I spent hours on this yesterday so if it is something stupid I’m going to be very annoyed (at myself).

I have a rain gauge which reliably publishes to MQTT and updates an MQTT sensor every time it ‘tips’ with the rain. The status alternates between ‘TIP1’ or ‘TIP2’.

So far so good.

I want to be able to stop counting tips during any maintenance or testing so I have a boolean that suspends counting.

Because an MQTT sensor can’t be templated I have another sensor that is templated, and reflects the MQTT sensor only if the boolean is in the correct state (off).

Still so far so good. That all happens as expected.

Now it gets weird.

I use History Stats sensors to count tips of varying time periods.

The issue I have is that:
The History Stats sensors only update for the first tip after I turn the boolean off.

What is going on here?
Thanks.


#===================
#=== Input Booleans
#===================
input_boolean:
  rain_gauge_suspend:

#=========
#=== MQTT
#=========
mqtt:
  sensor:
    # Rain Gauge MQTT Message Received
    # this records ALL tips but they are only counted in
    # sensor.rain_gauge if not in maintenance mode
    - name: Rain Gauge MQTT
      unique_id: rain_gauge_mqtt
      state_topic: 433/rain_gauge

#============
#=== Sensors
#============
sensor:
  #==================
  #=== History Stats
  #==================
  # Rain Gauge Tips Today
  - platform: history_stats
    name: Rain Gauge Tips Today
    unique_id: rain_gauge_tips_today
    entity_id: sensor.rain_gauge
    state: 
      - TIP1
      - TIP2
    type: count
    start: >
      {{ now().replace(hour=0, minute=0, second=0) }}
    end: >
      {{ now() }}

#==============
#=== Templates
#==============
template:
  # Tip Registered (only if not in maintenance mode)
  - trigger:
      - platform: state
        entity_id: sensor.rain_gauge_mqtt
        to:
          - TIP1
          - TIP2
    sensor:
      - name: Rain Gauge
        unique_id: rain_gauge
        state: >
          {% if states('input_boolean.rain_gauge_suspend') == 'off' %}
            {{ trigger.to_state.state }}
          {% else %}
            Maintenance Mode ({{ trigger.to_state.state }})
          {% endif %}

In a nutshell:
sensor_rain_qauge_mqtt - ALWAYS updates correctly
sensor.rain_gauge - ALWAYS updates correctly whether input_boolean.rain_gauge_suspend is on or off
sensor.rain_gauge_tips_today - (history_stats) only updates the first time after the boolean is turned off

Yes it can.

mqtt:
  sensor:
    # Rain Gauge MQTT Message Received
    # this records ALL tips but they are only counted in
    # sensor.rain_gauge if not in maintenance mode
    - name: Rain Gauge MQTT
      unique_id: rain_gauge_mqtt
      state_topic: 433/rain_gauge
      value_template: "{{ your template here }}"  ## <---- Ues this

Ok, thanks for pointing that out :scream: :man_facepalming:

However, I still have the same behaviour with the history sensor not updating after the first time


#=========
#=== MQTT
#=========
mqtt:
  sensor:
    # Rain Gauge MQTT Message Received
    - name: Rain Gauge
      unique_id: rain_gauge
      state_topic: 433/rain_gauge
      value_template: >
        {% if states('input_boolean.rain_gauge_suspend') == 'off' %}
          {{ value }}
        {% else %}
          Maintenance Mode ({{ value }})
        {% endif %}

You can’t do this:

What do you think the history stats sensor would make of this value when it is expecting a number?

If you look in Settings → System → Logs you will very likely see errors due to this.

Also your mqtt sensor does not have a unit_of_measurement specified so its state value will not be treated as a number.

Why does the history stats sensor care if it is a number? I just want to count the state changes when the state is either ‘TIP1’ or ‘TIP2’.

I use the count of state changes to calculate the actual rainfall measurement (One tip equals 0.3mm)

I’m not getting any errors in the logs…

Because statistics wasn’t designed to be done on words. What the hell would the count be for “Maintenance Mode”?

But it’s just a count of state changes. Nothing to do with the state itself.

History stats - Home Assistant (home-assistant.io)

1 Like

Sorry, ignore everything I said.

1 Like

Ok, so for anyone coming here it seems that the answer was quite simple but not really documented.
The history stats sensor will count the states but only if it changes to one of the listed states from something else not in the list.

Which in my opinion is not what the docs say, so it must be a bug.
image

It could of course be just a poorly documented ‘feature’ but I doubt it because that feature makes no sense (to me).

I set my MQTT sensor to expire after 1 second and everything seems to be working as expected.

It’s not critical to me0 but a one second granularity might not be enough to make my sensor accurate* but I can live with it (YMMV) as it’s just to adjust the amount of water my garden gets.

*Some of the rain we’ve had here recently has been staggering.