Count how many times a sensor crossed a threshold in a period

Hi community !

I would like to count how many time a sensor crossed a threshold during a period.
for instance, I want to know how many time the temperature of my RaspberryPi4 raised above 70 degres in the last 6h

I was looking into history_stats but I’m not able to use the value template

  - platform: history_stats
    name: Pi4_temp_above80_6h_count
    entity_id: sensor.processor_temperature
    state: ">70"
    type: time
    end: "{{ now() }}"
    duration:
      hours: 6

I tried to used value_template as below

value_template: '{{ states("sensor.processor_temperature") | float > 70 }}'

but the config is wrong.

any idea how to do this ?

thank you

Luj

1 Like

Create a binary template sensor with your template:

value_template: '{{ states("sensor.processor_temperature") | float > 70 }}'

Use history stats to count how long it has been on in 6 hours.

2 Likes

indeed you are right :slight_smile: thank you very much !

here is the code I have used:

binary_sensor:
  - platform: template
    sensors:
      rpi_temp_75degre:
        friendly_name: "RPi temp above 75degree"
        value_template: '{{ states("sensor.processor_temperature") | float > 75 }}'


sensor:
  - platform: history_stats
    name: rpi_temp_75degre_6h_count
    entity_id: binary_sensor.rpi_temp_75degre
    state: "on"
    type: ratio
    end: "{{ now() }}"
    duration:
      hours: 6

1 Like

Hi,

Can anyone help? My template has stopped working, I have tried my best to reformat it but I get errors / it doesn’t work!

template:
- sensors:
front_sump_on_24:
friendly_name: “Front sump on over 24H”
value_template: ‘{{ states(“sensor.front_sump_pump_energy_current_2”) | float > 1 }}’

Regards

This is a two year old topic.

Create a new topic and format your posted configuration correctly.