paulino / ha-mqtt-entities

Easy Home Assistant MQTT Integration Library
Apache License 2.0
5 stars 1 forks source link

Conflict with more devices #2

Closed proasnet closed 1 month ago

proasnet commented 1 month ago

Dear @paulino , When using one device to MQTT server the library works. As soon as I added second hardware device, there was a conflict between them. In the MQTT explorer, I saw that for each message from each sensor, the values ​​in the message were quickly swapped alternately between the 2 devices, there were not 2 separate sensors, there was only one, and it was quickly changing the value from one and the other device. In HA, only one device was created, and the values ​​of each entity quickly alternated according to the values ​​from one device and from second device. I think this is a major bug in the library.

Do you have any idea how to solve the problem please? Thank you

proasnet commented 1 month ago

I noticed one more difference from the Arduino Home Assistant integration library. It is a tree layout in the MQTT explorer. While your library creates a sensor, and then contains a configuration message, the "home assistant integration" library does the opposite. There is a device in the tree and there are sensors in it. I can't use the Arduino Home Assistant integration library, it doesn't work on one ESP32 with Wifi/ethernet to make it universal. Your library can do that, that's why I chose it.

Please see a attached video from MQTT explorer, you see a device ID and name as switching values.

https://github.com/user-attachments/assets/eec697da-737b-4035-bb7f-91b3c7637e5a

paulino commented 1 month ago

I'll try to reproduce the bug in a test.

The MQTT tree layout follows the examples from HomeAssistant at https://www.home-assistant.io/integrations/mqtt/#sensors

proasnet commented 1 month ago

Thank you. I had the same code in two ESP32s... As I public in this issue https://github.com/paulino/ha-mqtt-entities/issues/1 I filling identificators in a setup(), I don't know if this is causing the problem.

proasnet commented 1 month ago

Thank you. I had the same code in two ESP32s... As I public in this issue #1 I filling identificators in a setup(), I don't know if this is causing the problem.

Now I try your example "sensornumeric", only credentials, unique ID and name changed. In HA was created two devices, but only the first has example entity. The second device is empty.

https://github.com/user-attachments/assets/b9b67448-e7cf-416f-bc43-64e69f757577

obrazek obrazek

paulino commented 1 month ago

The problem is the entity's unique_id, they generate conflict with multiple devices. There is a new commit in the main branch. Please clone or update the repository in your local arduino installation and test again.

I've changed the library to generate "unique_id" from "device_id".

I also added a new function for #1

I tested this example and it works fine now:

#define ENTITIES_COUNT 2
HADevice ha_device_1 = HADevice("1.0");
HANumber ha_number_1 = HANumber("numberexample","Slicer",ha_device_1,1,100,1);

HADevice ha_device_2 = HADevice("1.0");
HANumber ha_number_2 = HANumber("numberexample","Slicer",ha_device_2,1,100,1);

void setup() {
    Serial.begin(115200);
    mqtt_client.setServer(MQTT_SERVER, MQTT_PORT);

    // Delayed device configuration
    ha_device_1.setName("Example 01: Device 1");
    ha_device_1.setIdentifier("device01");
    ha_device_2.setName("Example 01: Device 2");
    ha_device_2.setIdentifier("device02");

    HAMQTT.begin(mqtt_client,ENTITIES_COUNT);
    HAMQTT.addEntity(ha_number_1);
    HAMQTT.addEntity(ha_number_2);
    ha_number_1.setState(30);
    ha_number_2.setState(90);
    HAMQTT.setCallback(ha_callback);

    // start wifi
    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}
proasnet commented 1 month ago

Thank you @paulino , I am tried example and working fine. I edit my full code, where is a HAText, and compille error

