Is there a select statement?
Moderator: leecollings
-
- Posts: 748
- Joined: Saturday 27 February 2016 12:49
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2020.2
- Location: NL
- Contact:
Is there a select statement?
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?
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?
Hans
-
- Posts: 575
- Joined: Monday 20 March 2023 14:41
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Location: Somewhere in NL
- Contact:
Re: Is there a select statement?
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?
What would be the benefit over "if then elseif end" method?
Logic will get you from A to B. Imagination will take you everywhere.
-
- Posts: 660
- Joined: Thursday 10 November 2016 9:30
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Is there a select statement?
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.
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.
-
- Posts: 748
- Joined: Saturday 27 February 2016 12:49
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2020.2
- Location: NL
- Contact:
Re: Is there a select statement?
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.
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.
Hans
- jvdz
- Posts: 2328
- Joined: Tuesday 30 December 2014 19:25
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 4.107
- Location: Netherlands
- Contact:
Re: Is there a select statement?
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].
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
-
- Posts: 748
- Joined: Saturday 27 February 2016 12:49
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2020.2
- Location: NL
- Contact:
Re: Is there a select statement?
Not sure what you mean. Store what in an array? Get what command?
Could you give a small example?
Hans
-
- Posts: 356
- Joined: Saturday 27 February 2016 0:30
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2020.2
- Contact:
Re: Is there a select statement?
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
-
- Posts: 612
- Joined: Sunday 01 November 2015 22:45
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2023.2
- Location: Twente
- Contact:
Re: Is there a select statement?
If - elseif:
or:
like:
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
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
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
}
Bugs bug me.
-
- Posts: 748
- Joined: Saturday 27 February 2016 12:49
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2020.2
- Location: NL
- Contact:
Re: Is there a select statement?
Thank you! This is EXACTLY what I was looking for!HvdW wrote: ↑Thursday 06 February 2025 18:05 If - elseif:or: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
like: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
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 }

Hans
-
- Posts: 748
- Joined: Saturday 27 February 2016 12:49
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2020.2
- Location: NL
- Contact:
Re: Is there a select statement?
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:
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)
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
(The functions are not shown in this code snippet, but are not difficult)
Hans
-
- Posts: 575
- Joined: Monday 20 March 2023 14:41
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Location: Somewhere in NL
- Contact:
Re: Is there a select statement?
Why run through all changed variables, instead of
Code: Select all
return {
on = {
variables = { 'NFC' }
},
execute = function(dz, item)
... do stuff
end
}
Logic will get you from A to B. Imagination will take you everywhere.
-
- Posts: 748
- Joined: Saturday 27 February 2016 12:49
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2020.2
- Location: NL
- Contact:
Re: Is there a select statement?
This is dzvents, I wanted to stick to pure LUA...Kedi wrote: ↑Thursday 13 February 2025 13:38 Why run through all changed variables, instead ofCode: Select all
return { on = { variables = { 'NFC' } }, execute = function(dz, item) ... do stuff end }
But you're right. Instead of looping through the whole list of uservariables, I could just check for uservariablechanged[myVariable].
Hans
Who is online
Users browsing this forum: Google [Bot] and 1 guest