Running multiple instances of the same code in HA #264
Replies: 1 comment
-
I made a workaround - seems like the global uniqueness of entity UniqueIDs in HA may be to do with the MQTT broker? Hard to say. The result is that if I want to run the same code on multiple devices, I need to dynamically create these IDs. The problem with that is, since all the parameters passed for names and IDs in the homeassistant library are pointers, the strings you need to use have to be globally persistent and unchanging in their pointers. This means that the address of the string has to stay the same throughout the lifetime of the unit. This gets more complex if you are using a c++ object and initialising these entites in the object construction. For Example: As such constructing a unique ID like this does not work since the c_str() pointer is ephemeral, and changes to random stuff in nearby memory (like wifi passwords):
As a workaround I had to write (some ugly) code to set up a static array of strings and store them with immutable pointers.
Would it not be better for the homeassistant module to make local copies of all these parameters, and have them passed by value, so that we can use simpler client code? M Note: you can't use vectors here since they move the strings in memory when they need more space.
|
Beta Was this translation helpful? Give feedback.
-
I am trying to run the same code a number of times (multiple temperature sensors on Arduino Nano IoT 33's) and just use the preferences storage for things like device IDs etc. This all works fine, I'm using the mac address of the device as the uniqueID for the device, but I'm having problems with uniqueIDs of the entities on the devices. Each device has one entity with a uniquie ID (set as const *char at the constructor) of "temperature". However when I plug in multiple devices, then I get complaints from HA that the entity ID is not unique.
Looking at the library C++ code, I'm thinking that I need to be using the objectID?
Would the following description for this be accurate?
There seems to be some overlap between setting the uniqueId in the constructor and using the setObjectId method. In the context of the Arduino Home Assistant library, there's a subtle but important distinction between these two:
uniqueId in Constructor
Purpose: The uniqueId passed to the constructor (e.g., HASensor("temperature")) is primarily used to create a unique identifier for the device type object itself within the Arduino code. It helps distinguish this specific sensor or switch object from other objects of the same type you might have on your Arduino.
Internal Use: This uniqueId is mainly used internally by the library for managing the various device type objects and their associated MQTT topics.
setObjectId Method
Purpose: The setObjectId method is used to set the "Object ID" portion of the entity ID that will be generated in Home Assistant. This Object ID, combined with the integration domain (e.g., "sensor") forms the complete entity ID used to identify and control the component in Home Assistant.
External Representation: The objectId you set with this method directly influences how the component is represented and addressed in Home Assistant's configuration, automations, and user interface.
Key Differences
Scope:
uniqueId (constructor): Primarily for internal use within the Arduino code.
objectId (setObjectId): Directly affects the entity ID in Home Assistant.
Uniqueness:
uniqueId (constructor): Needs to be unique among device type objects of the same type on your Arduino.
objectId (setObjectId): Needs to be unique across all entities in your entire Home Assistant setup.
Flexibility:
uniqueId (constructor): Usually set once during object creation and might be less flexible to change later.
objectId (setObjectId): Can potentially be changed dynamically if needed, although it's generally recommended to keep it consistent.
Recommendation
Always use setObjectId to set a meaningful and unique identifier for each component you want to expose to Home Assistant. This will ensure clear and manageable entity IDs.
Use the uniqueId in the constructor primarily for internal organization within your Arduino code if you have multiple objects of the same type.
By understanding this distinction, you can effectively manage the identification of your Arduino-based components both within your code and in Home Assistant.
Beta Was this translation helpful? Give feedback.
All reactions