State change question

I have a rain sensor that counts ‘OFF’ states as following:

- platform: history_stats
  name: Rainsensor flips
  entity_id: binary_sensor.sonoff_1001d8970f # The SONOFF sensor
  state: 'off'
  type: count
  start: '{{ now().replace(hour=0, minute=0, second=0) }}'
  end: '{{ now() }}'

How can I modify the counting to include not only the ‘OFF’ states but also the ‘ON’ states (both OFF-to-ON and ON-to-OFF transitions)?"

1 Like

image

  state: 
    - 'off'
    - 'on'

Wow - great. Thanks a lot Koying !

Unfortunately, it doesn’t seem to work. My counter remains at 1 always.

In order for it to register with History Stats there needs to be an intermediate state, the “1” you’re seeing is most likely from the sensor going from “unavailable” to “off”.

Your options are basically to have a History Stats sensor for each state then combine them with a helper or Template sensor. Or, you can just take the value of the “on” History and multiply it by 2.

1 Like

There it is. Two sensors - one for each state and then added with a template sensor :smiley:

Worked like a charm. Thanks a lot @Didgeridrew !!

1 Like

I have one more thing @Didgeridrew !
I implemented individual History stats sensors now like so:

  - platform: history_stats
    name: Rainsensor flips 3 on
    entity_id: binary_sensor.sonoff_1001d8972e
    state: 'on'
    type: count
    start: '{{ now().replace(hour=0, minute=0, second=0) }}'
    end: '{{ now() }}'
  - platform: history_stats
    name: Rainsensor flips 3 off
    entity_id: binary_sensor.sonoff_1001d8972e
    state: 'off'
    type: count
    start: '{{ now().replace(hour=0, minute=0, second=0) }}'
    end: '{{ now() }}'

At midnight, only one of them got a reset to ‘0’, while the other was reset to ‘1’.
Why is that ?
Rainsensor flips 3 off:

Rainsensor flips 3 on:

why is that?