Rules if-then-else and fall-through

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
boolie
Normal user
Posts: 20
Joined: 23 May 2018, 21:35

Rules if-then-else and fall-through

#1 Post by boolie » 25 May 2018, 13:33

I over-estimated what the rules parser was capable of and found an interesting effect that could cause problems if one were to make a mistake by leaving out an else or endif... this doesn't feel like intentional behaviour and isn't logical, so it may need some work to stop it happening.

I wrote:

on Rules#Timer=1 do
if [dummy#var1] = 1
7dt,1
TaskValueSet,12,1,2
endif
if [dummy#var1] = 2
7dt,2
TaskValueSet,12,1,1
endif
endon

I expected, logically, the display to alternate between showing "1" and "2" each time the timer event occurred.

However, what happens in fact is that each time the event occurs with dummy#var1 = 1, the display shows "1" very briefly and then stops on "2". The code execution is falling through from the first "if" clause and executing the code inside the second "if" clause.

I get that the rules are very simple logic and the intended coding would be to nest the second "if" in an "else" section of the first "if" - although that will produce some very ugly code in an application like mine where there are 7 different display values to cycle through - but the problem here is that the parser fell through and executed code that shouldn't have been executed. That could happen easily if someone missed an "else" or "endif" in otherwise valid code.

Could the parser be amended so that it gives an error or ignores the second "if" clause (or better still, processes it properly)?
Various D1 & Sonoffs with ESPEasy, some Tasmota. Domoticz, Z-wave.
ESPEasy Patreon - keep up the good stuff, guys!

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

Re: Rules if-then-else and fall-through

#2 Post by grovkillen » 25 May 2018, 13:43

We'll make an code valuator in future releases where we can use JavaScript to parse through the code to make sure errors are detected. We're not there yet but it's on the to-do list.
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:

boolie
Normal user
Posts: 20
Joined: 23 May 2018, 21:35

Re: Rules if-then-else and fall-through

#3 Post by boolie » 25 May 2018, 16:10

Great idea :-)
Various D1 & Sonoffs with ESPEasy, some Tasmota. Domoticz, Z-wave.
ESPEasy Patreon - keep up the good stuff, guys!

boolie
Normal user
Posts: 20
Joined: 23 May 2018, 21:35

Re: Rules if-then-else and fall-through

#4 Post by boolie » 26 May 2018, 10:58

Cracked it :-)

I thought that my logic block was too much for the parser, but in fact it was partly my logic at fault... incrementing the counter in each "if" block was silly because the next block then had the right conditions to run. With multiple "if" blocks, that's not clever. D'oh! :lol:

So, here's how I cycle through a number of sensor values in a timed event:

On Rules#Timer=1 do
if [MyVars#cycle] = 1
7dt,[BME280_1#Temperature]
endif
if [MyVars#cycle] = 2
7dn,[BME280_1#Humidity]
endif
if [MyVars#cycle] = 3
7dt,[AM2302_1#Temperature]
endif
if [MyVars#cycle] = 4
7dt,[DS18_D6_1#Temperature]
endif // [MyVars#cycle] = 5
if [MyVars#cycle] = 5
7dn,[BH1750_Lux1#Lux]
endif

TaskValueSet 12,1,[MyVars#cycle]+1
if [MyVars#cycle]=6
TaskValueSet 12,1,1
endif

timerSet,1,5
endon

I'm very pleased to say that the parser does cope with those separate "if" blocks nicely :D If I try to take the alternative approach and nest each "if" in the "else" clause of the previous "if", it doesn't play nicely. Fortunately, in this case, separate "if" blocks makes for cleaner code anyway.

Always something to learn...!
Various D1 & Sonoffs with ESPEasy, some Tasmota. Domoticz, Z-wave.
ESPEasy Patreon - keep up the good stuff, guys!

arion_p
Normal user
Posts: 24
Joined: 05 Mar 2018, 22:02

Re: Rules if-then-else and fall-through

#5 Post by arion_p » 31 May 2018, 11:22

A nice addition for cases like this would be a 'switch' statement, although it may be too much for the current parser.

Also and 'exit' or 'break' statement would help make some rules cleaner to read.

Post Reply

Who is online

Users browsing this forum: No registered users and 22 guests