-
-
Notifications
You must be signed in to change notification settings - Fork 133
Description
I assume I am doing something wrong and would like to confirm if users have had success using multiple devices with unique ID's and unique device names but with identical Entity names. If yes, then I will confidently assume I am doing something wrong or my setup is flawed if not read on.
I refer to this
HASensorNumber myMillis("newdMillis", HASensorNumber::PrecisionP0);
as an Entity.
Perhaps it is simply a case that all devices need unique Entity names along with unique device id's and unique device names. However, I dont think that is the intent.
I have three ESP8266, all with unique ID's, unique device names but the same entity name. Device names are "leftleftleft", "middlemiddle" and "rightrightright". (Names chosen to help make it easier to debug, its where they sit on my bench.)
However, Only two show up.
If I delete all the devices in HA and and reset the esp's then I will get either "leftleftleft" or "middlemiddle" depending on which one boots up first and "rightrightright"
If the name of the entity (aka sensor) is made unique then they all show up but this does not seem like it is required or the intent, why else have unique device ids if you also have to have unique entity names. I am thinking it is an issue with HA and not the ArduinoHA library, or maybe its something I am doing wrong.
i.e the Entity name is set like this myMillis.setName("newXXXMillis");
if I change the entity name "newXXXMillis" to something different then it works and all three devices with the correct device names show up in HA
Looking at the log file and subscribing to monitor all mqtt activity then I see whats expected, i.e the unique id's and the related mqtt publishing from the 3 devices.
I have tried many permutations of unique device ID's, stop/starting mqtt, deleting the devices in HA and rebooting the devices but I cant get them all to show up unless I also use unique entity names.
Here is the code for the device named "leftleftleft", obviously I change the device name and the unique ID is automagically made unique using the MAC address. I have debug code to check they are in fact unique.
`#include <ESP8266WiFi.h>
#include <ArduinoHA.h>
#define LED_PIN D0
#define BROKER_ADDR "homeassistant.local"
#define WIFI_SSID "xxxxxx"
#define WIFI_PASSWORD "xxxxxx"
#define MQTTUSER "xxxxxx"
#define MQTTCRED "xxxxxxxx"
WiFiClient client;
HADevice device;
HAMqtt mqtt(client, device);
// "led" is unique ID of the switch. You should define your own ID.
HASwitch led("led");
HASensorNumber myMillis("newdMillis", HASensorNumber::PrecisionP0);
void onSwitchCommand(bool state, HASwitch* sender)
{
digitalWrite(LED_PIN, (state ? HIGH : LOW));
sender->setState(state); // report state back to the Home Assistant
}
void setup() {
Serial.begin(115200);
Serial.println("Starting...");
// Unique ID must be set!
byte myId[] = {0x01, 0xb2, 0xc3, 0x44, 0xb3, 0xc3, 0x83, 0x13, 0x02};
WiFi.macAddress(myId); //I overlay the MAC address over the first 6 bytes creating uniqueness. I have tried manual entering unique bytes for the three devices without sucess
device.setUniqueId(myId, sizeof(myId)); // the input array is cloned internally
for(int i=0; i<sizeof(myId); i++)
{
printHex(myId[i]);
}
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
// connect to wifi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500); // waiting for the connection
}
Serial.println();
Serial.println("Connected to the network");
// set device's details (optional)
device.setName("leftleftleft"); //This is changed for the other 2 devices
device.setSoftwareVersion("1.0.0");
device.setManufacturer("xxxxxx");
device.setModel("ESP8266");
// handle switch state
led.onCommand(onSwitchCommand);
led.setName("My LED"); // optional
myMillis.setIcon("mdi:timelapse");
myMillis.setName("newXXXMillis");
myMillis.setUnitOfMeasurement("mS");
//mqtt.begin(BROKER_ADDR);
mqtt.begin(BROKER_ADDR, MQTTUSER, MQTTCRED);
}
unsigned long previousMillis;
unsigned long intervalSendUpdates = 5000;
void loop()
{
mqtt.loop();
if ( millis() - previousMillis >= intervalSendUpdates)
{
previousMillis += intervalSendUpdates;
myMillis.setValue((int)millis());
}
state you want
}
void printHex(uint8_t num) {
char hexCar[2];
sprintf(hexCar, "%02X", num);
Serial.print(hexCar);
}
`