how can I run a command from a variable?

Moderator: leecollings

Post Reply
manjh
Posts: 749
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

how can I run a command from a variable?

Post 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)()
Hans
User avatar
gizmocuz
Posts: 2552
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: how can I run a command from a variable?

Post 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
Quality outlives Quantity!
manjh
Posts: 749
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: how can I run a command from a variable?

Post 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.
Hans
User avatar
jvdz
Posts: 2335
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: how can I run a command from a variable?

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
zak45
Posts: 953
Joined: Sunday 22 January 2017 11:37
Target OS: Windows
Domoticz version: V2024.4
Contact:

Re: how can I run a command from a variable?

Post 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
manjh
Posts: 749
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: how can I run a command from a variable?

Post 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.
Hans
User avatar
gizmocuz
Posts: 2552
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: how can I run a command from a variable?

Post 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
Quality outlives Quantity!
manjh
Posts: 749
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: how can I run a command from a variable?

Post 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... :)
Hans
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: how can I run a command from a variable?

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
manjh
Posts: 749
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: how can I run a command from a variable?

Post 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!
Hans
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: how can I run a command from a variable?

Post by dannybloe »

Ah, indeed, but only if you have different settings.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
manjh
Posts: 749
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: how can I run a command from a variable?

Post 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! :)
Hans
User avatar
gizmocuz
Posts: 2552
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: how can I run a command from a variable?

Post 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 !
Quality outlives Quantity!
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest