Page 1 of 1

Simple "Toggle" Switch LUA

Posted: Thursday 27 October 2016 14:43
by DomoSeb
I am trying to figure out why my LUA script is not fired/not works.
I have tried a couple of alternative ways, but I seem to be able to figure out why it is not fired!

Can you guys give me a suggestion?
(I am well aware that this has to be plain simple, but I have no clue at the moment where to dig deeper to make it work)
Here is my code:

Code: Select all

commandArray = {}
if (devicechanged['Switch - Shed Inside']) then
    if (commandArray['Relay - 03 - Lamp Shed Inside']=='On') then
        commandArray['Relay - 03 - Lamp Shed Inside']='Off'
    elseif (commandArray['Relay - 03 - Lamp Shed Inside']=='Off') then
        commandArray['Relay - 03 - Lamp Shed Inside']='On'
    end
--elseif (otherdevices['Switch - Shed Inside'] == 'Closed') then
 --   if (commandArray['Relay - 03 - Lamp Shed Inside']=='On') then
  --      commandArray['Relay - 03 - Lamp Shed Inside']='Off'
 --   elseif (commandArray['Relay - 03 - Lamp Shed Inside']=='Off') then
 --       commandArray['Relay - 03 - Lamp Shed Inside']='On'
--    end
end

return commandArray
'Switch - Shed Inside' = contact switch based on GPIO input
'Relay - 03 - Lamp Shed Inside' = on/of switch controlling GPIO output -> relay

Re: Simple "Toggle" Switch LUA

Posted: Thursday 27 October 2016 14:52
by Sneezydevil
Replace the commandArrays within the if's with something like:

Code: Select all

otherdevices['Relay - 03 - Lamp Shed Inside']
Now you first create a empty array (commandArray = {}), and is still empty when you start checking.

Should result in something like this.

Code: Select all

commandArray = {}
if (devicechanged['Switch - Shed Inside']) then
    if (otherdevices['Relay - 03 - Lamp Shed Inside']=='On') then
        commandArray['Relay - 03 - Lamp Shed Inside']='Off'
    elseif (otherdevices['Relay - 03 - Lamp Shed Inside']=='Off') then
        commandArray['Relay - 03 - Lamp Shed Inside']='On'
    end
end

return commandArray

Re: Simple "Toggle" Switch LUA

Posted: Thursday 27 October 2016 15:37
by DomoSeb
TY very much, working now.

Re: Simple "Toggle" Switch LUA

Posted: Monday 23 December 2019 18:00
by Roozen
I am trying to integrate the simple toggle function as action for a fibaro dimmer2 S2 singleclick.
Sounds simple, but somehow I can not get it work.

This is the script I am using:

Code: Select all

commandArray = {}
if (devicechanged['$S2 SingleClick']=='On') and (otherdevices['Tuinverlichting']=='Off') then
        commandArray['Tuinverlichting']='On'
elseif (devicechanged['$S2 SingleClick']=='On') and (otherdevices['Tuinverlichting']=='On') then
        commandArray['Tuinverlichting']='Off'
end
return commandArray
Any idea what I am doing wrong?

Re: Simple "Toggle" Switch LUA

Posted: Monday 13 January 2020 14:15
by bewo
Hi Roozen,

your script looks quite ok. Are you sure the name contains an "$"?
If yes, you can try an other way:

Code: Select all

commandArray = {}

if devicechanged['$S2 SingleClick'] then
	if otherdevices['Tuinverlichting'] == 'Off' then
		commandArray['Tuinverlichting'] = 'On'
	else
		commandArray['Tuinverlichting'] = 'Off'
end

return commandArray
If this didn't work, there sometimes is an issue with the dimmer state, so there you can use:

Code: Select all

commandArray = {}

if devicechanged['$S2 SingleClick'] then
	if otherdevices['Tuinverlichting'] == 0 then
		commandArray['Tuinverlichting'] = 'Set Level 100'
	else
		commandArray['Tuinverlichting'] = 'Set Level 0'
	end
end

return commandArray

Re: Simple "Toggle" Switch LUA

Posted: Wednesday 15 January 2020 19:12
by Roozen
Hi Bewo,

Thanks for your reply.
The above script works partially if the "tuinverlichting" is on
Then the "Tuinverlichting will switch off when pressing the $S2 Singleclick button

When pressing again it will not toggle on again
Looks like the part after else is not working

When I reverse the on/off as below, nothing happens

Code: Select all

commandArray = {}

if devicechanged['$S2 SingleClick'] then
	if otherdevices['Tuinverlichting'] == 'On' then
		commandArray['Tuinverlichting'] = 'Off'
	else
		commandArray['Tuinverlichting'] = 'On'
	end
end

return commandArray
The log states "Status: EventSystem: Script event triggered: Script #1"
No error visible

No Idea what to change.
I am using a fibaro dimmer switch ($S2 SingleClick) and try to trigger a neo coolcam wall plug (Tuinverlichting)

Re: Simple "Toggle" Switch LUA

Posted: Thursday 16 January 2020 2:02
by bewo
Ok... When it's off.... what ist the log output of:

Code: Select all

commandArray = {}

print('State: '..otherdevices['Tuinverlichting'])
print('Value: '..otherdevices_svalues['Tuinverlichting'])

return commandArray

Re: Simple "Toggle" Switch LUA

Posted: Thursday 16 January 2020 17:08
by Roozen
When off:
2020-01-16 17:05:19.607 Status: LUA: State: Off
2020-01-16 17:05:19.608 Status: LUA: Value: 0

When on:
2020-01-16 17:06:25.599 Status: LUA: State: On
2020-01-16 17:06:25.600 Status: LUA: Value: 100

Re: Simple "Toggle" Switch LUA

Posted: Thursday 16 January 2020 17:33
by Roozen
I have updated to script as below to monitor whether the else if commend is working corrrectly. This is the case

Code: Select all

commandArray = {}

if devicechanged['$S2_SingleClick'] then
	if otherdevices['Tuinverlichting']=='Off' then
		commandArray['Tuinverlichting']='100'and
		print('Tuinverlichting aan')
	else
		commandArray['Tuinverlichting']='0' and
		print('Tuinverlichting uit')
    end
end

return commandArray
If tuinverlichting is off, then it prints
2020-01-16 17:24:42.968 Status: LUA: Tuinverlichting aan

If tuinverlichting is on, then it prints
2020-01-16 17:23:00.116 Status: LUA: Tuinverlichting uit

Also changed the commandArray to 0 and 100 in stead of Off and On
This has no effect

When using the "tuinverlichting' switch everything works fine wth switching.
Log:
2020-01-16 17:34:20.758 Status: User: administrator initiated a switch command (10/Tuinverlichting/Off)
or
2020-01-16 17:34:20.758 Status: User: administrator initiated a switch command (10/Tuinverlichting/On)

It looks like something in the commandArray is not functioning

Re: Simple "Toggle" Switch LUA

Posted: Thursday 16 January 2020 18:59
by bewo
Ok, the log output is as expected..
Your command can't work because of a kind of grammar in LUA und case sensitivity. In this case it's not possible to write only 0. It have to be 'Set Level 0'.

BUT... -> It doesn't matter. I've did an mistake too. You've wrote:
I am using a fibaro dimmer switch ($S2 SingleClick) and try to trigger a neo coolcam wall plug (Tuinverlichting)
I misunderstood and i thought "Tuinverlichting" ist the lamp which should be dimmed but it's the wall plug. So my scripts above CAN'T work. Sorry about that.

There are some issues with the state of the fibaro dimmer as trigger in devicechanged table, but it's possible.
Just copy & paste this and have fun. I guess it will work. ;-)

Code: Select all

commandArray = {}

-- Turn wall plug off:
if devicechanged['$S2 SingleClick'] == 'Off' then
	if otherdevices['Tuinverlichting'] == 'On' then
		commandArray['Tuinverlichting'] = 'Off'
	end
end
-- Turn wall plug on:
if string.sub(otherdevices['$S2 SingleClick'], 1, 10) == 'Set Level:' or otherdevices['$S2 SingleClick'] == 'On' then
    if otherdevices['Tuinverlichting'] == 'Off' then
		commandArray['Tuinverlichting'] = 'On'
	end
end


return commandArray