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