Binary Sensor Counter - Count boolean state changes

Increment or decrement a counter helper whenever a binary sensor (e.g., a contact sensor) or an input boolean helper turns on, turns off, or does either. The automation is specifically triggered when the state changes from “off” to “on”, for example, and state changes from “unavailable” and “unknown” are ignored.

Optionally set a minimum duration the sensor state has to be changed for to reduce accidental counter modifications. This functionality allows you to reduce noise in your data (e.g., a door slamming shut, bouncing back open, then closing again) but also to count instances when a door or a window has been left open for too long.

My use case is counting how many times a door has been opened, but also to simply practice publishing blueprints.

Requirements

Usage

Create a new Binary Sensor Counter automation on your Automations page.

Installation

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Click the badge to import this blueprint or alternatively use the link or code below.

GitHub: https://github.com/DemianWright/home-assistant/blob/fcda8470b90d7e08ae89119b82f794928da6fd9b/blueprints/automation/DemianWright/binary_sensor_counter.yaml

Show YAML code
blueprint:
  name: Binary Sensor Counter
  description: |
    Modify a counter helper based on a binary sensor or an input boolean state change (e.g. a contact sensor opens).
    **Version**: 1.0.0
  author: Demian Wright
  homeassistant:
    min_version: "0.9.0"
  domain: automation
  input:
    sensor_entity:
      name: Sensor entity
      description: Sensor that triggers this automation.
      selector:
        entity:
          domain:
            - binary_sensor
            - input_boolean
    trigger_state:
      name: Trigger condition
      description: When to modify the counter.
      default: "to_on"
      selector:
        select:
          options:
            - label: Sensor turns on
              value: "to_on"
            - label: Sensor turns off
              value: "to_off"
            - label: Sensor turns on or off
              value: "to_either"
    trigger_duration:
      name: Trigger duration
      description: How long sensor state has to change for before modifying the counter. A short duration (e.g., 1 second) prevents unnecessary triggers, such as when a door slams shut, bounces back, and closes again (which would normally modify the counter multiple times). However, for tracking something like a button press, use a 0-second duration to trigger the automation on every state change, even rapid ones from button mashing.
      default:
        hours: 0
        minutes: 0
        seconds: 0
      selector:
        duration:
    counter_helper:
      name: Counter helper
      description: Counter helper to modify.
      selector:
        entity:
          domain: counter
    counter_modification:
      name: Modification type
      description: How to modify the counter helper.
      default: "increment"
      selector:
        select:
          options:
            - label: Increment by 1
              value: "increment"
            - label: Decrement by 1
              value: "decrement"
mode: parallel
max: 1000
trigger:
  - platform: state
    entity_id:
      - !input sensor_entity
    from: "off"
    to: "on"
    for: !input trigger_duration
  - platform: state
    entity_id:
      - !input sensor_entity
    from: "on"
    to: "off"
    for: !input trigger_duration
action:
  - variables:
      trigger_state: !input trigger_state
      modification: !input counter_modification
  - if:
      - condition: template
        value_template: "{{ trigger_state == 'to_either' or (trigger_state == 'to_on' and trigger.id == '0') or (trigger_state == 'to_off' and trigger.id == '1') }}"
    then:
      - service: >
          {% if modification == "increment" %}
            counter.increment
          {% else %}
            counter.decrement
          {% endif %}
        entity_id: !input counter_helper

Uninstall through your Blueprints page.

Changelog

Version 1.0.0 on 2024-01-02

  • Initial release.
1 Like

For future reference, the History Stats integration is designed for this purpose (and more). For example, this creates a History Stats sensor that maintains a count of the number of times a door is opened.

sensor:
  - platform: history_stats
    name: Door Openings 
    entity_id: binary_sensor.front_door
    state: "on"
    type: count 
    start: "{{ 0 }}"
    end: "{{ now() }}"

The start/end options limit the duration of data gathering (i.e. door openings for today, a week, month, etc) after which it resets and starts from zero. The example is configured to never reset and simply increment the count forever.

2 Likes