• 10 dec 2017: forum version update. In case of issues use this topic.
  • 30 nov 2017: pilight moved servers. In case of issues use this topic.
Hello There, Guest! Login Register


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Done] Eventing - operators MOD and DIV
#1
I currently have some rules whit actions to be triggered at a time with an offset to the current time (or sunset/sunrise time). Doing this, one has to take into account that minute- and/or hour values can over- or underflow. Currently this is done with IF - OR constructions, but in situations where both minutes and hours could over- or underflow this is leading to rather complex rules.

Situations like this could be handled much easier using DIV (integer division) and MOD (modulus) operators.

Simple example:
Current rule:
Code:
IF ((datetime.minute  - 30) >= 0 AND sunriseset.sunset == (datetime.hour + (datetime.minute - 30) / 100)) OR ((datetime.minute  - 30)  < 0 AND sunriseset.sunset == (datetime.hour - 1) + (datetime.minute - 30 + 60) / 100) THEN ...
With MOD and DIV
Code:
IF (datetime.hour + ((datetime.minute - 30) DIV 60 ) MOD 24) + (((datetime.minute -30) MOD 60) /100) == sunrise.sunset THEN ...
This rule would work with any value added to or subtracted from hours and or minutes. So I would be very pleased if the DIV and MOD operators could be added.
 
Reply
#2
You can try adding yourself. Creating a new operator module is rather easy Smile I want to fix some other stuff first before adding new operators.
Example:
https://github.com/pilight/pilight/blob/...s/divide.c

I would suggest chaning:
MOD -> %
DIV -> \

So
Code:
IF (datetime.hour + ((datetime.minute - 30) \ 60 ) % 24) + (((datetime.minute -30) % 60) /100) == sunrise.sunset THEN ...
 
Reply
#3
I will give it a try.

Can I build operators as modules that the daedom will load at startup, just like protocols?
 
Reply
#4
Yep, should work.
 
Reply
#5
(01-09-2015, 01:17 PM)curlymo Wrote: You can try adding yourself. Creating a new operator module is rather easy Smile I want to fix some other stuff first before adding new operators.
Example:
https://github.com/pilight/pilight/blob/...s/divide.c

I would suggest chaning:
MOD -> %
DIV -> \

So
Code:
IF (datetime.hour + ((datetime.minute - 30) \ 60 ) % 24) + (((datetime.minute -30) % 60) /100) == sunrise.sunset THEN ...

I think that \ (backslash) is interpreted as escape character in JSON. So you will have to use \\ (double backslash in the rule in config.json, or we must choose a different symbol (or "DIV" maybe?)
 
Reply
#6
Good point, but i think we should still use it. If we would eventually create a events generator, it's better to have proper naming and symboling.
 
Reply
#7
I have created the DIV (\) and MOD (%) operators. That was indeed quite simple. I still must test the results, but i ran into something I cant explain.

The rule
Code:
"ÍF (datetime.hour + (datetime.minute - 30) \\ 60 ) % 24 + (((datetime.minute - 30) \\ 60) / 100) == sunriseset.sunset THEN switch DEVICE tuin1 TO on"

Gives me an error that operator 24 doen't exist.)
But if I swap both terms of the comparison like this
Code:
"IF sunriseset.sunset == (datetime.hour + (datetime.minute - 30) \\ 60 ) % 24 + (((datetime.minute - 30) \\ 60) / 100) THEN switch DEVICE tuin1 TO on"

I don't get that error.
 
Reply
#8
And this:
Code:
"ÍF ((datetime.hour + (datetime.minute - 30) \\ 60 ) % 24) + (((datetime.minute - 30) \\ 60) / 100) == sunriseset.sunset THEN switch DEVICE tuin1 TO on"
 
Reply
#9
Code:
"tuinverlichtingAan": {
                        "rule": "IF ((datetime.hour + (datetime.minute - 30) \\ 60 ) % 24) + (((datetime.minute - 30) \\ 60) / 100) == sunriseset.sunset THEN switch DEVICE tuin1 TO on",
                        "active": 1
                },
Code:
[....] Starting : pilight[Jan 09 23:59:49:144389] pilight-daemon: ERROR: rule #1 invalid: operator 24 does not exist
failed!
\
 
Reply
#10
Can you post the source of the modules?
 
Reply
  


Possibly Related Threads...
Thread Author Replies Views Last Post
  [Done] pilight eventing - dimming step by step terrar 3 2,861 02-11-2015, 02:39 PM
Last Post: Niek
  [Done] pilight-cron (implemented with eventing) dzjaivnt 30 19,056 04-28-2014, 11:48 PM
Last Post: dzjaivnt
  [Done] Eventlist (implemented with eventing) JurnD 18 7,426 01-16-2014, 08:39 PM
Last Post: curlymo

Forum Jump:


Browsing: 1 Guest(s)