Count "on"-time of a binary-sensor

Hey folks,

how can I count during one day the time a binary-sensor is “on”?

change type: time to type: count in the example

I thougt allready about this, but I don’ know exactly how to do…

I think I should read it again.

literally copy the example and paste it into your configuration in the sensor section. Change the source entity_id, then change type: time to type: count.

Okay, I made these sensors:


  - platform: history_stats
    name: "Brennerstatistik Tag"
    entity_id: binary_sensor.heizungsbrenner
    state: "on"
    type: count
    start: "{{ now().replace(hour=0, minute=0) }}"
    end: "{{ now().replace(hour=23, minute=59) }}"
  - platform: history_stats
    name: "Brenndauerstatistik Tag"
    entity_id: binary_sensor.heizungsbrenner
    state: "on"
    type: time
    start: "{{ now().replace(hour=0, minute=0) }}"
    end: "{{ now().replace(hour=23, minute=59) }}"

And get these results:

“Brennerstatistik Tag” is a sensor that I allready had to count the switches from “off” to “on”
It also has the timevalue. Why can’t I use this?

The Timevalue is to high anyway.
Today it must be something about 195 minutes

…and how can I show the time in minutes?

You’re end is wrong. You can’t get information in the future and that will screw up your time. It should be exactly like the example, where end is just "{{ now() }}". You can also use "{{ today_at('00:00') }}" for the start to get exactly midnight.

to get your result in minutes, multiply the result by 60 in a template sensor.

how must I do this

create a template sensor, this is a close example of what you need to do

Oh no!
Templates are the worst thint to me. :frowning:

all you need to do is copy/paste that example and change the entity_id and the number after the * to 60.

where do I have to put this template?

All examples in the documentation are assumed to go into configuration.yaml unless otherwise specified.

1 Like

Okay, I’ll try

Is it somehow possible to access the value attribute of a history_stats sensor?

Yes, also through templates

or if you don’t want to deal with templates, use a special entity row in an entities card

I get this error in my logs:

Logger: homeassistant.helpers.template
Source: helpers/template.py:1291
First occurred: 19:48:39 (2 occurrences)
Last logged: 19:48:39

Template warning: 'float' got invalid input 'unknown' when rendering template '{{ states('sensor.brenndauer_tag')|float * 60 }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1

yep, that’s covered here

I don’t understand anything :frowning:

Okay, I wrote '{{ states('sensor.brenndauer_tag')|float(0) * 60 }}'

Now the error is gone

1 Like