Statistics for binary sensor - count only ON?

I want to have a counter how many times the door has been opened. Right now I have this:

sensor:
  - platform: statistics
    entity_id: binary_sensor.front_door
    precision: 0
    name: Door
    sampling_size: 100
    max_age:
      hours: 24

but it creates a counter which accumulates all state changes - i.e. ON and OFF. This effectively doubles the number of door openings and makes it impossible to distinguish an opening from closing.

How can I make a counter that only counts the “open door” states?

You probably want the history_stats sensor platform instead of statistics? You can track a specific state.

1 Like

Or simply create a counter and run an automation. See an example below:

-  action:
  - data:
      entity_id: counter.toilet_counter
    service: counter.increment
  alias: Toilet counter
  condition: []
  id: '1522826423914'
  trigger:
  - entity_id: light.toilet
    platform: state
    to: 'on'