rules question - variable not updated?

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
_Cyber_
Normal user
Posts: 113
Joined: 20 Oct 2019, 09:46

rules question - variable not updated?

#1 Post by _Cyber_ » 14 Dec 2020, 14:33

hi,

I am trying to set up a time and pulses counting mechanism for a GPIO state.

have currently the following rule:

Code: Select all

On System#Boot do
  Monitor GPIO,2
  timerSet,1,30
  Let,1,0 // counting seconds
  Let,2,0 // GPIO state
  Let,3,0 // counting pulses
  Let,4,0 // unixtime last secondcalculation
endon

On Clock#Time=All,00:01 do
  Let,1,0
  Let,3,0
EndOn

on GPIO#2=1 do
  if [VAR#2] = 0
    Let,2,1
    Let,3,[VAR#3]+1
    if [VAR#4] = 0
      Let,4,%unixtime%
    else
      Let,1,[VAR#1]+%unixtime%-[VAR#4]
    endif
    timerSet,1,2
  endif
endon

on GPIO#2=0 do
  if [VAR#2] = 1
    Let,2,0
    Let,1,[VAR#1]+%unixtime%-[VAR#4]
    Let,4,0
  endif
  timerSet,1,2
endon

On Rules#Timer=1 do
  if [Plugin#GPIO#Pinstate#2] = 1
    Let,1,[VAR#1]+%unixtime%-[VAR#4]
    Let,4,%unixtime%
  endif
  SendToHTTP,192.168.0.250,80,/humidity.php?mac=%mac%&rssi=%rssi%&temperature=[Temperature#Temperature]
  SendToHTTP,192.168.0.250,80,/apcucollector.php?smartswitch=%sysname%&ipv4=%ip%&uptime=%uptime%&load=%sysload%&freeram=%sysheap%&rssi=%rssi%&tempsens=[Temperature#Temperature]&rulesruntime=[Var#1]&rulespulses=[Var#3]
  timerSet,1,30
endon
now it is looking like the VAR#4 is not updated properly. the following log makes me thinking this:

Code: Select all

953015: HTTP: status,GPIO,2
959149: DS : Temperature: 26.31 (28-b0-46-56-b5-1-3c-ed)
959193: EVENT: Temperature#Temperature=16.31
961991: WD : Uptime 16 ConnectFailures 0 FreeMem 21448 WiFiStatus WL_CONNECTED ESPeasy internal wifi status: Conn. IP Init
966436: EVENT: Clock#Time=Mon,14:12
971752: EVENT: Rules#Timer=1,1
971789: ACT : SendToHTTP,192.168.0.250,80,/humidity.php?mac=EC:FA:BC:A7:CF:67&rssi=-71&temperature=16.31
971819: ACT : SendToHTTP,192.168.0.250,80,/apcucollector.php?smartswitch=Spieleraumheizung&ipv4=192.168.10.243&uptime=16&load=6.85&fre
971836: ACT : timerSet,1,30
979965: DS : Temperature: 26.31 (28-b0-46-56-b5-1-3c-ed)
979993: EVENT: Temperature#Temperature=16.31
983020: HTTP: status,GPIO,2
990953: HTTP: GPIO,2,1
990960: GPIO : port#2: set to 1
990994: EVENT: GPIO#2=1
991007: ACT : Let,2,1
991011: ACT : Let,3,2.00+1
991017: ACT : Let,4,1607951544
991025: ACT : timerSet,1,2
991533: HTTP: status,GPIO,2
991991: WD : Uptime 17 ConnectFailures 0 FreeMem 21448 WiFiStatus WL_CONNECTED ESPeasy internal wifi status: Conn. IP Init
993027: EVENT: Rules#Timer=1,1
993054: ACT : Let,1,512.00+1607951546-1607951488.00
993060: ACT : Let,4,1607951546
993070: ACT : SendToHTTP,192.168.0.250,80,/humidity.php?mac=EC:FA:BC:A7:CF:67&rssi=-71&temperature=16.31
993101: ACT : SendToHTTP,192.168.0.250,80,/apcucollector.php?smartswitch=Spieleraumheizung&ipv4=192.168.10.243&uptime=17&load=7.30&fre
993117: ACT : timerSet,1,30
1000781: DS : Temperature: 26.38 (28-b0-46-56-b5-1-3c-ed)
1000793: EVENT: Temperature#Temperature=16.38
1013014: HTTP: status,GPIO,2
1021597: DS : Temperature: 26.31 (28-b0-46-56-b5-1-3c-ed)
1021603: EVENT: Temperature#Temperature=16.31
1021991: WD : Uptime 17 ConnectFailures 0 FreeMem 21472 WiFiStatus WL_CONNECTED ESPeasy internal wifi status: Conn. IP Init
1023121: EVENT: Rules#Timer=1,1
1023149: ACT : Let,1,512.00+1607951576-1607951488.00
1023156: ACT : Let,4,1607951576
1023165: ACT : SendToHTTP,192.168.0.250,80,/humidity.php?mac=EC:FA:BC:A7:CF:67&rssi=-71&temperature=16.31
1023196: ACT : SendToHTTP,192.168.0.250,80,/apcucollector.php?smartswitch=Spieleraumheizung&ipv4=192.168.10.243&uptime=17&load=7.14&fre
1023213: ACT : timerSet,1,30
1026436: EVENT: Clock#Time=Mon,14:13
1042413: DS : Temperature: 26.31 (28-b0-46-56-b5-1-3c-ed)
1042493: EVENT: Temperature#Temperature=16.31
1043074: HTTP: status,GPIO,2
1051991: WD : Uptime 18 ConnectFailures 0 FreeMem 21472 WiFiStatus WL_CONNECTED ESPeasy internal wifi status: Conn. IP Init
1053216: EVENT: Rules#Timer=1,1
1053244: ACT : Let,1,640.00+1607951606-1607951616.00
1053250: ACT : Let,4,1607951606
1053259: ACT : SendToHTTP,192.168.0.250,80,/humidity.php?mac=EC:FA:BC:A7:CF:67&rssi=-70&temperature=16.31
1053291: ACT : SendToHTTP,192.168.0.250,80,/apcucollector.php?smartswitch=Spieleraumheizung&ipv4=192.168.10.243&uptime=18&load=7.04&fre
1053309: ACT : timerSet,1,30
1063229: DS : Temperature: 26.38 (28-b0-46-56-b5-1-3c-ed)
1063293: EVENT: Temperature#Temperature=16.38
1073040: HTTP: status,GPIO,2
1081991: WD : Uptime 18 ConnectFailures 0 FreeMem 21472 WiFiStatus WL_CONNECTED ESPeasy internal wifi status: Conn. IP Init
1083312: EVENT: Rules#Timer=1,1
1083364: ACT : Let,1,640.00+1607951636-1607951616.00
1083370: ACT : Let,4,1607951636
1083380: ACT : SendToHTTP,192.168.0.250,80,/humidity.php?mac=EC:FA:BC:A7:CF:67&rssi=-70&temperature=16.38
1083411: ACT : SendToHTTP,192.168.0.250,80,/apcucollector.php?smartswitch=Spieleraumheizung&ipv4=192.168.10.243&uptime=18&load=7.09&fre
1083427: ACT : timerSet,1,30
on line 991017 it is trying to set VAR#4 to 1607951544

on line 993054 that [VAR#4] is 1607951488.00 - I think this is from a previous run - Why? I reset it at GPIO state change to 0 to 0

on line 993060 it is trying to set VAR#4 to 1607951546

on line 1023149 [VAR#4] is still at 1607951488.00 - Why?

on line 1023156 it is trying to set VAR#4 to 1607951576

on line 1053244 now [VAR#4] is at 1607951616.00 - Why?

on line 1023156 it is trying to set VAR#4 to 1607951606

on line 1023149 [VAR#4] is still at 1607951616.00 - Why

did I do something wrong, or why it does not work?
have also tried to put the calculation in brackets e.g. Let,1,([VAR#1]+%unixtime%-[VAR#4]) - but this did not help.

I am using ESP_Easy_mega_20201102_normal_ESP8266_4M1M on a Wemos D1 mini.

Thanks!

BR
Alois

Micha_he
Normal user
Posts: 369
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: rules question - variable not updated?

#2 Post by Micha_he » 14 Dec 2020, 15:12

I mean, the value for your dummy-var is to large! I think 65535 is the maximum.

Edit: I already see, my post is nonsense...

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

Re: rules question - variable not updated?

#3 Post by TD-er » 14 Dec 2020, 17:17

Micha_he wrote: 14 Dec 2020, 15:12 I mean, the value for your dummy-var is to large! I think 65535 is the maximum.

Edit: I already see, my post is nonsense...
Not entirely nonsense...
The limit isn't 16 bit, but more like 22 bit as the values are internally treated as floats.
So when storing large values you will run into issues caused by the 'resolution' of float values.

This means you cannot store the unix time in a variable or at least not with a decent resolution.
For this reason we have a variable to give the nr of seconds of the current day.
See %syssec_d%

_Cyber_
Normal user
Posts: 113
Joined: 20 Oct 2019, 09:46

Re: rules question - variable not updated?

#4 Post by _Cyber_ » 14 Dec 2020, 17:57

thanks. :-)
exchanged to %syssec_d% and everything is working as expected. :-)

BR
Alois

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

Re: rules question - variable not updated?

#5 Post by GravityRZ » 12 Feb 2021, 18:43

Nice.
i was doing something similar with %unixtime% but did not have any problems at all
just to be save i exchanged it for %syssec_d%

:D
TD-er wrote: 14 Dec 2020, 17:17
Micha_he wrote: 14 Dec 2020, 15:12 I mean, the value for your dummy-var is to large! I think 65535 is the maximum.

Edit: I already see, my post is nonsense...
Not entirely nonsense...
The limit isn't 16 bit, but more like 22 bit as the values are internally treated as floats.
So when storing large values you will run into issues caused by the 'resolution' of float values.

This means you cannot store the unix time in a variable or at least not with a decent resolution.
For this reason we have a variable to give the nr of seconds of the current day.
See %syssec_d%

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

Re: rules question - variable not updated?

#6 Post by TD-er » 12 Feb 2021, 22:21

I did increase the internal resolution of variables only a few weeks ago, so it will probably only be in the last nightly build or maybe one build before that one.
Before the internal variables were a float (22 bit "accuracy" for storing an int)
Now they are stored as a double, so you will not loose any resolution if you try to store a 32 bit int (or uint) in a variable.

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

Re: rules question - variable not updated?

#7 Post by GravityRZ » 14 Feb 2021, 12:33

i am using 20210114 build.

if it is not in that build than i still do not experience problems.
looking at the system variables it seems to store the entire %unixtime% value

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

Re: rules question - variable not updated?

#8 Post by TD-er » 14 Feb 2021, 13:09

A simple test would be to store the unixtime in a variable, set a timer on 1 second and store unix time in the next variable.
If the difference is 1, then you have the double feature :)

Another option could be to store unix time and subtract (or add) 0.1 from it.
If it stores the same unix time with the expected difference, then you also know you have the double resolution.

_Cyber_
Normal user
Posts: 113
Joined: 20 Oct 2019, 09:46

Re: rules question - variable not updated?

#9 Post by _Cyber_ » 14 Feb 2021, 23:16

GravityRZ wrote: 14 Feb 2021, 12:33 i am using 20210114 build.

if it is not in that build than i still do not experience problems.
looking at the system variables it seems to store the entire %unixtime% value
me not. I am on 20210114 too:

following codesnippet:

Code: Select all

 Let,11,24 // shutter unixtime start
 event,ControlShutter=%eventvalue%
endon
and in ControlShutter:

Code: Select all

   Let,%v11%,%unixtime%
   logentry,"%v11% is [VAR#%v11%]"
this produces the following log:

Code: Select all

4981432: ACT : Let,24,1613319127
4981439: ACT : logentry,'24 is 1613319168'
4981441: 24 is 1613319168
and, a moment later (where I changed to Let,11,39)

Code: Select all

4983163: ACT : Let,39,1613319130
4983170: ACT : logentry,'39 is 1613319168'
4983172: 39 is 1613319168
[code]

so, by whatever reason it is logged correct, but assigned wrong.
also in system variables I can see all 7 variables which should have the most recent timestamp in the same timestamp instead. And, pushing the variables via HTTP show the wrong values...

btw., is there any variable which reflects the runningtime timestamp which I can see in the logs?

BR
Alois

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

Re: rules question - variable not updated?

#10 Post by GravityRZ » 15 Feb 2021, 08:57

that is exactly what is did.
for me it is working but i use only a couple of variables
TD-er wrote: 14 Feb 2021, 13:09 A simple test would be to store the unixtime in a variable, set a timer on 1 second and store unix time in the next variable.
If the difference is 1, then you have the double feature :)

Another option could be to store unix time and subtract (or add) 0.1 from it.
If it stores the same unix time with the expected difference, then you also know you have the double resolution.

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

Re: rules question - variable not updated?

#11 Post by GravityRZ » 15 Feb 2021, 08:59

GravityRZ wrote: 15 Feb 2021, 08:57 that is exactly what is did.
for me it is working but i use only a couple of variables
@ _Cyber_
maybe the problem is that you are using variables above 10.

i remember reading a post where this was a problem(it should not be)


TD-er wrote: 14 Feb 2021, 13:09 A simple test would be to store the unixtime in a variable, set a timer on 1 second and store unix time in the next variable.
If the difference is 1, then you have the double feature :)

Another option could be to store unix time and subtract (or add) 0.1 from it.
If it stores the same unix time with the expected difference, then you also know you have the double resolution.

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

Re: rules question - variable not updated?

#12 Post by TD-er » 15 Feb 2021, 09:36

A recent commit removed the limit of the variable count.
It was 16 before, but now I use a map for variables, so you can use any unsigned int value as variable index.
Just remember each variable takes at least 12 bytes (8 for the double, 4 for the key), so removing this limit may cause memory issues if you do use a lot of variables.
It is more useful for those hitting the limit of 16 and/or those who want to group variables.

If a variable was never assigned (via "let" command) then the returning value will be 0.0

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 25 guests