rules calculate time difference in milliseconds

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
GravityRZ
Normal user
Posts: 206
Joined: 23 Dec 2019, 21:24

rules calculate time difference in milliseconds

#1 Post by GravityRZ » 22 Jan 2020, 18:55

is there a way to save the time in milliseconds so i can compare it with an earlier variable

i want to calculate the time it takes between 2 events but need it in millisecond accuracy(eg 1 second is not accurate enough)

so basically i want

on trigger 1
save seconds and milliseconds in variable A

on trigger 2
save seconds and milliseconds in variable B

substract B from A to get the exact time between 2 events

if %sysmillisec% would have existed i could use thast one but i only see %syssec%
also total time can not be saved with millisecond accuracy

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: rules calculate time difference in milliseconds

#2 Post by grovkillen » 22 Jan 2020, 19:27

Same here, only seconds officially.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: rules calculate time difference in milliseconds

#3 Post by TD-er » 22 Jan 2020, 19:58

Well if it is for debugging purposes, then you could have a look at the logs, where things are displayed in msec resolution.
Currently we don't have something to be used in rules that can handle msec resolution.

Also the rules parsing itself may take some time, so the timestamp of the sample should be recorded and made available somehow, as it is rather useless to determine a timestamp in msec in the rules.
The fluctuation in rules execution is in the order of a second and not msec.

Some plugins or controllers may block execution of other code for some time.
It would be great if all of these blocking code parts don't block for > 100 msec, but as you may see in the timing stats page, there are quite a few parts that may take > 100 msec (highlighted in that page)
These blocks also have effect on the rules execution.

GravityRZ
Normal user
Posts: 206
Joined: 23 Dec 2019, 21:24

Re: rules calculate time difference in milliseconds

#4 Post by GravityRZ » 26 Jan 2020, 20:35

ok clear.

i am doning the timing now in domoticz and calcultate the time difference between 2 SendtoHTTP calls
i notice however that there is a 1 second delay between the same calls

the first time the SendtoHTTP is 1 second different from the second

it fluctuates on a regular base
it loops until the counter has reached 4 and then does a SendHTTP to domoticz yo update the watermeter by 4 liters


call 1 17 seconds
call 2 18 seconds
call 3 17 seconds
call 4 18 seconds

below is the code
what could be causing this 1 second delay

Watermeter(device 1) updates every second
Liters(device 3), every 60 seconds

On System#Boot do // When the ESP boots, do
TaskValueSet 3,1,0 // TaskValueSet TASKnr,VARnr,Value, Reset the Liters counter to 0
EndOn

On Watermeter#Count do // When Pulse is detected
if [Watermeter#Count] > 0
TaskValueSet 3,1,[Liters#Counter]+1 // Set the Pulse to the Liters dummy sensor
endif
if [Liters#Counter] = 4 // send every 4 liters to domoticz for better resolution
SendToHTTP 192.168.1.50,8084,/json.htm?type=command&param=udevice&idx=337&nvalue=0&svalue=4
TaskValueSet 3,1,0
endif
EndOn

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: rules calculate time difference in milliseconds

#5 Post by TD-er » 26 Jan 2020, 23:08

what could be causing this 1 second delay
My guess would be the call to sending data to the controller.
Please have a look at the controller settings.

And timing on the receiving end will not give you any usable indication of the time difference between samples.
There are way too many unknowns affecting the latency on the receiving end.

GravityRZ
Normal user
Posts: 206
Joined: 23 Dec 2019, 21:24

Re: rules calculate time difference in milliseconds

#6 Post by GravityRZ » 27 Jan 2020, 16:01

ok clear.

i will change the rules so i do the calculation inside the ESP(on a 1 second base)
i can then see if the flow readings are more consistent

UPDATE******
flow is more consistent but on a 1 liter resolution of course
Also used a second variable to reset the flow after 60 seconds if water usage is zero.
watermeter gets updated instantly
flowmeter every 60 seconds

Code: Select all

//------------ INITIAL RELEASE
On System#Boot do     						
   TaskValueSet 3,1,0
   TaskValueSet 3,2,0						
   TimerSet,1,60      						
EndOn
 
On Watermeter#Count do						
	if [Watermeter#Count] > 0
		TaskValueSet 3,1,[Liters#Counter]+1		
		SendToHTTP 192.168.1.50,8084,/json.htm?type=command&param=udevice&idx=337&nvalue=0&svalue=1
	endif
EndOn

On Rules#Timer=1 do  						
	if [Liters#Counter] > 0	or [Liters#Previous] > 0	
		SendToHTTP 192.168.1.50,8084,/json.htm?type=command&param=udevice&idx=338&nvalue=0&svalue=[Liters#Counter]
		TaskValueSet 3,2,[Liters#Counter]		
                TaskValueSet 3,1,0				
	endif
	TimerSet,1,60      					
Endon

GravityRZ
Normal user
Posts: 206
Joined: 23 Dec 2019, 21:24

Re: rules calculate time difference in milliseconds

#7 Post by GravityRZ » 31 Jan 2020, 08:43

i noticed that the time variable from the pulse counter is actually showing the time between pulses in ms.

i am now taking that number to calculate the flow in liters

e.g. 60000/ Time= flow

seems to work very accurate and also the number correspond with the flow i expect.

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: rules calculate time difference in milliseconds

#8 Post by TD-er » 03 Feb 2020, 14:51

Can you give the relevant part of your rules?
I am not entirely sure I understand where the time value comes from.

GravityRZ
Normal user
Posts: 206
Joined: 23 Dec 2019, 21:24

Re: rules calculate time difference in milliseconds

#9 Post by GravityRZ » 03 Feb 2020, 15:19

sure,

here is the code
On Watermeter#Count do // When Pulse is detected
if [Watermeter#Count] > 0
SendToHTTP 192.168.1.50,8084,/json.htm?type=command&param=udevice&idx=337&nvalue=0&svalue=1
TaskValueSet 3,3,60000/[Watermeter#Time]
SendToHTTP 192.168.1.50,8084,/json.htm?type=command&param=udevice&idx=338&nvalue=0&svalue=[Liters#Flow]
endif
the time part is coming form a pulse counter where it is the Time part (pulsecounter which sents Delta/Total/Time)

works really well and i do not need to count pulses in a 60 second interval but know instantly what the flow is

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: rules calculate time difference in milliseconds

#10 Post by TD-er » 03 Feb 2020, 15:25

Ah OK, that's nice indeed and very useful. (as in we should add it to the documentation as an example :) )

GravityRZ
Normal user
Posts: 206
Joined: 23 Dec 2019, 21:24

Re: rules calculate time difference in milliseconds

#11 Post by GravityRZ » 03 Feb 2020, 18:15

indeed we should
here is the complete script(i left some stuff in so that it is backwards compatible in case i want to start counting pulses again every 60 seconds
On System#Boot do // When the ESP boots, do
TaskValueSet 3,1,0 // TaskValueSet TASKnr,VARnr,Value, Reset the Liters counter to 0
TaskValueSet 3,2,0 // TaskValueSet TASKnr,VARnr,Value, Reset the PreviousLiters counter to 0
TaskValueSet 3,3,0 // TaskValueSet TASKnr,VARnr,Value, Reset the Flow counter to 0
TaskValueSet 3,4,0 // TaskValueSet TASKnr,VARnr,Value, Reset the PreviousFlow counter to 0

TimerSet,1,30 // Set Timer 1 for the next event in 30 seconds
EndOn

On Watermeter#Count do // When Pulse is detected
if [Watermeter#Count] > 0
SendToHTTP 192.168.1.50,8084,/json.htm?type=command&param=udevice&idx=337&nvalue=0&svalue=1
TaskValueSet 3,3,60000/[Watermeter#Time]
SendToHTTP 192.168.1.50,8084,/json.htm?type=command&param=udevice&idx=338&nvalue=0&svalue=[Liters#Flow]
endif
EndOn

On Rules#Timer=1 do // When Timer 1 expires, do
if [Liters#Flow] > 0 or [Liters#PreviousFlow] > 0 // Only send value if amount of Liters > 0
SendToHTTP 192.168.1.50,8084,/json.htm?type=command&param=udevice&idx=338&nvalue=0&svalue=[Liters#Flow]
TaskValueSet 3,4,[Liters#Flow] // set flow to previous counter
TaskValueSet 3,3,0
endif
TimerSet,1,30 // Set Timer 1 for the next event in 30 seconds
Endon
interval of the pulse counter is 1 second

Post Reply

Who is online

Users browsing this forum: No registered users and 28 guests