Page 1 of 1
Is there a select statement?
Posted: Wednesday 05 February 2025 8:28
by manjh
Hopefully there is a simple answer to this...
In other programming languages I can use the select statement to execute parts of code depending on content of a variable.
For instance:
Assume variable x has a numeric value.
Select (x)
when (1) do
...
end
when (2) do
...
end
otherwise
...
end
end
I know I could do this using if-then-elseif etcetera, but the select would be much cleaner....
Is there something like this?
Re: Is there a select statement?
Posted: Wednesday 05 February 2025 9:15
by Kedi
In
https://www.lua.org/manual/5.1/manual.html you can see that the select statement is used for somewhat different.
What would be the benefit over "if then elseif end" method?
Re: Is there a select statement?
Posted: Wednesday 05 February 2025 10:11
by lost
The C-like switch/case is not present in Lua (was same for very long with Python as well), usual advice to avoid heavy if/elseif usage is to rework the logic using what is the basis of this language: Tables...
http://lua-users.org/wiki/SwitchStatement
That's one among C oldies I often miss using Lua or Python, others being static variables and ternary operator: There is always some tricks, but not being a language feature I always have to remember/search when needed.
Re: Is there a select statement?
Posted: Thursday 06 February 2025 8:57
by manjh
Maybe I should explain the purpose I wanted this for.
I use a UserVariable to store a value, coming out of a number of NFC sources.
Each value represents a specific command: like "turn on lights", "turn off ventilation", "toggle driveway spotlight", etcetera.
My routine picks up that uservariable, and then executes a specific subroutine.
One way to do it would be to name the subroutine with the value and then call it using the variable.
For example: name the subroutine "1", and then find a way to call that directly using the uservariable.
Unfortunately I see no way to do this in LUA.
The article pointed at by "lost" in this thread, shows many interesting workarounds to do it.
But I guess for now I'll stick to the if-then-elseif list.
Re: Is there a select statement?
Posted: Thursday 06 February 2025 9:13
by jvdz
Can't you simply store them in an array using the number as key? That way you should be able to get the command with array[key].
Re: Is there a select statement?
Posted: Thursday 06 February 2025 13:39
by manjh
jvdz wrote: ↑Thursday 06 February 2025 9:13
Can't you simply store them in an array using the number as key? That way you should be able to get the command with array[key].
Not sure what you mean. Store what in an array? Get what command?
Could you give a small example?
Re: Is there a select statement?
Posted: Thursday 06 February 2025 13:55
by FlyingDomotic
Idea, for a switch number between 3 and 6, to use something like:
Code: Select all
array[3] = "AAAA"
array[4] = "BBBB"
array[5] = "CCCC"
array[6] = "DDDD"
if number >= 3 and number <=6 then
print(array[number])
else
print("Number out of 3-6 range")
end
Re: Is there a select statement?
Posted: Thursday 06 February 2025 18:05
by HvdW
If - elseif:
Code: Select all
local x = 2 -- Example value
if x == 1 then
-- Code to execute when x is 1
print("x is 1")
elseif x == 2 then
-- Code to execute when x is 2
print("x is 2")
else
-- Code to execute for all other cases
print("x is something else")
end
or:
Code: Select all
local x = 2 -- Example value
local actions = {
[1] = function()
-- Code to execute when x is 1
print("x is 1")
end,
[2] = function()
-- Code to execute when x is 2
print("x is 2")
end
}
local action = actions[x] or function()
-- Code to execute for all other cases
print("x is something else")
end
action() -- Execute the corresponding function
like:
Code: Select all
return {
on = {
devices = { 'MyDevice' }
},
execute = function(domoticz, device)
local x = device.state -- Assuming the device state is a numeric value
local actions = {
[1] = function()
domoticz.log("x is 1", domoticz.LOG_INFO)
-- Additional actions for x == 1
end,
[2] = function()
domoticz.log("x is 2", domoticz.LOG_INFO)
-- Additional actions for x == 2
end
}
local action = actions[x] or function()
domoticz.log("x is something else", domoticz.LOG_INFO)
-- Additional actions for other cases
end
action() -- Execute the corresponding function
end
}
Re: Is there a select statement?
Posted: Friday 07 February 2025 9:08
by manjh
HvdW wrote: ↑Thursday 06 February 2025 18:05
If - elseif:
Code: Select all
local x = 2 -- Example value
if x == 1 then
-- Code to execute when x is 1
print("x is 1")
elseif x == 2 then
-- Code to execute when x is 2
print("x is 2")
else
-- Code to execute for all other cases
print("x is something else")
end
or:
Code: Select all
local x = 2 -- Example value
local actions = {
[1] = function()
-- Code to execute when x is 1
print("x is 1")
end,
[2] = function()
-- Code to execute when x is 2
print("x is 2")
end
}
local action = actions[x] or function()
-- Code to execute for all other cases
print("x is something else")
end
action() -- Execute the corresponding function
like:
Code: Select all
return {
on = {
devices = { 'MyDevice' }
},
execute = function(domoticz, device)
local x = device.state -- Assuming the device state is a numeric value
local actions = {
[1] = function()
domoticz.log("x is 1", domoticz.LOG_INFO)
-- Additional actions for x == 1
end,
[2] = function()
domoticz.log("x is 2", domoticz.LOG_INFO)
-- Additional actions for x == 2
end
}
local action = actions[x] or function()
domoticz.log("x is something else", domoticz.LOG_INFO)
-- Additional actions for other cases
end
action() -- Execute the corresponding function
end
}
Thank you! This is EXACTLY what I was looking for!

Re: Is there a select statement?
Posted: Thursday 13 February 2025 12:57
by manjh
For everyone that is interested, I found an elegant way to solve my coding problem.
It starts with a UserVariable that contains an integer value. For each unique value of that UserVariable, I create a function, called NFCn.
This is the code to call the right function, based on the uservariable:
Code: Select all
-- run through all changed variables
local myVariable = "NFC"
for variableName,variableValue in pairs(uservariablechanged) do
-- check if NFC value was changed
if (variableName == myVariable) then
-- found it. Now compose function name
local FunctionName = variableName..variableValue
-- if function exists, call it. Else report.
if (_G[FunctionName] ~= nil) then
_G[FunctionName]()
else
print(FunctionName.." does not exist")
end
end
end
This shows a very compact and elegant way to do it, much smaller than the if-then-elseif-etcetera construct!
(The functions are not shown in this code snippet, but are not difficult)
Re: Is there a select statement?
Posted: Thursday 13 February 2025 13:38
by Kedi
Why run through all changed variables, instead of
Code: Select all
return {
on = {
variables = { 'NFC' }
},
execute = function(dz, item)
... do stuff
end
}
Re: Is there a select statement?
Posted: Thursday 13 February 2025 14:57
by manjh
Kedi wrote: ↑Thursday 13 February 2025 13:38
Why run through all changed variables, instead of
Code: Select all
return {
on = {
variables = { 'NFC' }
},
execute = function(dz, item)
... do stuff
end
}
This is dzvents, I wanted to stick to pure LUA...
But you're right. Instead of looping through the whole list of uservariables, I could just check for uservariablechanged[myVariable].