Statistics sensor can't be this hard

I want to know the max value of a sensor over the last 5 minutes.
and in my case the value can only be 0 or 1.
the statistics sensor is kind of usesless here because if
7 minutes ago the sensor changed to 1
and 2 minutes ago it changed to 0 it still says 0 because it only uses the changes not the starting value.
if there are no changes the sensor is unavailable.

now I need 3 sensors to fix this behaviar.

- platform: history_stats
  unique_id: gas_was_consumed_history
  name: "Gas was consumed history"
  entity_id: sensor.thermostat_opentherm_slave_fl
  state: 1
  type: count
  start: >
    {{ as_timestamp(now()) - (300) }}
  duration: 00:00:10
- platform: statistics
  name: "Gas was consumed statistics"
  unique_id: gas_was_consumed_statistics
  entity_id: sensor.thermostat_opentherm_slave_fl
  state_characteristic: value_max
  max_age:
    minutes: 5
  precision: 0

and a template sensor

      - unique_id: gas_was_consumed
        name: "Gas was consumed"
        state: >
          {% if states('sensor.gas_was_consumed_statistics')|is_number and states('sensor.gas_was_consumed_statistics')|int == 1 %}
            {{ states('sensor.gas_was_consumed_statistics') }}
          {% elif states('sensor.gas_was_consumed_history')|is_number and states('sensor.gas_was_consumed_history')|int == 1 %}
            {{ states('sensor.gas_was_consumed_history') }}
          {% else %}
            {{ states('sensor.thermostat_opentherm_slave_fl') }}
          {% endif %}

I can’t assume this is the way it should be done, I guess I’m missing something obvious but don’t know what it is.

That’s the only proper statistics method.

Your history_stats config is just counting the number of times you have the number 1.

Your template is doing oddness.

If you want a sensor that has the max value at all times even when it’s not changing in your 5 minute window, then add a template sensor on top of it that checks if your stats sensor has a value, and if it doesn’t just output the statistic sensors source sensor.

{{ states('sensor.gas_was_consumed_statistics') if 'gas_was_consumed_statistics' | has_value else states('sensor.thermostat_opentherm_slave_fl') }}

good one, that means == 1 should change to >0

that doesn’t work in them mentioned example.

the value was 1 at the beginning (5 minutes ago)
then it changes to 0
stats gives 0
my current value gives 0
so the result is 0
and I need it to be 1.

that’s why I tried using history_stats,
if stats =0, check what the value was exactly 5 minutes ago.

the goal here is to get a notification when the gas usage increases but nothing has used gas.
my gas meters updates every 5 minutes, therefor the delay.
and sensor.thermostat_opentherm_slave_fl updates every 10 seconds and indicates if my boiler/water heater is active.

Then you want to use the derivative integration. Which will tell you the rate of change over a duration.

thnx that on might be helpfull for the trigger of my automation.
now I have

- id: "1709670398760"
  alias: Gas Leak Detection
  description: ""
  trigger:
    - platform: state
      entity_id:
        - sensor.gas_consumed
  condition:
    - condition: numeric_state
      entity_id: sensor.gas_was_consumed
      below: 1

still if sensor.thermostat_opentherm_slave_fl was not active for the last 5 minutes
sensor.gas_was_consumed should flatline,
image

if not I should get a warning, now I got a waring while increasingn 0.001 M3, that I may can fix using Derivative

if you set up derivative properly, you’ll have a graph that looks like this:
image