IDX instead of switchnames in LUA?

Moderator: leecollings

Post Reply
Bikey
Posts: 331
Joined: Sunday 22 February 2015 12:19
Target OS: Linux
Domoticz version: 2020.x
Location: Netherlands
Contact:

IDX instead of switchnames in LUA?

Post 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.
Bikey
Posts: 331
Joined: Sunday 22 February 2015 12:19
Target OS: Linux
Domoticz version: 2020.x
Location: Netherlands
Contact:

Re: IDX instead of switchnames in LUA?

Post by Bikey »

This would think this is a common "problem", has anybody found a solution to make your scripts resilient against changing device names?
tozzke
Posts: 135
Joined: Friday 02 January 2015 9:22
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands
Contact:

Re: IDX instead of switchnames in LUA?

Post 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
mrf68

Re: IDX instead of switchnames in LUA?

Post 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. ;)
Bikey
Posts: 331
Joined: Sunday 22 February 2015 12:19
Target OS: Linux
Domoticz version: 2020.x
Location: Netherlands
Contact:

Re: IDX instead of switchnames in LUA?

Post 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?
jmleglise
Posts: 192
Joined: Monday 12 January 2015 23:27
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: FRANCE
Contact:

Re: IDX instead of switchnames in LUA?

Post by jmleglise »

loadfile

look here for howto use it https://www.domoticz.com/wiki/Lua_-_json.lua
My script : https://github.com/jmleglise
RFXTRX433E: Blind Somfy RTS, Portal Somfy Evolvia, chacon IO, Oregon, PIR sensor PT2262
My Last project : Location de maison de vacances a Ouistreham vue mer
KMTronic USB relay
Chinese Z-WAVE: Neo CoolCam
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: IDX instead of switchnames in LUA?

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
supermat
Posts: 14
Joined: Thursday 07 April 2016 18:57
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: IDX instead of switchnames in LUA?

Post 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.
SjoerdNLD
Posts: 6
Joined: Friday 12 May 2017 20:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: IDX instead of switchnames in LUA?

Post 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"] :roll:

Source: http://www.domoticz.com/forum/viewtopic.php?t=12010
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest