Alias names for devices ID

I’ve found a few articles scattered about that may cover this but wanted to get a few thoughts on this idea.

Having to refer to devices by their id number is the usual way to do things and can sometimes lead to errors when there are many devices. More so if you have to replace a faulty device and go through all of your coding to search and replace that device number with the modified value.

Could we delcare a variable in the startup code that references a value and therefore any future modfications can be quick and easy besides giving a more meaningful name to a device.

example:
replace this code within a scene or wherever…

--get virtual switch state
local VirtualSwitchState = luup.variable_get("urn:upnp-org:serviceId:VSwitch1", "Status", 30)

with the following

--get virtual switch state
local VirtualSwitchState = luup.variable_get("urn:upnp-org:serviceId:VSwitch1", "Status", Dev_PorchLight)

and insert into the startup code:

Dev_PorchLight=30 --textual representation of device number

dont think i need to declare it as “global Dev_PorchLight=30” as it will be global by default because its in the startup code?

Could anyone advance on this suggestion or comment if this is not advisable in anyway, please?
thanks

This is a very common way of doing it and is basically considered somewhat a programming standard. It’s much easier to handle changes and what not and also makes the code easier to read. One thing you may want to do is to come up with a naming convention that differentiates global variables from local variables. For instance some people use all caps for global, or start every variable with a “g”, etc.

I go one step further with this concept. I place in the startup code functions that have code that is complex. For example, I have the following function in my startup code for running scenes:

function RunScene(scnnum)
  luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1","RunScene",{ SceneNum = scnnum },0)
end

So if I have a scene that needs to invoke another scene, I can just use the code:

RunScene(gMyScene)

Thanks for the tips. Helpful.
I also attempted to do a remote command control (over the web via fwd mios sever) but changing the device ID number used in the url for the variable name I set up in the startup code. It appeared to work.
Is this also a good/safe strategy?