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”