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
- A binary sensor or an input boolean helper to trigger the automation. If you want to track state changes of multiple binary sensors in a single counter, use a group helper.
- A counter helper to modify. You can create one through: Helpers page > Create helper > Counter.
Usage
Create a new Binary Sensor Counter automation on your Automations page.
Installation
Click the badge to import this blueprint or alternatively use the link or code below.
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.