toggle  [Solved]

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

Moderator: leecollings

Post Reply
markjgabb
Posts: 142
Joined: Tuesday 24 January 2017 23:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Australia
Contact:

toggle

Post 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
}
V 2020.2 RPI 3
RFlink 334 mhz
mysensors
broadlink
Mirabella Genio Globes
User avatar
boum
Posts: 136
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: toggle

Post 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 :)
markjgabb
Posts: 142
Joined: Tuesday 24 January 2017 23:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Australia
Contact:

Re: toggle

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

V 2020.2 RPI 3
RFlink 334 mhz
mysensors
broadlink
Mirabella Genio Globes
markjgabb
Posts: 142
Joined: Tuesday 24 January 2017 23:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Australia
Contact:

Re: toggle  [Solved]

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

V 2020.2 RPI 3
RFlink 334 mhz
mysensors
broadlink
Mirabella Genio Globes
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest