The history_stats sensor to count temperature above threshold

Hi
I try to create sensor to count records with temperature above 25 in past 24h hours.
I have temperature sensor:

sensor.online_thermometer_temperature

and added following entry in my sensors.yaml:

- platform: history_stats
  name: "over_25C_count"
  entity_id: sensor.online_thermometer_temperature
  state: ">25"
  type: count
  start: "{{ now() - timedelta(hours=24) }}"
  end: "{{ now() }}"

the problem is it always returns 0 even if temperature exceeds 25 degrees, any idea what i am doing wrong?

You can’t put expressions in the state option. Only states, like “on” or “off” or “12”.

What you are matching on is the literal string / word “>25”. It is not the expression “greater than 25”.

You need to make a template binary sensor that is on when your sensor is greater than 25. Then use the history stats integration to track when that binary sensor is “on”.

Thank you, now i know how to fix it :slight_smile:

1 Like

Is such history_stats sensor valid?:

- platform: history_stats
  name: "over_25C_count"
  entity_id: binary_sensor.over_25c
  state: "on"
  type: count
  start: "{{ now() - timedelta(hours=24) }}"
  end: "{{ now() }}"