Simple "Toggle" Switch LUA

Moderator: leecollings

Post Reply
DomoSeb
Posts: 5
Joined: Tuesday 24 November 2015 6:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Simple "Toggle" Switch LUA

Post 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
Sneezydevil
Posts: 111
Joined: Monday 18 January 2016 9:56
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Simple "Toggle" Switch LUA

Post 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
DomoSeb
Posts: 5
Joined: Tuesday 24 November 2015 6:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Simple "Toggle" Switch LUA

Post by DomoSeb »

TY very much, working now.
Roozen
Posts: 8
Joined: Monday 23 December 2019 17:53
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Kerkrade, Netherlands
Contact:

Re: Simple "Toggle" Switch LUA

Post 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?
User avatar
bewo
Posts: 74
Joined: Monday 13 July 2015 12:27
Target OS: Linux
Domoticz version: 2021.1
Location: Bavaria - Germany
Contact:

Re: Simple "Toggle" Switch LUA

Post 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
Individual projects:
Domoticz on a Intel Xeon Server | AeonLabs Z-Wave Gen.5 | RFXCOM RFXtrx433E USB | ESP-Wifi-Modules | Shellys
Wall-mounted 22" Touch Control Display (self construct) | LUA wind monitor| LUA heating control | and many many more :)
Roozen
Posts: 8
Joined: Monday 23 December 2019 17:53
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Kerkrade, Netherlands
Contact:

Re: Simple "Toggle" Switch LUA

Post 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)
Last edited by Roozen on Thursday 16 January 2020 17:09, edited 1 time in total.
User avatar
bewo
Posts: 74
Joined: Monday 13 July 2015 12:27
Target OS: Linux
Domoticz version: 2021.1
Location: Bavaria - Germany
Contact:

Re: Simple "Toggle" Switch LUA

Post 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
Individual projects:
Domoticz on a Intel Xeon Server | AeonLabs Z-Wave Gen.5 | RFXCOM RFXtrx433E USB | ESP-Wifi-Modules | Shellys
Wall-mounted 22" Touch Control Display (self construct) | LUA wind monitor| LUA heating control | and many many more :)
Roozen
Posts: 8
Joined: Monday 23 December 2019 17:53
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Kerkrade, Netherlands
Contact:

Re: Simple "Toggle" Switch LUA

Post 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
Roozen
Posts: 8
Joined: Monday 23 December 2019 17:53
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Kerkrade, Netherlands
Contact:

Re: Simple "Toggle" Switch LUA

Post 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
User avatar
bewo
Posts: 74
Joined: Monday 13 July 2015 12:27
Target OS: Linux
Domoticz version: 2021.1
Location: Bavaria - Germany
Contact:

Re: Simple "Toggle" Switch LUA

Post 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
Individual projects:
Domoticz on a Intel Xeon Server | AeonLabs Z-Wave Gen.5 | RFXCOM RFXtrx433E USB | ESP-Wifi-Modules | Shellys
Wall-mounted 22" Touch Control Display (self construct) | LUA wind monitor| LUA heating control | and many many more :)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest