Page 1 of 1

toggle

Posted: Saturday 16 November 2019 7:13
by markjgabb
hi all
Im trying to setup a dzvents script to toggle some lights on a button press
but i keep gettings errors on my ends...

is anyone able to review where im up to and direct me the correct way?

error is as below, with code below that

2019-11-16 17:12:12.949 Status: dzVents: Error (2.4.19): error loading module 'Lounge button' from file '/home/pi/domoticz/scripts/dzVents/generated_scripts/Lounge button.lua':
2019-11-16 17:12:12.949 ...ticz/scripts/dzVents/generated_scripts/Lounge button.lua:13: 'end' expected (to close 'function' at line 6) near 'else'

Code: Select all

return {
    on = { 
        devices = { 35 
        },
    
    execute = function(dz, item)
        local lights = dz.devices('fan light 1', 'fan light 2')
        

        if lights.state == 'On' then
            lights.switchOff()
        end
        else
            lights.switchOff()
                
            
        end
}

Re: toggle

Posted: Saturday 16 November 2019 11:12
by boum
You have an extra "end" statement in you if-then-else-end block. You should try this:

Code: Select all

return {
    on = { 
        devices = { 35 }
        },
    
    execute = function(dz, item)
        if item.active then
	    dz.devices('fan light 1').toggleSwitch()
	    dz.devices('fan light 2').toggleSwitch()
        end
    end
}
In fact, when rewriting your script, I noticed a few things.
You should always check that your curly braces {} are matched (missing for the devices table in the "on")
You should always check that your "end" statements are balanced too.
The domoticz.devices() function takes either 1 or 0 arguments. If you put more, you'll only get the first device.
Before changing the light states, you should check the state of the triggering device (item.active or the value of item.state), otherwise the lights will toggle state when the button is pressed, then released.

I hope this helps :)

Re: toggle

Posted: Sunday 17 November 2019 9:09
by markjgabb
ok yeah it does, i had to rework for a different way.

It now works to turn lights on, but i cant turn them back off the same way....

in theory 38 is the fan lights dummy switch
when i turn it on the two fan lights should turn on
and when i turn it off the two fan lights should turn off..
i Would of thought this was simple

but i can turn them on but then not off
when the master switch is off every 30 seconds the lights flicker for a moment, but stay on.

Code: Select all


return {
    on = { 
        devices = { 38 }
         },
    
    execute = function(dz, item)
        if item.active then
            if item.state == 'On' then
	            dz.devices('Fan1').switchOn()
	            dz.devices('Fan2').switchOn()
	       elseif item.state == 'Off' then
	            dz.devices('Fan1').switchOff()
	            dz.devices('Fan2').switchOff()
            end
        end
    end
}


Re: toggle  [Solved]

Posted: Sunday 17 November 2019 11:02
by markjgabb
ok im an idiot

after working through it all i got the following to work

Code: Select all


return {
    on = { 
        devices = { 38 }
         },
    
    execute = function(dz, item)
            dz.log('fan lights switch activated')
            if item.state == 'On' then
                dz.log('fan lights master turned on')
	            dz.devices('Fan1').switchOn()
	            dz.devices('Fan2').switchOn()
	       elseif item.state == 'Off' then
	           dz.log('fan lights master turned off')
	            dz.devices('Fan1').switchOff()
	            dz.devices('Fan2').switchOff()
            end
    end
}