Count switches of binary sensor

Hello folks,

How can I count the switching cycles of a binary sensor?

With an automation using a State Trigger that increments a counter entity.

Or use a History Stats sensor to indicate the number of times the binary_sensor is on during the current day (or other time period).

2 Likes

Okay thank you very much.

Is it also possible to display the measured data, for example in a bar chart or a mini-graph card?

So, I measure all switching cycles within a day and have the results for all days displayed in a diagram ā€¦?

Now I tried it this way:

sensor:
  - platform: history_stats
    name: "Brennerstatistik Tag"
    entity_id: binary_sensor.heizungsbrenner
    state: "1"
    type: time
    start: "{{ now().replace(hour=0, minute=0) }}"
    end: "{{ now().replace(hour=23, minute=59) }}"
  - platform: history_stats
    name: "Brennerstatistik Jahr"
    entity_id: sensor.brennerstatistik_tag
    state: "1"
    type: time
    start: "{{ now().replace(month=1, day=1, hour=0, minute=0, second=0) }}"
    end: "{{ now().replace(month=12, day=31, hour=23, minute=59, second=0) }}"

binary_sensor:
  - platform: mqtt
    name: "Heizungsbrenner"
    unique_id: "heatronic_brenner"
    state_topic: "home/heizung/brenner"
    payload_on: "1"
    payload_off: "0"

I am curious if it works

It dosenā€™t work.
What am I doing wrong?

Change the value of the type option from time to count.

1 Like

I saw it allready.

how can I put all the daily counted values into a chart?

It seems the history_stats sensor is the wrong way

The History Stats sensor can be used to report the daily count (and then it resets to start counting for the next day). What you want is to preserve each dayā€™s total count so that you can show weekly/monthly/yearly information (in a graph). To do that, use the History Stats sensor in combination with the Utility Meter integration.

Thatā€™s what I do to record the total amount of time my heating (and cooling) system operates throughout the year.

You can use the History Chart card to display a graph (and thereā€™s a more sophisticated charting solution called ApexCharts).

1 Like

Many thanks, but Iā€™m afraid, that I donā€™t know how tu use the Utility Meter Integration to solve my problem.

Are you sure, that this is the right tool to compare all daily measurements (amount at 23:59)

Try this, let it run for a few days, decide if itā€™s doing what you want.

utility_meter:
  burner_daily:
    source: sensor.brennerstatistik_tag
    cycle: daily
  burner_weekly:
    source: sensor.brennerstatistik_tag
    cycle: weekly
  burner_monthly:
    source: sensor.brennerstatistik_tag
    cycle: monthly
  burner_yearly:
    source: sensor.brennerstatistik_tag
    cycle: yearly

If you choose to change the sensor names to German, make sure the first one (sensor.burner_daily) is not named exactly the same as the existing History Stats sensor (sensor.brennerstatistik_tag).

1 Like

Thank you, Iā€™ll try

Hi, itā€™s me again

Is there a way to count the off-states of a binary_sensor an reset it to zero, when the state is ON

tanks for help

I donā€™t understand your question.

I get a MQTT-signal every minute.
I want to count this signal when it has the value ā€œ0ā€
When the counter ist 15 I want to trigger an automation
When the signal has a value ā€œ1ā€, the counter should reset.

An MQTT Binary Sensor does nothing if the received value is the same as its current value.

If an MQTT Binary Sensorā€™s state is 1 and then it receives a 0 followed by another 0 and then another 0, only the initial 0 causes a state-change from 1 to 0 and thatā€™s what the History Stats sensor would count (if itā€™s configured to count zeros).

Okay, and how can I solve my problem?

It is an additional problemā€¦

Here are all of the MQTT-based integrations.

The one that might be best for your application is MQTT Device Trigger. However it might be challenging to make it usable with the 1 and 0 payloads you are receiving. Anyway, I suggest you explore that one.

itā€™s easy,follow tips below:
1态add code to your configuration.yaml

sensor:
  - platform: template
    sensors:
      switches_on_count:
        friendly_name: "å·²ę‰“å¼€ēš„å¼€å…³ę•°é‡ē»Ÿč®”"
        value_template: "{{ states.switch | selectattr('state', 'eq', 'on') | list | count }}"

2态add a card

views:
  - title: "Home"
    cards:
      - type: entities
        entities:
          - sensor.switches_on_count

@ simplify123
n this how to filter out a certain area in my Case ā€œHome Buildingā€