Unexpected result with nested if statement  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
User avatar
Ragdag
Posts: 169
Joined: Friday 30 March 2018 13:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Unexpected result with nested if statement

Post by Ragdag »

I have the following piece of code.

Code: Select all

        if (item.isDevice) and ((User1Thuis.state == 'Off') and (User2Thuis.state == 'Off') and VakantieSchema.state == 'Off') then
            dz.log('User1 and User2 not at home, turning on holiday lighting schedule', dz.LOG_DEBUG)
            pushover(PushoverUsers.User1, PushoverApps.Huis, 'Vakantie Verlichting', 'User1 and User2 not at home. \nTurning on holiday lighting schedule', 0, 'none' )
            VakantieSchema.switchOn()
        elseif (item.isDevice) and ((User1Thuis.state == 'On') or (User2Thuis.state == 'On') and VakantieSchema.state == 'On') then
            VakantieSchema.switchOff()
            dz.log('User1 and User2 at home, turning off holiday lighting schedule', dz.LOG_DEBUG)
            if (User1Thuis.state == 'On') and (User2Thuis.state == 'Off') then
                pushover(PushoverUsers.User1, PushoverApps.Huis, 'Vakantie Verlichting', 'User1 is home. \nTurning off holiday lighting schedule', 0, 'none' )
            elseif (User2Thuis == 'On') and (User1Thuis.state == 'Off') then
                pushover(PushoverUsers.User1, PushoverApps.Huis, 'Vakantie Verlichting', 'User2 is home. \nTurning off holiday lighting schedule', 0, 'none' )
            elseif (User1Thuis.state == 'On') and (User2Thuis.state == 'On') then
                pushover(PushoverUsers.User1, PushoverApps.Huis, 'Vakantie Verlichting', 'User1 and User2 are home. \nTurning off holiday lighting schedule', 0, 'none' )
            end
        end
        
I would expect that the nested if block:
"if (User1Thuis.state == 'On') and (User2Thuis.state == 'Off') then"

would only be executed if the main
"elseif (item.isDevice) and ((User1Thuis.state == 'On') or (User2Thuis.state == 'On') and VakantieSchema.state == 'On') then" block is true.
That is not the case and it will trigger the pushover notification.\

Am I missing something?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Unexpected result with nested if statement

Post by waaren »

Ragdag wrote: Tuesday 26 May 2020 8:37 ...
I would expect that the nested if block:
"if (User1Thuis.state == 'On') and (User2Thuis.state == 'Off') then"

would only be executed if the main
"elseif (item.isDevice) and ((User1Thuis.state == 'On') or (User2Thuis.state == 'On') and VakantieSchema.state == 'On') then" block is true.
That is not the case and it will trigger the pushover notification.\

Am I missing something?
Hard to tell without seeing the log. Can you use this in your script and share the relevant log lines? It will show you and us some potential useful information about the flow through the code.

Code: Select all

        dz.log('----------- 1 --------', dz.LOG_DEBUG)
        if (item.isDevice) and ((User1Thuis.state == 'Off') and (User2Thuis.state == 'Off') and VakantieSchema.state == 'Off') then
            dz.log('----------- 2 --------', dz.LOG_DEBUG)
            dz.log('User1 and User2 not at home, turning on holiday lighting schedule', dz.LOG_DEBUG)
            pushover(PushoverUsers.User1, PushoverApps.Huis, 'Vakantie Verlichting', 'User1 and User2 not at home. \nTurning on holiday lighting schedule', 0, 'none' )
            VakantieSchema.switchOn()
        elseif (item.isDevice) and ((User1Thuis.state == 'On') or (User2Thuis.state == 'On') and VakantieSchema.state == 'On') then
            dz.log('----------- 3 --------', dz.LOG_DEBUG)
            VakantieSchema.switchOff()
            dz.log('User1 and User2 at home, turning off holiday lighting schedule', dz.LOG_DEBUG)
            if (User1Thuis.state == 'On') and (User2Thuis.state == 'Off') then
                dz.log('----------- 4 --------', dz.LOG_DEBUG)
                pushover(PushoverUsers.User1, PushoverApps.Huis, 'Vakantie Verlichting', 'User1 is home. \nTurning off holiday lighting schedule', 0, 'none' )
            elseif (User2Thuis == 'On') and (User1Thuis.state == 'Off') then
                dz.log('----------- 5 --------', dz.LOG_DEBUG)
                pushover(PushoverUsers.User1, PushoverApps.Huis, 'Vakantie Verlichting', 'User2 is home. \nTurning off holiday lighting schedule', 0, 'none' )
            elseif (User1Thuis.state == 'On') and (User2Thuis.state == 'On') then
                dz.log('----------- 6 --------', dz.LOG_DEBUG)
                pushover(PushoverUsers.User1, PushoverApps.Huis, 'Vakantie Verlichting', 'User1 and User2 are home. \nTurning off holiday lighting schedule', 0, 'none' )
            end
        end
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
Ragdag
Posts: 169
Joined: Friday 30 March 2018 13:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Unexpected result with nested if statement  [Solved]

Post by Ragdag »

Never mind, I think I found it the brackets are not correct :oops:
In the elseif the 2 OR statements should be grouped and currently it is the OR and the AND that is grouped.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest