Page 1 of 1

LUA - nested if problem

Posted: Saturday 20 July 2019 17:15
by Luuki
Hi, I am just not seeing it anymore. I am trying to turn my Blockly into LUA because of added flexibility regarding nested if statements.

I have this:

Code: Select all

time = os.date("*t")
if (devicechanged['$BADKAMER - Bewegingssensor'] == 'On') then
    if (time.hour >= 16 and time.hour <=18) then
        commandArray['BADKAMER - Spotjes']='Set Level 30'
        print('Beweging gedetecteerd in de avond/nacht uren, BADKAMER - Spotjes gedmind aan')
    
    else commandArray['BADKAMER - Spotjes']='Set Level 100'
    print('Beweging gedetecteerd overdag, BADKAMER - Spotjes op 100% aan')
    end
end
return commandArray
It just seems that the second (nested) if statement is never executed. Of course I did debug the expression and also replaced it with just plain "true", like so:

Code: Select all

time = os.date("*t")
if (devicechanged['$BADKAMER - Bewegingssensor'] == 'On') then
    if (true) then
        commandArray['BADKAMER - Spotjes']='Set Level 30'
        print('Beweging gedetecteerd in de avond/nacht uren, BADKAMER - Spotjes gedmind aan')
    
    else commandArray['BADKAMER - Spotjes']='Set Level 100'
    print('Beweging gedetecteerd overdag, BADKAMER - Spotjes op 100% aan')
    end
end
and I also did test the outer if got evaluated to true which it does.
I am out of options. I checked online tutorials as well. I am trying to do something impossible in LUA or am I just getting detail-blind from scripting too much today? ;)

Re: LUA - nested if problem

Posted: Saturday 20 July 2019 18:05
by jvdz
Are you sure the devicename '$BADKAMER - Bewegingssensor' is correct?
What is the name of the script in case this is a file in domoticz\scripts\lua?
Which options did you specify in case this is an LUA script via the internal editor?

Jos

Re: LUA - nested if problem

Posted: Saturday 20 July 2019 22:35
by waaren
Luuki wrote: Saturday 20 July 2019 17:15 I am just not seeing it anymore. I am trying to turn my Blockly into LUA because of added flexibility regarding nested if statements.
Try this:

Code: Select all

commandArray = {}

time = os.date("*t")
if (devicechanged['$BADKAMER - Bewegingssensor'] == 'On') then
    if (time.hour >= 22 and time.hour <= 23) then
        commandArray['BADKAMER - Spotjes']='Set Level 30'
        print('Beweging gedetecteerd in de avond/nacht uren, BADKAMER - Spotjes gedmind aan')
    
    else commandArray['BADKAMER - Spotjes']='Set Level 100'
        print('Beweging gedetecteerd overdag, BADKAMER - Spotjes op 100% aan')
    end
end

return commandArray
or in dzVents

Code: Select all

return
{
    on  = { devices = { '$BADKAMER - Bewegingssensor' }},
        
    execute = function(dz, item)
        
        if item.active then
            dimmer = dz.devices('BADKAMER - Spotjes')
            if dz.time.matchesRule('at 22:00-23:59') then
                dimmer.dimTo(30)
            else
                dimmer.dimTo(100)
            end
        end
    end
}


Re: LUA - nested if problem

Posted: Thursday 25 July 2019 9:22
by Luuki
jvdz wrote: Saturday 20 July 2019 18:05 Are you sure the devicename '$BADKAMER - Bewegingssensor' is correct?
What is the name of the script in case this is a file in domoticz\scripts\lua?
Which options did you specify in case this is an LUA script via the internal editor?

Jos
I chose a new LUA script > Devices, in the internal Events editor.
And the name of the device is indeed '$BADKAMER - Bewegingssensor' I double checked again :)

@waaren, I only see one difference in your script vs mine which is the time? (And the commandArray opening statement, which I have, but forgot to copy in my OP).

Re: LUA - nested if problem

Posted: Thursday 25 July 2019 10:06
by waaren
Luuki wrote: Thursday 25 July 2019 9:22 I only see one difference in your script vs mine which is the time? (And the commandArray opening statement, which I have, but forgot to copy in my OP).
Correct. Without the commandArray declaration before it's first use, your script failed my test. The only thing to got it working was to add that.
If it still fails on your system then either the name is not exactly the same (maybe a funny character in your script or in the device itself) or it does not return an 'On' when activated.
Might be helpful to add some print statements to verify this.

Re: LUA - nested if problem

Posted: Thursday 25 July 2019 10:10
by jvdz
So the PIR switch is hidden since it has a $ as prefix?
Try something like this to ensure the device change really get's fired:

Code: Select all

commandArray = {}

time = os.date("*t")
if (devicechanged['$BADKAMER - Bewegingssensor']) then
	print("$BADKAMER - Bewegingssensor:" .. devicechanged['$BADKAMER - Bewegingssensor'])
	if (devicechanged['$BADKAMER - Bewegingssensor'] == 'On') then
		if (time.hour >= 22 and time.hour <= 23) then
			commandArray['BADKAMER - Spotjes']='Set Level 30'
			print('Beweging gedetecteerd in de avond/nacht uren, BADKAMER - Spotjes gedmind aan')

		else commandArray['BADKAMER - Spotjes']='Set Level 100'
			print('Beweging gedetecteerd overdag, BADKAMER - Spotjes op 100% aan')
		end
	end
end

return commandArray
This should add a logentry each time the detection happens and the switch returns to off.

Jos

Re: LUA - nested if problem

Posted: Friday 23 August 2019 15:09
by Luuki
Thanks, I will try that! Currently however I cannot because my whole domoticz is rendered useless because of failing openzwave 1.6...

Re: LUA - nested if problem

Posted: Friday 23 August 2019 15:55
by jvdz
Luuki wrote: Friday 23 August 2019 15:09 Thanks, I will try that! Currently however I cannot because my whole domoticz is rendered useless because of failing openzwave 1.6...
That is why my production system isn't running BETA ever and not even the latest production right away. ;)
I first test them on a test setup and give it the time to get possible bugs detected and resolved.
Jos