Page 1 of 1
IDX instead of switchnames in LUA?
Posted: Wednesday 30 March 2016 15:49
by Bikey
I thought I've read this earlier in the forum, but can't find it, so forgive me if this have been answered before:
My problem is:
In LUA the actual names of devices have to be used for for commands, like commandArray['MySwitchName'] = 'Off'
Problem is, if you change the name of your device, this will break all LUA-scripts.
So question is: is there a way to use the IDX-value instead of the names in your LUA-scripts? This is also how BLockly works.
Re: IDX instead of switchnames in LUA?
Posted: Saturday 02 April 2016 12:58
by Bikey
This would think this is a common "problem", has anybody found a solution to make your scripts resilient against changing device names?
Re: IDX instead of switchnames in LUA?
Posted: Saturday 02 April 2016 13:36
by tozzke
not really that I know of.... except for creating local variables at the top of your script and use them instead of the full switch names. In this way you'll only have to change that the name of the switch once at each script
Re: IDX instead of switchnames in LUA?
Posted: Monday 04 April 2016 11:49
by mrf68
I like the fact that I can use the names instead of the IDX. One of my contacts didn't work properly anymore. I had a spare, placed it at the door, used the name of the old one (after renaming the old one first) and started to use it. I don't need to go through all the scripts, it just works. So I wouldn't vote for IDX instead of names, I could live with IDX AND names.
Re: IDX instead of switchnames in LUA?
Posted: Monday 04 April 2016 12:30
by Bikey
tozzke wrote:not really that I know of.... except for creating local variables at the top of your script and use them instead of the full switch names. In this way you'll only have to change that the name of the switch once at each script
Yeah, that is how I do this now as well. Would even be better if you could have 1 place to maintain al those assignments for all scripts. I'm just starting with LUA, but I imagine that you could use something like an include file?
Re: IDX instead of switchnames in LUA?
Posted: Saturday 09 April 2016 17:40
by jmleglise
Re: IDX instead of switchnames in LUA?
Posted: Saturday 09 April 2016 18:05
by jvdz
Something like this should also work using the new otherdevices_idx[] table in LUA to get the current devicename for an IDX.
untested code example:
Code: Select all
function getdevname4idx(deviceIDX)
for i, v in pairs(otherdevices_idx) do
if v == deviceIDX then
return i
end
end
return 0
end
commandArray[getdevname4idx(123)] = 'Off'
Jos
Re: IDX instead of switchnames in LUA?
Posted: Friday 17 February 2017 22:23
by supermat
jvdz wrote:Something like this should also work using the new otherdevices_idx[] table in LUA to get the current devicename for an IDX.
untested code example:
Code: Select all
function getdevname4idx(deviceIDX)
for i, v in pairs(otherdevices_idx) do
if v == deviceIDX then
return i
end
end
return 0
end
commandArray[getdevname4idx(123)] = 'Off'
Jos
Thank you JVDZ this is what I am searching.
Re: IDX instead of switchnames in LUA?
Posted: Friday 12 May 2017 21:08
by SjoerdNLD
I was also searching for this; Now using name for my scripts, instead of IDX.
You can just use: otherdevices_idx["Bathroom Light"]
Source:
http://www.domoticz.com/forum/viewtopic.php?t=12010