[ONOFF] ON OFF behavior fixes (#1828)

-Avoid plug restart in case of OverCurrent detection, introduced with central queue add
-publish uselaststate value
-if uselaststate is false we switch off the relay at restart
This commit is contained in:
Florian authored and GitHub committed 2023-12-07 15:48:06 -06:00
1 parent 87cb2fecd2
commit 8c8dc6b6f6
3 files changed
+17 -11

No files matched your search

+13 -8
View File
@@ -121,8 +121,11 @@ void setupONOFF() {
# ifdef ACTUATOR_ONOFF_DEFAULT # ifdef ACTUATOR_ONOFF_DEFAULT
digitalWrite(ACTUATOR_ONOFF_GPIO, ACTUATOR_ONOFF_DEFAULT); digitalWrite(ACTUATOR_ONOFF_GPIO, ACTUATOR_ONOFF_DEFAULT);
# elif defined(ESP32) # elif defined(ESP32)
if (ONOFFConfig.useLastStateOnStart) if (ONOFFConfig.useLastStateOnStart) {
digitalWrite(ACTUATOR_ONOFF_GPIO, ONOFFConfig.ONOFFState); digitalWrite(ACTUATOR_ONOFF_GPIO, ONOFFConfig.ONOFFState);
} else {
digitalWrite(ACTUATOR_ONOFF_GPIO, !ACTUATOR_ON);
}
# endif # endif
updatePowerIndicator(); updatePowerIndicator();
Log.trace(F("ZactuatorONOFF setup done" CR)); Log.trace(F("ZactuatorONOFF setup done" CR));
@@ -194,6 +197,7 @@ void MQTTtoONOFF(char* topicOri, JsonObject& ONOFFdata) {
// Load config from json if available // Load config from json if available
ONOFFConfig_fromJson(ONOFFdata); ONOFFConfig_fromJson(ONOFFdata);
stateONOFFMeasures();
} }
} }
# endif # endif
@@ -285,14 +289,12 @@ void ActuatorTrigger() {
} else { } else {
PowerIndicatorOFF(); PowerIndicatorOFF();
} }
// Send the state of the switch to the broker so as to update the status
StaticJsonDocument<64> jsonBuffer;
JsonObject ONOFFdata = jsonBuffer.to<JsonObject>();
ONOFFdata["cmd"] = (int)level;
ONOFFdata["origin"] = subjectGTWONOFFtoMQTT;
handleJsonEnqueue(ONOFFdata, QueueSemaphoreTimeOutTask);
# ifdef ESP32 # ifdef ESP32
if (ONOFFConfig.useLastStateOnStart) { if (ONOFFConfig.useLastStateOnStart) {
StaticJsonDocument<64> jsonBuffer;
JsonObject ONOFFdata = jsonBuffer.to<JsonObject>();
ONOFFdata["cmd"] = (int)level;
ONOFFdata["save"] = true; ONOFFdata["save"] = true;
ONOFFConfig_fromJson(ONOFFdata); ONOFFConfig_fromJson(ONOFFdata);
} }
@@ -302,9 +304,12 @@ void ActuatorTrigger() {
void stateONOFFMeasures() { void stateONOFFMeasures() {
//Publish actuator state //Publish actuator state
StaticJsonDocument<64> jsonBuffer; StaticJsonDocument<128> jsonBuffer;
JsonObject ONOFFdata = jsonBuffer.to<JsonObject>(); JsonObject ONOFFdata = jsonBuffer.to<JsonObject>();
ONOFFdata["cmd"] = (int)digitalRead(ACTUATOR_ONOFF_GPIO); ONOFFdata["cmd"] = (int)digitalRead(ACTUATOR_ONOFF_GPIO);
# ifdef ESP32
ONOFFdata["uselaststate"] = ONOFFConfig.useLastStateOnStart;
# endif
ONOFFdata["origin"] = subjectGTWONOFFtoMQTT; ONOFFdata["origin"] = subjectGTWONOFFtoMQTT;
handleJsonEnqueue(ONOFFdata, QueueSemaphoreTimeOutTask); handleJsonEnqueue(ONOFFdata, QueueSemaphoreTimeOutTask);
} }
+3 -2
View File
@@ -949,7 +949,7 @@ void pubMqttDiscovery() {
# ifdef ZactuatorONOFF # ifdef ZactuatorONOFF
Log.trace(F("actuatorONOFFDiscovery" CR)); Log.trace(F("actuatorONOFFDiscovery" CR));
char* actuatorONOFF[8] = {"switch", "actuatorONOFF", "", "", "", "{\"cmd\":1}", "{\"cmd\":0}", ""}; char* actuatorONOFF[8] = {"switch", "actuatorONOFF", "", "", "{{ value_json.cmd }}", "{\"cmd\":1}", "{\"cmd\":0}", ""};
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement //component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
//trc(actuatorONOFF[1]); //trc(actuatorONOFF[1]);
@@ -959,7 +959,8 @@ void pubMqttDiscovery() {
actuatorONOFF[5], actuatorONOFF[6], actuatorONOFF[7], actuatorONOFF[5], actuatorONOFF[6], actuatorONOFF[7],
0, Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoONOFF, 0, Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoONOFF,
"", "", "", "", false, // device name, device manufacturer, device model, device ID, retain "", "", "", "", false, // device name, device manufacturer, device model, device ID, retain
stateClassNone //State Class stateClassNone, //State Class
"0", "1" //state_off, state_on
); );
# endif # endif
+1 -1
View File
@@ -106,7 +106,7 @@ void setupRN8209() {
cal.EC = RN8209_EC; cal.EC = RN8209_EC;
set_user_param(cal); set_user_param(cal);
init_8209c_interface(); init_8209c_interface();
xTaskCreate(rn8209_loop, "rn8209_loop", 5000, NULL, 10, &rn8209TaskHandle); xTaskCreate(rn8209_loop, "rn8209_loop", 5500, NULL, 10, &rn8209TaskHandle);
//esp_task_wdt_add(rn8209TaskHandle); //esp_task_wdt_add(rn8209TaskHandle);
Log.trace(F("ZsensorRN8209 setup done " CR)); Log.trace(F("ZsensorRN8209 setup done " CR));
} }