Count state changes of a binary sensor per day

Since some days I have a problem with my DSL connection.
(And it will take a while to be solved, because the landlord needs to take action…)
The Fritz integration gives a binary_sensor.fritz_box_7590_connection-sensor.
Does someone have an idea how…

  • to count the state changes per day (or better per 24h starting at 2 o’clock) when the daily reconnect happens?
  • to display this as a number (Bignumber card?)?
  • to display this numbers as a graph (apexchart?)?

Use the count mode:

1 Like

Thanks for the hint.

That’s my solution:

- platform: history_stats
  name: "DSL Disconnects"
  entity_id: binary_sensor.fritz_box_7590_connection
  state: "off"
  type: count
  start: "{{ now().replace(hour=3, minute=0) }}"
  duration:
    hours: 24

- platform: template
  sensors:
    dsl_reconnected_last:
      unique_id: dsl_reconnected_last
      icon_template: mdi:router-network
      value_template: >-
        {%- set last_boot = states('sensor.fritz_box_7590_connection_uptime') %}
          {%- if last_boot in ['unavailable', 'unknown'] or last_boot is none %}
            unknown
          {%- else %}
            {%- set uptime = now().timestamp() - as_timestamp(last_boot) %}
            {%- set time = uptime | int %}
            {%- set minutes = ((time % 3600) // 60) %}
            {%- set minutes = '{}min'.format(minutes) if minutes > 0 else '' %}
            {%- set hours = ((time % 86400) // 3600) %}
            {%- set hours = '{}h '.format(hours) if hours > 0 else '' %}
            {%- set days = (time // 86400) %}
            {%- set days = '{}d '.format(days) if days > 0 else '' %}
            {{ 'Less than 1 min' if time < 60 else days + hours + minutes }}
          {%- endif %}

How would I give the history_stats-sensor a unique ID?

History stats does not support unique id.

Tried some and it seems to work - at least a bit.
Today (without a recognized change) my counter stays at zero (although there was a reconnect at 10 o’clock) and after a (soft) HA restart switched to one.
My purpose is to count the DSL reconnects my monitoring binary_sensor.fritz_box_7590_connection.

Is this the best sensor to count DSL reconnects?
What would be the best to start counting after the normal nightly disconnect?

Here my code:

- platform: history_stats
  name: "DSL Disconnects"
  entity_id: binary_sensor.fritz_box_7590_connection
  state: "off"
  type: count
  start: "{{ now().replace(hour=3, minute=0) }}"
  duration:
    hours: 21

I have similar problem so I jumped into this topic.
I have a binary sensor binary_sensor.buderus_kessel_unter_betrieb that tracks when and for how long my Buderus furnace is operational meaning using gas fuel. I want to create sensor that will track this binanry sensor and count how long furnace was running in a day. Tried various combinations, even tried to use chatgpt to get this sensor working but nothing seems to work. So I get back to history stats from documentation. This is my code from sensors.yaml

- platform: history_stats
  name: Buderus On Time Today
  entity_id: binary_sensor.buderus_kessel_unter_betrieb
  state: "on"
  type: time
  start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
  end: '{{ now() }}'

I want that sensor_buderus_on_time_today calcualte how log is binanry sensor on for a day. Day is time from 00:00:00 to 23:59:59.
I created another sensor that will do estimate on gas usage like this

  sensors:
    gas_usage:
      friendly_name: "Gas Usage"
      unit_of_measurement: "m3"
      value_template: "{{ (states('sensor.buderus_on_time_today') | float * 0.4 * 60) | round(2) }}"

But I can’t get first calculation right.

I used wrong binary sensor …ahhh. Now its working.