@@ -437,13 +437,14 @@ void loop()
437437 }
438438 notificationLED (); // notification LED routine
439439
440- if ((haDiscTrigger || _settings.data .haDiscovery ) && measureJson (Json) > jsonSize)
440+ if ((haDiscTrigger || _settings.data .haDiscovery ) && measureJson (Json) > jsonSize)
441+ {
442+ if (sendHaDiscovery ())
441443 {
442- if (sendHaDiscovery ()){
443444 haDiscTrigger = false ;
444445 jsonSize = measureJson (Json);
445- }
446446 }
447+ }
447448 }
448449
449450 if (restartNow && millis () >= (RestartTimer + 500 ))
@@ -474,11 +475,11 @@ bool getJsonData()
474475
475476 // jsonESP["Flash_Size"] = ESP.getFlashChipSize();
476477 // jsonESP["Sketch_Size"] = ESP.getSketchSize();
477- // jsonESP["Free_Sketch_Space"] = ESP.getFreeSketchSpace();
478+ // jsonESP["Free_Sketch_Space"] = ESP.getFreeSketchSpace();
478479 // jsonESP["Real_Flash_Size"] = ESP.getFlashChipRealSize();
479- // jsonESP["Free_Heap"] = ESP.getFreeHeap();
480- // jsonESP["HEAP_Fragmentation"] = ESP.getHeapFragmentation();
481- // jsonESP["WS_Clients"] = ws.getClients();
480+ // jsonESP["Free_Heap"] = ESP.getFreeHeap();
481+ // jsonESP["HEAP_Fragmentation"] = ESP.getHeapFragmentation();
482+ // jsonESP["WS_Clients"] = ws.getClients();
482483 // jsonESP["Free_BlockSize"] = ESP.getMaxFreeBlockSize();
483484
484485 // Serial.println();
@@ -528,20 +529,20 @@ bool getJsonData()
528529 {
529530 Json[FPSTR (VePrettyData[j][1 ])] = myve.veValue [i];
530531 }
531-
532- // if the Name Device_Model, search in the list for the device code
533- if (strcmp (VePrettyData[j][1 ], " Device_model" ) == 0 )
534- {
535- for (size_t k = 0 ; k < sizeof (VeDirectDeviceList) / sizeof (**VeDirectDeviceList[0 ]); k++)
536- {
537- if (strcmp (VeDirectDeviceList[k][0 ], myve.veValue [i]) == 0 )
538- {
539- Json[FPSTR (VePrettyData[j][1 ])] = FPSTR (VeDirectDeviceList[k][1 ]);
540- break ;
541- }
542- }
543- }
544-
532+
533+ // if the Name Device_Model, search in the list for the device code
534+ if (strcmp (VePrettyData[j][1 ], " Device_model" ) == 0 )
535+ {
536+ for (size_t k = 0 ; k < sizeof (VeDirectDeviceList) / sizeof (**VeDirectDeviceList[0 ]); k++)
537+ {
538+ if (strcmp (VeDirectDeviceList[k][0 ], myve.veValue [i]) == 0 )
539+ {
540+ Json[FPSTR (VePrettyData[j][1 ])] = FPSTR (VeDirectDeviceList[k][1 ]);
541+ break ;
542+ }
543+ }
544+ }
545+
545546 // if the Name AR - Alarm_code, search in the list for the device code
546547 if (strcmp (VePrettyData[j][1 ], " Alarm_code" ) == 0 )
547548 {
@@ -620,7 +621,7 @@ bool connectMQTT()
620621 {
621622 mqttclient.publish ((topic + String (" /IP" )).c_str (), String (WiFi.localIP ().toString ()).c_str ());
622623 mqttclient.publish ((topic + String (" /Alive" )).c_str (), " true" , true ); // LWT online message must be retained!
623- // mqttclient.publish((topic + String("/Wifi_RSSI")).c_str(), String(WiFi.RSSI()).c_str());
624+ // mqttclient.publish((topic + String("/Wifi_RSSI")).c_str(), String(WiFi.RSSI()).c_str());
624625
625626 if (strlen (_settings.data .mqttTriggerPath ) > 0 )
626627 {
@@ -713,50 +714,51 @@ bool sendHaDiscovery()
713714 " }" ;
714715
715716 char topBuff[128 ];
716- // char configBuff[1024];
717- // size_t mqttContentLength;
717+ // char configBuff[1024];
718+ // size_t mqttContentLength;
718719 for (size_t i = 0 ; i < sizeof haDescriptor / sizeof haDescriptor[0 ]; i++)
719720 {
720721 if (Json.containsKey (haDescriptor[i][0 ]))
721722 {
722- String haPayLoad = String (" {" ) +
723- " \" name\" :\" " + haDescriptor[i][0 ] + " \" ," +
724- " \" stat_t\" :\" " + _settings.data .mqttTopic + " /" + haDescriptor[i][0 ] + " \" ," +
725- " \" uniq_id\" :\" " + mqttClientId + " ." + haDescriptor[i][0 ] + " \" ," +
726- " \" ic\" :\" mdi:" + haDescriptor[i][1 ] + " \" ," ;
727- if (strlen (haDescriptor[i][2 ]) != 0 )
728- haPayLoad += (String) " \" unit_of_meas\" :\" " + haDescriptor[i][2 ] + " \" ," ;
729- if (strlen (haDescriptor[i][3 ]) != 0 )
730- haPayLoad += (String) " \" dev_cla\" :\" " + haDescriptor[i][3 ] + " \" ," ;
731- haPayLoad += haDeviceDescription;
732- haPayLoad += " }" ;
733- sprintf (topBuff, " homeassistant/sensor/%s/%s/config" , _settings.data .deviceName , haDescriptor[i][0 ]); // build the topic
734- mqttclient.beginPublish (topBuff, haPayLoad.length (), true );
735- for (size_t i = 0 ; i < haPayLoad.length (); i++)
736- {
737- mqttclient.write (haPayLoad[i]);
738- }
739- mqttclient.endPublish ();
740-
741-
742-
743-
744-
745-
746- /*
747-
723+ String haPayLoad = String (" {" ) +
724+ " \" name\" :\" " + haDescriptor[i][0 ] + " \" ," +
725+ " \" stat_t\" :\" " + _settings.data .mqttTopic + " /" + haDescriptor[i][0 ] + " \" ," +
726+ " \" uniq_id\" :\" " + mqttClientId + " ." + haDescriptor[i][0 ] + " \" ," +
727+ " \" ic\" :\" mdi:" + haDescriptor[i][1 ] + " \" ," ;
728+ if (strlen (haDescriptor[i][2 ]) != 0 )
729+ haPayLoad += (String) " \" unit_of_meas\" :\" " + haDescriptor[i][2 ] + " \" ," ;
730+
731+ if (strcmp (haDescriptor[i][2 ], " kWh" ) == 0 || strcmp (haDescriptor[i][2 ], " Wh" ) == 0 )
732+ haPayLoad += (String) " \" state_class\" :\" total_increasing\" ," ;
733+ if (strcmp (haDescriptor[i][2 ], " A" ) == 0 || strcmp (haDescriptor[i][2 ], " V" ) == 0 || strcmp (haDescriptor[i][2 ], " W" ) == 0 )
734+ haPayLoad += (String) " \" state_class\" :\" measurement\" ," ;
735+
736+ if (strlen (haDescriptor[i][3 ]) != 0 )
737+ haPayLoad += (String) " \" dev_cla\" :\" " + haDescriptor[i][3 ] + " \" ," ;
738+ haPayLoad += haDeviceDescription;
739+ haPayLoad += " }" ;
748740 sprintf (topBuff, " homeassistant/sensor/%s/%s/config" , _settings.data .deviceName , haDescriptor[i][0 ]); // build the topic
749-
750- mqttContentLength = sprintf(configBuff, "{\"state_topic\": \"%s/%s\",\"unique_id\": \"sensor.%s_%s\",\"name\": \"%s\",\"icon\": \"mdi:%s\",\"unit_of_measurement\": \"%s\",\"device_class\":\"%s\",\"device\":{\"identifiers\":[\"%s\"], \"configuration_url\":\"http://%s\",\"name\":\"%s\", \"model\":\"%s\",\"manufacturer\":\"SoftWareCrash\",\"sw_version\":\"Victron2MQTT %s\"}}",
751- _settings.data.mqttTopic, haDescriptor[i][0], _settings.data.deviceName, haDescriptor[i][0], haDescriptor[i][0], haDescriptor[i][1], haDescriptor[i][2], haDescriptor[i][3], Json["Serial_number"].as<String>().c_str(), jsonESP["IP"].as<String>().c_str(), _settings.data.deviceName, Json["Model_description"].as<String>().c_str(), SOFTWARE_VERSION);
752-
753- mqttclient.beginPublish(topBuff, mqttContentLength, true);
754- for (size_t i = 0; i < mqttContentLength; i++)
741+ mqttclient.beginPublish (topBuff, haPayLoad.length (), true );
742+ for (size_t i = 0 ; i < haPayLoad.length (); i++)
755743 {
756- mqttclient.write(configBuff [i]);
744+ mqttclient.write (haPayLoad [i]);
757745 }
758746 mqttclient.endPublish ();
759- */
747+
748+ /*
749+
750+ sprintf(topBuff, "homeassistant/sensor/%s/%s/config", _settings.data.deviceName, haDescriptor[i][0]); // build the topic
751+
752+ mqttContentLength = sprintf(configBuff, "{\"state_topic\": \"%s/%s\",\"unique_id\": \"sensor.%s_%s\",\"name\": \"%s\",\"icon\": \"mdi:%s\",\"unit_of_measurement\": \"%s\",\"device_class\":\"%s\",\"device\":{\"identifiers\":[\"%s\"], \"configuration_url\":\"http://%s\",\"name\":\"%s\", \"model\":\"%s\",\"manufacturer\":\"SoftWareCrash\",\"sw_version\":\"Victron2MQTT %s\"}}",
753+ _settings.data.mqttTopic, haDescriptor[i][0], _settings.data.deviceName, haDescriptor[i][0], haDescriptor[i][0], haDescriptor[i][1], haDescriptor[i][2], haDescriptor[i][3], Json["Serial_number"].as<String>().c_str(), jsonESP["IP"].as<String>().c_str(), _settings.data.deviceName, Json["Model_description"].as<String>().c_str(), SOFTWARE_VERSION);
754+
755+ mqttclient.beginPublish(topBuff, mqttContentLength, true);
756+ for (size_t i = 0; i < mqttContentLength; i++)
757+ {
758+ mqttclient.write(configBuff[i]);
759+ }
760+ mqttclient.endPublish();
761+ */
760762 }
761763 }
762764 return true ;
0 commit comments