We have moved at community.getvera.com

Author Topic: Alias names for devices ID  (Read 1022 times)

Offline UKsub

  • Jr. Member
  • **
  • Posts: 51
  • Karma: +0/-1
Alias names for devices ID
« on: January 14, 2016, 07:42:57 am »
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...
Code: [Select]
--get virtual switch state
local VirtualSwitchState = luup.variable_get("urn:upnp-org:serviceId:VSwitch1", "Status", 30)

with the following
Code: [Select]
--get virtual switch state
local VirtualSwitchState = luup.variable_get("urn:upnp-org:serviceId:VSwitch1", "Status", Dev_PorchLight)

and insert into the startup code:
Code: [Select]
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
« Last Edit: January 14, 2016, 08:00:43 am by UKsub »

Offline jlind

  • Full Member
  • ***
  • Posts: 235
  • Karma: +12/-6
Re: Alias names for devices ID
« Reply #1 on: January 14, 2016, 09:33:47 am »
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. 
VeraLite/VeraPlus with UI7, Multiple GE switches, GE Outlets, Aeon Smart Switches, Minimote, GE Portable outlets  Apps: (Pentair Autelis Plugin, Weather Underground, Honeywell WiFi Thermo, System Monitor, AlternateUI)

Offline aa6vh

  • Hero Member
  • *****
  • Posts: 642
  • Karma: +15/-0
Re: Alias names for devices ID
« Reply #2 on: January 14, 2016, 10:16:48 am »
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:

Code: [Select]
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:

Code: [Select]
RunScene(gMyScene)

Offline UKsub

  • Jr. Member
  • **
  • Posts: 51
  • Karma: +0/-1
Re: Alias names for devices ID
« Reply #3 on: January 15, 2016, 04:49:27 pm »
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?