Page 1 of 1

Rules with combining logic

Posted: 05 Apr 2017, 12:08
by Elektrofreak
Hi all,

I am planning to use a SONOFF with a BH1750 luminosity sensor to control a secondary lamp in a bath room. The ESP will be used without an external controller but with the internal rules.

What I understand is the following example rule:

Code: Select all

On brightness#lux do
  if [brighness#lux]>1  // Master light switched on, so turn on relais
    gpio,12,1
    gpio,13,0    
  endif
endon
On brightness#lux do
  if [brighness#lux]<50  // Master light switched off but light connected to relais still on, so turn off relais
    gpio,12,0
    gpio,13,1    
  endif
endon
The problem is that the trigger level for turning off is higher than the trigger level for turning on. Thus, I need to take also the current state into consideration. The device monitoring the relais IO is called relais. So I would like to use something like this:

Code: Select all

On brightness#lux do
  if [brighness#lux]>1 and [relais#relais] == 0  // Master light switched on, so turn on relais
    gpio,12,1
    gpio,13,0    
  endif
endon
On brightness#lux do
  if [brighness#lux]<50 and [relais#relais] != 0 // Master light switched off but light connected to relais still on, so turn off relais
    gpio,12,0
    gpio,13,1    
  endif
endon
But I already crashed the config once and the brightness sensor connected to the SONOFF uses the (disabled) RX and TX lines so I need to re-flach each time I mess up the config :cry: .

Could you tell me if my idea with a combining "and" is possible? Would it be possible to interleave two if loops?


Please help :roll:

Re: Rules with combining logic

Posted: 06 Apr 2017, 22:41
by paxi
The rules tutorial states that neither boolean logic nor nesting is supported. :?
You can work around this limitation by creating another rule:

Code: Select all

On event1 do
  If condition1 is met
   Event checkAND 
Endon

On checkAND do
   If condition2 is met
    Desired action
Endon

Re: Rules with combining logic

Posted: 18 Apr 2017, 08:26
by Elektrofreak
Thank you for your reply. I will test it and give feedback ;)

Re: Rules with combining logic

Posted: 18 Apr 2017, 20:24
by toffel969
paxi wrote: 06 Apr 2017, 22:41 The rules tutorial states that neither boolean logic nor nesting is supported. :?
You can work around this limitation by creating another rule:

Code: Select all

On event1 do
  If condition1 is met
   Event checkAND 
Endon

On checkAND do
   If condition2 is met
    Desired action
Endon
It definitely works. @paxi, I allowed myself to use above to update the rules tutorial in the wiki, trusting it's OK for you

Re: Rules with combining logic

Posted: 18 Apr 2017, 20:32
by Martinus
You should also be able to use a condition directly within the "on xxx" line like this sample that i'm using:

Code: Select all

on OutsideLDR#Lux<50 do
  if [Lights#Hal]=0
    SerialSend 10;NewKaku;123454;2;ON;
    SerialSend 10;NewKaku;123455;2;ON;
    TaskValueSet 3,1,1
  endif
endon

Re: Rules with combining logic

Posted: 24 Apr 2017, 09:21
by paxi
I feel honored toffel969. ;)
Martinus' solution is way more elegant for cases where at least one condition depends on the variable triggering the event. Mine is 'universal'. :D

Re: Rules with combining logic

Posted: 24 Apr 2017, 09:28
by toffel969
paxi wrote: 24 Apr 2017, 09:21 I feel honored toffel969. ;)
Martinus' solution is way more elegant for cases where at least one condition depends on the variable triggering the event. Mine is 'universal'. :D
I know :-) I use both of them in combination for MONSTER-rules ;-)