Skip to content

Commit

Permalink
[DISC] battery_ok discovery differentiation (#1958)
Browse files Browse the repository at this point in the history
Differentiation between 0%-100% float battery_ok devices' discovery and binary_sensor devices battery_ok
  • Loading branch information
DigiH authored Jun 10, 2024
1 parent 417d28f commit 6e90670
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
41 changes: 33 additions & 8 deletions main/ZgatewayRTL_433.ino
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,11 @@ void launchRTL_433Discovery(bool overrideDiscovery) {
String idWoKey = pdevice->uniqueId;
idWoKey.remove(idWoKey.length() - (strlen(parameters[i][0]) + 1));
DISCOVERY_TRACE_LOG(F("idWoKey %s" CR), idWoKey.c_str());
String value_template = "{{ value_json." + String(parameters[i][0]) + " | is_defined }}";
if (strcmp(parameters[i][0], "battery_ok") == 0) {
if (SYSConfig.ohdiscovery) {
value_template = "{{ value_json." + String(parameters[i][0]) + " * 99 + 1 }}"; // https://github.com/merbanan/rtl_433/blob/93f0f30c28cfb6b82b8cc3753415b01a85bee91d/examples/rtl_433_mqtt_hass.py#L187
} else {
value_template = "{{ float(value_json." + String(parameters[i][0]) + ") * 99 + 1 | is_defined }}"; // https://github.com/merbanan/rtl_433/blob/93f0f30c28cfb6b82b8cc3753415b01a85bee91d/examples/rtl_433_mqtt_hass.py#L187
}
String value_template = "";
if (SYSConfig.ohdiscovery) {
value_template = "{{ value_json." + String(parameters[i][0]) + " }}";
} else {
value_template = "{{ value_json." + String(parameters[i][0]) + " | is_defined }}";
}
String topic = subjectRTL_433toMQTT;
# if valueAsATopic
Expand All @@ -142,7 +140,34 @@ void launchRTL_433Discovery(bool overrideDiscovery) {
DISCOVERY_TRACE_LOG(F("idWoKeyAndModel %s" CR), idWoKeyAndModel.c_str());
topic = topic + "/" + String(pdevice->modelName) + idWoKeyAndModel;
# endif
if (strcmp(parameters[i][0], "tamper") == 0 || strcmp(parameters[i][0], "alarm") == 0 || strcmp(parameters[i][0], "motion") == 0) {
if (strcmp(parameters[i][0], "battery_ok") == 0) {
if (strcmp(pdevice->modelName, "Govee-Water") == 0 || strcmp(pdevice->modelName, "Govee-Contact") == 0 || strcmp(pdevice->modelName, "Archos-TBH") == 0 || strcmp(pdevice->modelName, "FineOffset-WH31L") == 0 || strcmp(pdevice->modelName, "Fineoffset-WH45") == 0 || strcmp(pdevice->modelName, "Fineoffset-WN34") == 0 || strcmp(pdevice->modelName, "Fineoffset-WS80") == 0 || strcmp(pdevice->modelName, "Fineoffset-WH0290") == 0 || strcmp(pdevice->modelName, "Fineoffset-WH51") == 0 || strcmp(pdevice->modelName, "Kedsum-TH") == 0 || strcmp(pdevice->modelName, "AVE") == 0 || strcmp(pdevice->modelName, "TPMS") == 0) {
if (SYSConfig.ohdiscovery) {
value_template = "{{ (value_json." + String(parameters[i][0]) + " * 100) | round(0)}}";
} else {
value_template = "{{ (float(value_json." + String(parameters[i][0]) + ") * 100) | round(0) | is_defined }}";
}
createDiscovery("sensor", //set Type
(char*)topic.c_str(), parameters[i][1], pdevice->uniqueId, //set state_topic,name,uniqueId
"", parameters[i][3], (char*)value_template.c_str(), //set availability_topic,device_class,value_template,
"", "", "%", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay
"", "", false, "", //set,payload_available,payload_not available ,is a gateway entity, command topic
(char*)idWoKey.c_str(), "", pdevice->modelName, (char*)idWoKey.c_str(), false, // device name, device manufacturer, device model, device ID, retain
stateClassMeasurement //State Class
);
} else {
createDiscovery("binary_sensor", //set Type
(char*)topic.c_str(), parameters[i][1], pdevice->uniqueId, //set state_topic,name,uniqueId
"", parameters[i][3], (char*)value_template.c_str(), //set availability_topic,device_class,value_template,
"0", "1", "", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay
"", "", false, "", //set,payload_available,payload_not available ,is a gateway entity, command topic
(char*)idWoKey.c_str(), "", pdevice->modelName, (char*)idWoKey.c_str(), false, // device name, device manufacturer, device model, device ID, retain
"" //State Class
);
}
} else if (strcmp(parameters[i][0], "tamper") == 0 || strcmp(parameters[i][0], "alarm") == 0 || strcmp(parameters[i][0], "motion") == 0) {
createDiscovery("binary_sensor", //set Type
(char*)topic.c_str(), parameters[i][1], pdevice->uniqueId, //set state_topic,name,uniqueId
"", parameters[i][3], (char*)value_template.c_str(), //set availability_topic,device_class,value_template,
Expand Down
4 changes: 2 additions & 2 deletions main/config_RF.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ struct RTL_433device {
const char parameters[53][4][24] = {
// RTL_433 key, name, unit, device_class
{"alarm", "Alarm", "", ""},
{"battery_mV", "Battery", "mV", "voltage"},
{"battery_ok", "Battery", "%", "battery"},
{"battery_mV", "Battery Voltage", "mV", "voltage"},
{"battery_ok", "Battery", "", "battery"},
{"co2_ppm", "Carbon Dioxide", "ppm", "carbon_dioxide"},
{"depth_cm", "Depth", "cm", "distance"},
{"estimated_pm10_0_ug_m3", "Estimated PM10", "μg/m³", "pm10"},
Expand Down
1 change: 1 addition & 0 deletions main/config_mqttDiscovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ const char* availableHASSUnits[] = {"W",
"m³",
"mg/m³",
"m/s²",
"mV",
"lx",
"Ω",
"%",
Expand Down

0 comments on commit 6e90670

Please sign in to comment.