Page 1 of 1
how can I run a command from a variable?
Posted: Sunday 12 November 2017 11:36
by manjh
I would like to set up a complete command in a variable, and then execute it.
For example:
Code: Select all
cmd_var = "commandArray['Lamp'] = 'On'"
Obviously this is a simple example. I want to build up a command in the logic of my LUA script
The missing bit is how to execute this command?
Is this possible, and if so: how?
Edit: I found the answer, not in the Domoticz Wiki or forum, but in a general LUA forum.
This is how to do it:
Code: Select all
cmd_var = "commandArray['Lamp'] = 'On'"
load(cmd_var)()
Re: how can I run a command from a variable?
Posted: Sunday 12 November 2017 11:52
by gizmocuz
Thats not possible.
But maybe explain why you need this for ... what are you trying to do ?
Maybe it is already possible with Blockly, dzVents, Lua, Python, Scenes/Groups
Re: how can I run a command from a variable?
Posted: Sunday 12 November 2017 14:04
by manjh
The example that I added to my first post actually works.
My purpose: I want to create a command in one LUA script, and put it in a user variable.
Another LUA time-drive script can then pick up this command and execute it for a number of iterations.
A second user variable contains the required number.
Reason behind this: I have one sript that I execute when going to bed at night, it is invoked by a button on my remote control..
The script will turn off all the lights in my living room by invoking the "all lights off" scene.
About half of these lights are controlled via RF (KAKU), and since the RF band is quite busy here it happens occasionally that some commands are "missed". This results in lights staying on, if I don't notice they will still be on in the morning. Bot very good for the WAF..
What I have done to fix this is: the "turnoff" script will put a command in a user variable, and set the required frequency in another user variable. It also invokes the "all lights off" script as usual.
The other time-driven script will notice the integer in the user variable. It will decrement that by one, and execute the command. Repeats until the integer is zero.
This way the lights are usually turned off by the first command, but for safety will be turned off again two more times with one minute intervals.
Sounds complicated, but it really isn't.
Because I put the command in the user variable, I will be able to re-use this setup for other purposes. Not sure if any.
Re: how can I run a command from a variable?
Posted: Sunday 12 November 2017 14:10
by jvdz
I've done the same by doing it this way:
Code: Select all
function ChangeSwitch(DeviceName, Command)
local dstatus = otherdevices[DeviceName]
local dvalue = otherdevices_svalues[DeviceName]
print("!### ChangeSwitch DeviceName:" ..DeviceName .." Command:" .. Command .. " (" .. ScriptName .. ")")
print(" Current status:" ..dstatus .." dvalue:" .. dvalue .. " (" .. ScriptName .. ")")
if dstatus == nil then
-- invalid switch
print(" ==> Invalid switchname " .. DeviceName .. " (" .. ScriptName .. ")")
else
print(" ==> Set " .. DeviceName .. " to " .. Command .. " inx=" .. #commandArray + 1 .. " (" .. ScriptName .. ")")
commandArray[#commandArray + 1] = {[DeviceName] = Command}
end
return
end
ChangeSwitch("Tuin-Kaku","Off")
ChangeSwitch("Tuin-Kaku","Off AFTER 5")
ChangeSwitch("Tuin-Kaku","Off AFTER 10")
The above is really making use of a standard function library I use in all my scripts, so the short direct version of that code is:
Code: Select all
commandArray[#commandArray + 1] = {"Tuin-Kaku" = "Off"}
commandArray[#commandArray + 1] = {"Tuin-Kaku" = "Off AFTER 5"}
commandArray[#commandArray + 1] = {"Tuin-Kaku" = "Off AFTER 10"}
Jos
Re: how can I run a command from a variable?
Posted: Sunday 12 November 2017 14:59
by zak45
had the same problem, solved by using LUA command :
"Off REPEAT 2 INTERVAL 5 SECONDS"
see :
https://www.domoticz.com/wiki/LUA_commands
Re: how can I run a command from a variable?
Posted: Sunday 12 November 2017 19:22
by manjh
Yes, I was hoping to do it that way. But the difference is that I want to invoke a scene. I don't think that supports this way.
Re: how can I run a command from a variable?
Posted: Monday 13 November 2017 8:40
by gizmocuz
This all can be done without scripting and using a scene
In the scene you can specify delays
Also how i use it, first turn on the bedroom light, next turn off all ground floor lights one by one (with a delay) the one closest to the stair last
If you do not trust your RF then you better connect a good antenna or position this on a more central place.
Or as mentioned, add a second OFF with a delay
Re: how can I run a command from a variable?
Posted: Monday 13 November 2017 10:28
by manjh
Brilliant. As usual, the best solutions are the simplest.
I never expected that it is allowed to use one device twice in a scene!
About RF: it is not a matter of trust, I know the reason why RF commands missed. I have five temp/hum sensors that transmit data about every 50 seconds or so. It is not a large amount of data, but still...
I've now modified the scene. We'll see if one repetition of RF commands is sufficient.
The good part: we have now learned how to put a command into a variable and execute it...

Re: how can I run a command from a variable?
Posted: Monday 13 November 2017 10:52
by dannybloe
manjh wrote: ↑Monday 13 November 2017 10:28
I never expected that it is allowed to use one device twice in a scene!
I don't think you can. A device can be added only once to a scene.
Re: how can I run a command from a variable?
Posted: Monday 13 November 2017 10:59
by manjh
dannybloe wrote: ↑Monday 13 November 2017 10:52
manjh wrote: ↑Monday 13 November 2017 10:28
I never expected that it is allowed to use one device twice in a scene!
I don't think you can. A device can be added only once to a scene.
I've done it just a few minutes ago. Duplicates are accepted with no argument!
Re: how can I run a command from a variable?
Posted: Monday 13 November 2017 11:02
by dannybloe
Ah, indeed, but only if you have different settings.
Re: how can I run a command from a variable?
Posted: Monday 13 November 2017 11:03
by manjh
manjh wrote: ↑Monday 13 November 2017 10:59
dannybloe wrote: ↑Monday 13 November 2017 10:52
manjh wrote: ↑Monday 13 November 2017 10:28
I never expected that it is allowed to use one device twice in a scene!
I don't think you can. A device can be added only once to a scene.
I've done it just a few minutes ago. Duplicates are accepted with no argument!
Just to be sure I tested it: works great!

Re: how can I run a command from a variable?
Posted: Monday 13 November 2017 14:42
by gizmocuz
@danny, you can even make a good morning light with a scene.... 1%, 3%, 5%, 10%, 15%, 25%, 50%, 100%...
it makes no sense to add the same device with the same delay no...
Great it's working !