Modulo operation in rules

Moderators: grovkillen, Stuntteam, TD-er

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

Modulo operation in rules

#1 Post by Micha_he » 14 Mar 2021, 10:43

Has someone a tip, how I can use the modulo-operator/-function (mod(x,y) or {mod:x:y} ) in a rule if-condition?
I can find help in the docs...

Thanks

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

Re: Modulo operation in rules

#2 Post by TD-er » 14 Mar 2021, 10:48

If computation like actions do not seem to work, you can always try to store it first in a variable using the "let" command.
See some examples here: https://espeasy.readthedocs.io/en/lates ... -functions

The problem is that functions may sometimes need several passes to get parsed completely and that's not always done right.
So when trying to make complex computations (e.g. call a function with arguments which are a result of a computation) it is best to store it in a variable first.

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

Re: Modulo operation in rules

#3 Post by Micha_he » 14 Mar 2021, 11:13

But on the linked page, isn't a example for mod, or?

And the rule

Code: Select all

Let,4,{mod:15:7}
Logentry,%v4%
ends in the log

Code: Select all

2312470: ACT : Let,4,{mod:15:7}
2312473: Calculate: Unknown token
2312478: ACT : Logentry,0
2312483: 0
and the rule

Code: Select all

Let,4,mod(15,7)
Logentry,%v4%
produce this

Code: Select all

2732473: ACT : Let,4,mod(15,7)
2732476: Command unknown: Let,4,mod(15,7)
2732500: ACT : Logentry,0
2732504: 0
I'm going crazy...

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

Re: Modulo operation in rules

#4 Post by TD-er » 14 Mar 2021, 11:17

I will have a look at it, as I had written the documentation when implementing the new functions but then decided that it was a bit too complex so changed it.
I will check for the "mod" operator to see what should be the correct syntax.

N.B. also make sure to check if you are running a "custom" or "normal" build as the "testing" and "dev" builds have the LIMIT_BUILD_SIZE define enabled which does exclude a number of these rules functions.

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

Re: Modulo operation in rules

#5 Post by Micha_he » 14 Mar 2021, 11:22

Tested with:
  • ESP_Easy_mega_20210223_hard_SONOFF_POW_4M1M
  • ESP_Easy_mega_20210223_hard_other_POW_ESP8285_1M

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

Re: Modulo operation in rules

#6 Post by TD-er » 14 Mar 2021, 11:32

Can you test with the "%" operator?

Code: Select all

let,1,10%3
Should store "1" into the 1st variable.

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

Re: Modulo operation in rules

#7 Post by Micha_he » 14 Mar 2021, 11:49

That works. But not in a if-condition. I must assign it to a var and usw then the var in the if-condition.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 31 guests