C:\Users\j.jirutka\Documents\Arduino\libraries\HaMqttEntities\src/hatext.cpp:55: multiple definition of `HAText::onReceivedTopic(PubSubClient*, unsigned char*, unsigned int)'; sketch\src\HaMqttEntities\src\hatext.cpp.o:Z:\EKOKROK\11_TPV\01 ELEKTRO\05 Výrobné podklady Arduino Nextion a RPI\Riadiace jednotky\Arduino Basic Profi (Nové)\02_VÝVOJOVÁ_VERZE\Program_ESP32\esp_comunicator\src\HaMqttEntities\src/hatext.cpp:55: first defined here
c:/users/j.jirutka/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32-elf-gcc/esp-2021r2-patch5-8.4.0/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: libraries\HaMqttEntities\hatext.cpp.o:(.data._ZN6HAText9componentE+0x0): multiple definition of `HAText::component'; sketch\src\HaMqttEntities\src\hatext.cpp.o:(.data._ZN6HAText9componentE+0x0): first defined here
c:/users/j.jirutka/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32-elf-gcc/esp-2021r2-patch5-8.4.0/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: libraries\HaMqttEntities\hatext.cpp.o: in function `HAText::HAText(char const*, char const*, unsigned int)':
C:\Users\j.jirutka\Documents\Arduino\libraries\HaMqttEntities\src/hatext.cpp:17: multiple definition of `HAText::HAText(char const*, char const*, unsigned int)'; sketch\src\HaMqttEntities\src\hatext.cpp.o:Z:\EKOKROK\11_TPV\01 ELEKTRO\05 Výrobné podklady Arduino Nextion a RPI\Riadiace jednotky\Arduino Basic Profi (Nové)\02_VÝVOJOVÁ_VERZE\Program_ESP32\esp_comunicator\src\HaMqttEntities\src/hatext.cpp:17: first defined here
c:/users/j.jirutka/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32-elf-gcc/esp-2021r2-patch5-8.4.0/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: libraries\HaMqttEntities\hatext.cpp.o: in function `HAText::HAText(char const*, char const*, unsigned int)':
C:\Users\j.jirutka\Documents\Arduino\libraries\HaMqttEntities\src/hatext.cpp:17: multiple definition of `HAText::HAText(char const*, char const*, unsigned int)'; sketch\src\HaMqttEntities\src\hatext.cpp.o:Z:\EKOKROK\11_TPV\01 ELEKTRO\05 Výrobné podklady Arduino Nextion a RPI\Riadiace jednotky\Arduino Basic Profi (Nové)\02_VÝVOJOVÁ_VERZE\Program_ESP32\esp_comunicator\src\HaMqttEntities\src/hatext.cpp:17: first defined here
c:/users/j.jirutka/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32-elf-gcc/esp-2021r2-patch5-8.4.0/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: libraries\HaMqttEntities\hatext.cpp.o: in function `HAText::HAText(char const*, char const*, HADevice&, unsigned int)':
C:\Users\j.jirutka\Documents\Arduino\libraries\HaMqttEntities\src/hatext.cpp:11: multiple definition of `HAText::HAText(char const*, char const*, HADevice&, unsigned int)'; sketch\src\HaMqttEntities\src\hatext.cpp.o:Z:\EKOKROK\11_TPV\01 ELEKTRO\05 Výrobné podklady Arduino Nextion a RPI\Riadiace jednotky\Arduino Basic Profi (Nové)\02_VÝVOJOVÁ_VERZE\Program_ESP32\esp_comunicator\src\HaMqttEntities\src/hatext.cpp:11: first defined here
c:/users/j.jirutka/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32-elf-gcc/esp-2021r2-patch5-8.4.0/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: libraries\HaMqttEntities\hatext.cpp.o: in function `HAText::HAText(char const*, char const*, HADevice&, unsigned int)':
C:\Users\j.jirutka\Documents\Arduino\libraries\HaMqttEntities\src/hatext.cpp:11: multiple definition of `HAText::HAText(char const*, char const*, HADevice&, unsigned int)'; sketch\src\HaMqttEntities\src\hatext.cpp.o:Z:\EKOKROK\11_TPV\01 ELEKTRO\05 Výrobné podklady Arduino Nextion a RPI\Riadiace jednotky\Arduino Basic Profi (Nové)\02_VÝVOJOVÁ_VERZE\Program_ESP32\esp_comunicator\src\HaMqttEntities\src/hatext.cpp:11: first defined here
c:/users/j.jirutka/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32-elf-gcc/esp-2021r2-patch5-8.4.0/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: libraries\HaMqttEntities\hatext.cpp.o: in function `HAText::setState(char const*)':
C:\Users\j.jirutka\Documents\Arduino\libraries\HaMqttEntities\src/hatext.cpp:43: multiple definition of `HAText::setState(char const*)'; sketch\src\HaMqttEntities\src\hatext.cpp.o:Z:\EKOKROK\11_TPV\01 ELEKTRO\05 Výrobné podklady Arduino Nextion a RPI\Riadiace jednotky\Arduino Basic Profi (Nové)\02_VÝVOJOVÁ_VERZE\Program_ESP32\esp_comunicator\src\HaMqttEntities\src/hatext.cpp:43: first defined here
collect2.exe: error: ld returned 1 exit status

In static global I have this text definitions

HASensorText ha_text_atmel_fw_ver = HASensorText( "fw_ver" , "I-01 Atmel FW" , ha_device , 5 );
HASensorText ha_text_serial_number = HASensorText( "serial_number", "I-02 SN" , ha_device , 20 );
HASensorText ha_text_chod_m1 = HASensorText( "m1_run" , "I-08 M1 Run" , ha_device , 12 );
HASensorText ha_text_chod_m2 = HASensorText( "m2_run" , "I-10 M2 Run" , ha_device , 12 );
HASensorText ha_text_pozice_okna = HASensorText( "win_position" , "I-21 Window pos." , ha_device , 20 );
HASensorText ha_text_servis_datum = HASensorText( "service_date" , "I-22 Service date" , ha_device , 12 );
HASensorText ha_text_validace_datum = HASensorText( "validation_date" , "I-24 Valid date" , ha_device , 12 );
HASensorText ha_text_mod = HASensorText( "mod" , "I-26 Mode" , ha_device , 12 );
HASensorText ha_text_stabilized = HASensorText( "stabilized" , "I-28 Stabilized" , ha_device , 12 );
HASensorText ha_text_alarm = HASensorText( "alarms_hex" , "I-50 Alarms HEX" , ha_device , 12 );

EDIT: This on all types, not only text.

paulino commented 1 month ago

Maybe a problem in your Arduino installation, you may have the library twice.

Check if is Arduino/library folder twice, or if you have in your project under "src" folder and again in "Arduino/library"

proasnet commented 1 month ago

I will check it after 5.8., I am leaving work now to hollidays. Thank you very much again.

proasnet commented 1 month ago

Hello @paulino , holidays are over and today I tried your new version with 2 identical devices connected at the same time. From the initial test it seems to work great. Thanks for your help. I will look forward to further improvements.