LUA - nested if problem

Moderator: leecollings

Post Reply
Luuki
Posts: 4
Joined: Thursday 18 October 2018 8:38
Target OS: -
Domoticz version:
Contact:

LUA - nested if problem

Post 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? ;)
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: LUA - nested if problem

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: LUA - nested if problem

Post 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
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Luuki
Posts: 4
Joined: Thursday 18 October 2018 8:38
Target OS: -
Domoticz version:
Contact:

Re: LUA - nested if problem

Post 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).
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: LUA - nested if problem

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: LUA - nested if problem

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Luuki
Posts: 4
Joined: Thursday 18 October 2018 8:38
Target OS: -
Domoticz version:
Contact:

Re: LUA - nested if problem

Post by Luuki »

Thanks, I will try that! Currently however I cannot because my whole domoticz is rendered useless because of failing openzwave 1.6...
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: LUA - nested if problem

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest