Duplicate Device Names in HS4
The Problem with HS4 Device Reference Numbers
The only truly unique HomeSeer Device identifier is the Referenece Number assigned by HomeSeer when a Device or Feature is created. While no two Device or Features will have the same Reference Number, there is no guarentee that a Device will always have the same Reference Number in the future. If a Device is deleted and then recreated, a different Reference Number will be assigned. Too often this happens when HomeSeer must be reinstalled. More frequently, it can happen when one or more plugins must be removed and reinstalled, or if a Device is inadvertantly deleted.
Because HS Events reference devices only by their Reference Numbers, these Events break when new Reference Numbers are assigned. This is also a problem for any script that relies on Device Reference Numbers. Fortunately, there are other options for referencing Devices within scripts that are more likely to remain constant through time.
Using Device Name
Many script writers will depend upon the Device Name instead of the Reference Number. The same Name will usually by assigned to a Device each time a plugin creates it, so it should be constant through time. hs.GetDeviceRefByName() is used to obtain the Device Reference Number. The parameter passed to GetDeviceRefByName is not just the Name field for the Device, but must also include both the Location and Location2 for the Device. A Fully Qualified Name (FQN) is created like this:
fqn= dev.Location2 & " " & dev.Location & " " & dev.Name
There is always a chance that more than one Device will be defined with the same FQN. When this is the case, there is no way to determine which Device Reference Number will be returned. As long as you know there are no duplicate names defined for the Devices/Features you will use in your scripts, all is good.
In HS3, it was very unlikely that your HS system would contain many duplicate names (that is, FQNs). But this all changed in HS4 with the introduction of the concept of Devices (root) and Features (child). I am seeing many cases where plugins are defining multiple Features with the same FQN. An example would be multiple Devices defined by a weather plugin for each of the next 10 days. Each of these Devices have multiple Features for things like Temperature, Humidity, etc. If each of these Features are defined with the same name, ie Temperature, it is uncertain which Device Reference Number will be returnd by GetDeviceRefByName.
Using Device Address
Each Device or Feature also can be assigned a Device Address. When a plugin creates a Device/Feature and assigns an Address to it, it is most likely defined in a way to be unique and the same Address will be assigned every time that Device is created. Unfortunately, many plugins do not assign an Address at all, and it is defaulted to null. If your script is referencing Devices/Features that have defined Addresses, this is the best way to obtain the current Reference Number using hs4.GetDeviceByAddress or hs4.GetFeatureByAddress.
Using GetFeatureRefByFullname
There is a third option that can be used for Devices/Features that do not have an Address assigned and do not have unique FQNs.
In HS4, many Features are created with the same FQN. However, they usually have parent Devices that have different FQNs. The combination of parent Device FQN and child Feature FQN is usually unique across HS4. tenGlobalMethods provides the GetFeatureRefByFullname method that takes the FQN of the parent Device and the FQN of the child Feature and returns the Reference Number. You can use tenScriptAid to build the code to call GetFeatureRefByFullname for any Device/Feature so you do not have to manually code both FQNs for the call, eliminating keying errors.
Checking Your HS4 System for Duplicates
Three seperate HS4 scripts are available:
FindDuplicateNames that will identify all hs.GetRefByName duplicates
FindDuplicateAddresses that will identify all HS4 Devices/Features with the same Address
FindDuplicateFullnames that will identify all GetFeatureRefByFullname duplicates [Requires tenGlobalMethods]
To check for duplicates on your HS4 system, download FindDuplicates.zip and copy the three included scripts into your HS4 scripts folder, and then create an Event that runs each of these scripts. The results will be generated into text files on your desktop.
Video detailing how to use these scripts: CLICK HERE