Need precise seconds of switch ON state from history statistics

I used this alternative (splitted into two rules and then calculated in home assistant):

Rule1 ON Energy#Power>0 DO Backlog Publish stat/loader/on {"Time":"%timestamp%"}; Rule2 1 ENDON 
Rule2 ON Energy#Power==0 DO Backlog Publish stat/loader/off {"Time":"%timestamp%"}; Rule2 0 ENDON
1 Like

@novitibo yes I saw that above and thats what I am going to try out when I get time (I like how you use the rule to change whether the other rule is on or not as well).

But what I was looking for was how you were using the below snippet you mentioned?
And if you could post the whole rule it was in?

Cheers!

Example:

ON wifi#disconnected DO Var1 %timestamp% ENDON
ON wifi#connected DO Var2 %timestamp% ENDON
ON mqtt#connected DO Publish stat/topic/BLACKOUT {"From":"%Var1%","To":"%Var2%"} ENDON

@novitibo It works with a very basic trigger:

ON POWER1#state=0 DO Var1 %timestamp% ENDON

and in the console I then see

14:43:34 RUL: POWER1#STATE=0 performs "Var1 2019-10-21T14:43:34"
14:43:34 MQT: stat/sonoff33/RESULT = {"Var1":"2019-10-21T14:43:34"}

So I moved on and have now developed this:

`
ON POWER1#state=1 DO Var2 %timestamp% ENDON
ON POWER1#state=0 DO Var1 %timestamp% ENDON
ON Rules#Timer=1 DO Var1 ENDON
ON Rules#Timer=1 DO Var2 ENDON

`
NOTE1 Above is all ‘appended’ to become one rule
NOTE2: This is just testing on a movement sensor I have which is good because its outside and the wind is turning it on and off atm…very handy!
NOTE3: I use the ruletimer as a trigger so I can manually trigger it by inputting:

ruletime 3 at the console…then that just quickly counts down to ‘1’

Sonoff Web Console output:
PS: Dont be confused by the other output sending EVENT’s to sonnoff13…just look for the Var1 and Var2 output

17:18:22 MQT: stat/sonoff33/POWER1 = ON
17:18:22 RUL: POWER1#STATE=1 performs "backlog publish cmnd/sonoff13/Event Movement_Patio"
17:18:22 RUL: POWER1#STATE=1 performs "Var2 2019-10-21T17:18:22"
17:18:22 MQT: stat/sonoff33/RESULT = {"Var2":"2019-10-21T17:18:22"}
17:18:22 MQT: cmnd/sonoff13/Event = Movement_Patio
17:18:24 MQT: stat/sonoff33/RESULT = {"POWER1":"OFF"}
17:18:24 MQT: stat/sonoff33/POWER1 = OFF
17:18:25 RUL: POWER1#STATE=0 performs "Var1 2019-10-21T17:18:25"
17:18:25 MQT: stat/sonoff33/RESULT = {"Var1":"2019-10-21T17:18:25"}
17:18:46 MQT: stat/sonoff33/RESULT = {"POWER1":"ON"}
17:18:46 MQT: stat/sonoff33/POWER1 = ON
17:18:46 RUL: POWER1#STATE=1 performs "backlog publish cmnd/sonoff13/Event Movement_Patio"
17:18:46 RUL: POWER1#STATE=1 performs "Var2 2019-10-21T17:18:46"
17:18:46 MQT: stat/sonoff33/RESULT = {"Var2":"2019-10-21T17:18:46"}
17:18:46 MQT: cmnd/sonoff13/Event = Movement_Patio
17:18:48 MQT: stat/sonoff33/RESULT = {"POWER1":"OFF"}
17:18:48 MQT: stat/sonoff33/POWER1 = OFF
17:18:48 RUL: POWER1#STATE=0 performs "Var1 2019-10-21T17:18:48"
17:18:48 MQT: stat/sonoff33/RESULT = {"Var1":"2019-10-21T17:18:48"}
17:19:19 RSL: tele/sonoff33/STATE = {"Time":"2019-10-21T17:19:19","Uptime":"7T02:20:30","Vcc":3.462,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"POWER1":"OFF","POWER2":"OFF","POWER3":"OFF","POWER4":"OFF","POWER5":"OFF","Wifi":{"AP":1,"SSId":"Bugwire","BSSId":"E0:3F:49:72:B8:E0","Channel":6,"RSSI":92,"LinkCount":10,"Downtime":"0T00:00:35"}}
17:19:19 MQT: tele/sonoff33/SENSOR = {"Time":"2019-10-21T17:19:19","Switch1":"OFF","Switch5":"OFF"}17:34:00 MQT: stat/sonoff33/RESULT = {"Var2":"2019-10-21T17:34:00"}
17:34:00 MQT: cmnd/sonoff13/Event = Movement_Patio
17:34:02 MQT: stat/sonoff33/RESULT = {"POWER1":"OFF"}
17:34:02 MQT: stat/sonoff33/POWER1 = OFF
17:34:02 RUL: POWER1#STATE=0 performs "Var1 2019-10-21T17:34:02"
17:34:02 MQT: stat/sonoff33/RESULT = {"Var1":"2019-10-21T17:34:02"}
17:34:19 RSL: tele/sonoff33/STATE = {"Time":"2019-10-21T17:34:19","Uptime":"7T02:35:30","Vcc":3.462,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"POWER1":"OFF","POWER2":"OFF","POWER3":"OFF","POWER4":"OFF","POWER5":"OFF","Wifi":{"AP":1,"SSId":"Bugwire","BSSId":"E0:3F:49:72:B8:E0","Channel":6,"RSSI":100,"LinkCount":10,"Downtime":"0T00:00:35"}}
17:34:19 MQT: tele/sonoff33/SENSOR = {"Time":"2019-10-21T17:34:19","Switch1":"OFF","Switch5":"OFF"}
17:35:30 MQT: stat/sonoff33/RESULT = {"POWER1":"ON"}
17:35:30 MQT: stat/sonoff33/POWER1 = ON
17:35:30 RUL: POWER1#STATE=1 performs "backlog publish cmnd/sonoff13/Event Movement_Patio"
17:35:30 RUL: POWER1#STATE=1 performs "Var2 2019-10-21T17:35:30"
17:35:30 MQT: stat/sonoff33/RESULT = {"Var2":"2019-10-21T17:35:30"}
17:35:30 MQT: cmnd/sonoff13/Event = Movement_Patio
17:35:32 MQT: stat/sonoff33/RESULT = {"POWER1":"OFF"}
17:35:32 MQT: stat/sonoff33/POWER1 = OFF
17:35:32 RUL: POWER1#STATE=0 performs "Var1 2019-10-21T17:35:32"
17:35:32 MQT: stat/sonoff33/RESULT = {"Var1":"2019-10-21T17:35:32"}
17:39:19 RSL: tele/sonoff33/STATE = {"Time":"2019-10-21T17:39:19","Uptime":"7T02:40:30","Vcc":3.462,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"POWER1":"OFF","POWER2":"OFF","POWER3":"OFF","POWER4":"OFF","POWER5":"OFF","Wifi":{"AP":1,"SSId":"Bugwire","BSSId":"E0:3F:49:72:B8:E0","Channel":6,"RSSI":90,"LinkCount":10,"Downtime":"0T00:00:35"}}
17:39:19 MQT: tele/sonoff33/SENSOR = {"Time":"2019-10-21T17:39:19","Switch1":"OFF","Switch5":"OFF"}
17:39:22 MQT: stat/sonoff33/RESULT = {"POWER1":"ON"}
17:39:22 MQT: stat/sonoff33/POWER1 = ON
17:39:22 RUL: POWER1#STATE=1 performs "backlog publish cmnd/sonoff13/Event Movement_Patio"
17:39:22 RUL: POWER1#STATE=1 performs "Var2 2019-10-21T17:39:22"
17:39:22 MQT: stat/sonoff33/RESULT = {"Var2":"2019-10-21T17:39:22"}
17:39:23 MQT: cmnd/sonoff13/Event = Movement_Patio
17:39:25 MQT: stat/sonoff33/RESULT = {"POWER1":"OFF"}
17:39:25 MQT: stat/sonoff33/POWER1 = OFF
17:39:25 RUL: POWER1#STATE=0 performs "Var1 2019-10-21T17:39:25"
17:39:25 MQT: stat/sonoff33/RESULT = {"Var1":"2019-10-21T17:39:25"}
17:40:24 MQT: stat/sonoff33/RESULT = {"POWER1":"ON"}
17:40:24 MQT: stat/sonoff33/POWER1 = ON
17:40:24 RUL: POWER1#STATE=1 performs "backlog publish cmnd/sonoff13/Event Movement_Patio"
17:40:24 RUL: POWER1#STATE=1 performs "Var2 2019-10-21T17:40:24"
17:40:24 MQT: stat/sonoff33/RESULT = {"Var2":"2019-10-21T17:40:24"}
17:40:25 MQT: cmnd/sonoff13/Event = Movement_Patio
17:40:27 MQT: stat/sonoff33/RESULT = {"POWER1":"OFF"}
17:40:27 MQT: stat/sonoff33/POWER1 = OFF
17:40:27 RUL: POWER1#STATE=0 performs "Var1 2019-10-21T17:40:27"
17:40:27 MQT: stat/sonoff33/RESULT = {"Var1":"2019-10-21T17:40:27"}
17:40:38 MQT: stat/sonoff33/RESULT = {"POWER1":"ON"}
17:40:38 MQT: stat/sonoff33/POWER1 = ON
17:40:38 RUL: POWER1#STATE=1 performs "backlog publish cmnd/sonoff13/Event Movement_Patio"
17:40:38 RUL: POWER1#STATE=1 performs "Var2 2019-10-21T17:40:38"
17:40:38 MQT: stat/sonoff33/RESULT = {"Var2":"2019-10-21T17:40:38"}
17:40:38 MQT: cmnd/sonoff13/Event = Movement_Patio
17:40:40 MQT: stat/sonoff33/RESULT = {"POWER1":"OFF"}
17:40:40 MQT: stat/sonoff33/POWER1 = OFF
17:40:40 RUL: POWER1#STATE=0 performs "Var1 2019-10-21T17:40:40"
17:40:40 MQT: stat/sonoff33/RESULT = {"Var1":"2019-10-21T17:40:40"}

Triggering with ruletimer 3

08:32:28 CMD: ruletimer 3
08:32:28 MQT: stat/sonoff33/RESULT = {"T1":3,"T2":0,"T3":0,"T4":0,"T5":0,"T6":0,"T7":0,"T8":0}
08:32:31 RUL: RULES#TIMER=1 performs "Var1"
08:32:31 MQT: stat/sonoff33/RESULT = {"Var1":"2019-10-22T08:31:18"}
08:32:31 RUL: RULES#TIMER=1 performs "Var2"
08:32:31 MQT: stat/sonoff33/RESULT = {"Var2":"2019-10-22T08:31:15"}

Example misleading

I believe the example is misleading in how this must be written up as a rule.
I have worked on this since last night and just finally figured out how to do it a short while ago. Yeah…I’m a bit slow!

Have a go yourself and see if you can use this as I can see Var and Mem values being useful (Mem retains over a reboot) which is very handy I think.

Maths?

Also…it appears that maths is not possible BUT I HOPE I’M WRONG ABOUT THAT?

I am also interested in measuring duration in seconds. I have a well pump monitor, and measure the amount of time the pump is on. This can be converted (approximately) to litres of water pumped, but it would only be useful if I can measure seconds and not 0.01 hour resolution.

It would be most useful if the history_stats could be configured to do seconds.

I have now created a patch to history_stats giving the option of returning the history time in seconds and then made a pull request. Hopefully this will get in to a future release soon.

For applications like the one described in this thread, or my own pump monitor where you have to calibrate liquid volume flow, and then multiply by a higher resolution value, this will be useful.

Decimal hours was not a good option for me.

Hi all, I changed the history_stats in seconds, and copied it in the custom components folder, but my configuration still won’t accept this file i keep getting messages, : "Platform error sensor.history_stats_in_seconds - No module named ‘custom_components.history_stats_in_seconds.sensor’; ‘custom_components.history_stats_in_seconds’ is not a package.
can you point me in the right direction i am fairly new to HA

This was never incorporated into HA. I’m afraid I messed up my pull request and need to do it again.

1 Like

Thanks, I would very appreciate that

Looking for more accurate control of history_stats. In my case, I created a sensor to count number of “on” for a motion sensor in the past 10 minutes as below:

- platform: history_stats
  name: RingMotion10m
  entity_id: binary_sensor.ring_motion
  state: "on"
  type: count
  end: "{{ now() }}"
  duration: 00:10:00

It works but not that accurate, when the ring_motion reported on, the RingMotion10m value doesn’t change immediately, it has up to 30 seconds (sometime less and sometime more) of delay based on my observations. I think this is related to how history_stats works at 0.01 hour accuracy?

5/16 12:17:21 PM Ring Motion on
5/16 12:17:51 PM RingMotion10m 1

This feature request might interest you?

2 Likes

How is this done now, when history_stats is integrated into core and history_stats.py seems not to exist anymore, at least, not that I could find it?

In other words: How can I get seconds from history stats, NOT hours in two decimal accuracy, which is not accurtae enough???

2 Likes

@cowboy this is exactly what I need… I’m not very clued up on where the files are and how to find them though!! Any chance you can help me with finding my current histor_stats file? I know how to copy etc, but just can’t for the life of me find the file.

Screen Shot 2022-07-18 at 8.05.05 PM

I use the terminal add-on to homeassistant for the command line.

I know this is an old thread… but thought it was worth a try

So I have spent the last couple of hours trying to do this… no joy. I was able to copy the history_stats directory to my custom_components directory, but no history_stats.py is in that directory, and when searching the full bin, I was not able to find it anywhere in my install. How can I get this custom component working @petro

Thanks in advance

If you don’t know python code, I don’t suggest you use it. I copied/pasted and modified the code. The codebase has probably change as well as the API calls. I recommend going another route.

1 Like

Thanks, very informative answer.

Hi, Can someone help me how to make this custom component? Wil there be history component with seconds option in future ha?

I am new to ha, but I can program.

Regards, Ales

Would be so good to have this in HA :slight_smile:

My pull request Stop rounding history_stats sensor by amosyuen · Pull Request #97195 · home-assistant/core · GitHub just got merged which should stop rounding the sensor. The default precision is still 0.01, but you can fix that by changing the entity setting. Either set the time unit to seconds or change the precision to 0.0001.