Statistics based helper on PIR sensor

I created statistics based helper that uses data from my simple PIR sensor. My first helper counts number of on state during last hour. It works ok.

I want to calculate also how much time during last hour sensor was on. I use helper with “Average step” for that. This one does not work as expected. Why the graph looks so strange?

Here is pure sensor history:
Zrzut ekranu 2024-10-10 141800

That is what the History Stats sensor is for.

Gaps in the graph can occur when HA is reloaded/restarted, when source sensors or devices are unavailable or unknown, or when the underlying math of the sensor produces errors (for example, division by 0).

I thought that Statistics are suitable for my case but I will try with your suggestion. After creation (in UI) as helper can I see it’s “code” somewhere? I will observe its graph now to see if it shows what I expect.

Not easily. But History Stats is not UI-only, you can still create sensors in YAML instead of using the Helper menu.

In fact I assumed that I can but hoped that even those from UI are in some config file and can be used as a base. However, they probably are stored in some database which is not so easily accessible.

Unfortunately my sensor keeps showing 0.


Probably I have to put something in “state” (“stan” on my screenshot). However, I do not know what. Probably value that my sensor reports as on but where can I find exact value/string?
I am also not so sure about this config:

image

You absolutely have to add a state value… it’s required. For a binary sensor like your motion sensor you probably want to use on.

For the start and end time I would use the following:

Start:
{{ now() - timedelta(hour=1) }}

End:
{{ now() }}