Page 1 of 1

Rules Question - Can you do "Calculations" in rules?

Posted: 27 Dec 2020, 10:55
by Zodiac69
Hi All

I have a rule that decode a mqtt event and split each of the 16 bits to switch a MCP23017 port On/Off, thanks to ThomasB - viewtopic.php?f=4&t=7988#p47122
I would like to display the "Calculated" value on a oled, each bit represent a different value, how can i:
1. Assign a value to the "bit"
2. Sum the value and make it available to display?

Is this possible, or is there another way of doing this?

Z69

Re: Rules Question - Can you do "Calculations" in rules?

Posted: 27 Dec 2020, 13:33
by TD-er
Right now we don't have logic operators like logic and/or, which makes it a bit hard to extract single bits out of values.

As it has been requested a few times before, I will try to add extra commands for div/mod and some other bitwise operators.

Right now you can try to split the number into less values using the existing string operators we already have.

For example %c_2hex%(100000) will replace the "100000" into "186A0"
This can then be split into a smaller number of hex decimals, via https://espeasy.readthedocs.io/en/lates ... #substring
Sadly the position starts from the "most significant" digit, so it is still hard to extract the correct bit as the resulting value of the HEX string may not be of constant length.

So you still may need to do way too much in the rules to extract a bit, which makes a valid point for the need of proper bit extraction commands.

Re: Rules Question - Can you do "Calculations" in rules?

Posted: 27 Dec 2020, 15:18
by TD-er
OK, I took a look at the code and it looked like finding and describing work-around was probably taking longer then implementing it.
So can you have a look at this PR: https://github.com/letscontrolit/ESPEasy/pull/3439 ?