Page 1 of 1

Unexpected result with nested if statement

Posted: Tuesday 26 May 2020 8:37
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?

Re: Unexpected result with nested if statement

Posted: Tuesday 26 May 2020 9:40
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

Re: Unexpected result with nested if statement  [Solved]

Posted: Tuesday 26 May 2020 9:44
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.