fixes suggested by Michael, renamed topic sensors to sensor_data #506

This commit is contained in:
proddy
2020-09-20 19:42:15 +02:00
parent 17e44e1dca
commit 32634755b2
3 changed files with 17 additions and 18 deletions

View File

@@ -24,11 +24,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- renamed wWCircPumpType to wWChargeType
- Installation and Configuration notes moved to the official EMS-ESP documentation site
- Removed the _cmd from the MQTT topic names
- MQTT sensors topic renamed to sensor_data
### Removed
-
## [2.0.1] September 13 2020
### Added

View File

@@ -442,7 +442,8 @@ bool Thermostat::export_values(uint8_t mqtt_format, JsonObject & rootThermostat)
char s[3];
strlcpy(topic, "thermostat_data", 30);
strlcat(topic, Helpers::itoa(s, hc->hc_num()), 30); // append hc to topic
rootThermostat.clear(); // clear object
Mqtt::publish(topic, rootThermostat);
rootThermostat.clear(); // clear object
} else if (mqtt_format == MQTT_format::HA) {
// see if we have already registered this with HA MQTT Discovery, if not send the config
if (!hc->ha_registered()) {

View File

@@ -279,7 +279,7 @@ bool Sensor::command_info(const char * value, const int8_t id, JsonObject & outp
// creates JSON doc from values
// returns false if empty
// e.g. sensors = {"sensor1":{"id":"28-EA41-9497-0E03-5F","temp":"23.30"},"sensor2":{"id":"28-233D-9497-0C03-8B","temp":"24.0"}}
// e.g. sensor_data = {"sensor1":{"id":"28-EA41-9497-0E03-5F","temp":"23.30"},"sensor2":{"id":"28-233D-9497-0C03-8B","temp":"24.0"}}
bool Sensor::export_values(JsonObject & output) {
if (devices_.size() == 0) {
return false;
@@ -287,9 +287,9 @@ bool Sensor::export_values(JsonObject & output) {
uint8_t i = 1; // sensor count
for (const auto & device : devices_) {
char s[7];
char sensorID[10]; // sensor{1-n}
strlcpy(sensorID, "sensor", 10);
strlcat(sensorID, Helpers::itoa(s, i), 10);
char sensorID[20]; // sensor{1-n}
strlcpy(sensorID, "sensor_data", 20);
strlcat(sensorID, Helpers::itoa(s, i), 20);
JsonObject dataSensor = output.createNestedObject(sensorID);
dataSensor["id"] = device.to_string();
dataSensor["temp"] = Helpers::render_value(s, device.temperature_c, 1);
@@ -308,16 +308,15 @@ void Sensor::publish_values() {
return;
}
// if we're not using nested JSON, send each sensor out seperately
// sensor1, sensor2 etc...
// e.g. sensor_1 = {"temp":20.2}
// if we're not using nested JSON, send each sensor out seperately as sensor1, sensor2 etc...
// e.g. sensor_data1 = {"temp":20.2}
if (mqtt_format_ == MQTT_format::SINGLE) {
StaticJsonDocument<100> doc;
for (const auto & device : devices_) {
char s[7]; // sensorrange -55.00 to 125.00
doc["temp"] = Helpers::render_value(s, device.temperature_c, 1);
char topic[60]; // sensors{1-n}
strlcpy(topic, "sensor_", 50); // create topic, e.g. home/ems-esp/sensor_28-EA41-9497-0E03-5F
char topic[60]; // sensors{1-n}
strlcpy(topic, "sensor_data", 50); // create topic, e.g. home/ems-esp/sensor_28-EA41-9497-0E03-5F
strlcat(topic, device.to_string().c_str(), 60);
Mqtt::publish(topic, doc.as<JsonObject>());
doc.clear(); // clear json doc so we can reuse the buffer again
@@ -331,13 +330,13 @@ void Sensor::publish_values() {
char s[7];
if (mqtt_format_ == MQTT_format::CUSTOM) {
// e.g. sensors = {28-EA41-9497-0E03-5F":23.30,"28-233D-9497-0C03-8B":24.0}
// e.g. sensor_data = {28-EA41-9497-0E03-5F":23.30,"28-233D-9497-0C03-8B":24.0}
doc[device.to_string()] = Helpers::render_value(s, device.temperature_c, 1);
} else if ((mqtt_format_ == MQTT_format::NESTED) || (mqtt_format_ == MQTT_format::HA)) {
// e.g. sensors = {"sensor1":{"id":"28-EA41-9497-0E03-5F","temp":"23.30"},"sensor2":{"id":"28-233D-9497-0C03-8B","temp":"24.0"}}
char sensorID[10]; // sensor{1-n}
strlcpy(sensorID, "sensor", 10);
strlcat(sensorID, Helpers::itoa(s, i), 10);
// e.g. sensor_data = {"sensor1":{"id":"28-EA41-9497-0E03-5F","temp":"23.30"},"sensor2":{"id":"28-233D-9497-0C03-8B","temp":"24.0"}}
char sensorID[20]; // sensor{1-n}
strlcpy(sensorID, "sensor_data", 20);
strlcat(sensorID, Helpers::itoa(s, i), 20);
JsonObject dataSensor = doc.createNestedObject(sensorID);
dataSensor["id"] = device.to_string();
dataSensor["temp"] = Helpers::render_value(s, device.temperature_c, 1);
@@ -346,7 +345,6 @@ void Sensor::publish_values() {
// special for HA
if (mqtt_format_ == MQTT_format::HA) {
std::string topic(100, '\0');
// create the config if this hasn't already been done
/* e.g.
{
@@ -382,7 +380,7 @@ void Sensor::publish_values() {
}
if ((mqtt_format_ == MQTT_format::NESTED) || (mqtt_format_ == MQTT_format::CUSTOM)) {
Mqtt::publish(F("sensors"), doc.as<JsonObject>());
Mqtt::publish(F("sensor_data"), doc.as<JsonObject>());
} else if (mqtt_format_ == MQTT_format::HA) {
Mqtt::publish(F("homeassistant/sensor/ems-esp/state"), doc.as<JsonObject>());
}