fahrenheit uom

This commit is contained in:
MichaelDvP
2022-02-16 18:59:22 +01:00
parent b2eaca27de
commit 3b41d6fff6
3 changed files with 15 additions and 20 deletions

View File

@@ -89,7 +89,7 @@ void AnalogSensor::reload() {
it++; it++;
} }
// add new sensors from list // add new sensors from list
for (auto & sensor : sensors) { for (auto & sensor : sensors) {
bool found = false; bool found = false;
for (auto & sensor_ : sensors_) { for (auto & sensor_ : sensors_) {
if (sensor_.id() == sensor.id) { if (sensor_.id() == sensor.id) {
@@ -229,7 +229,7 @@ void AnalogSensor::measure() {
} else if (!sensor.poll_) { // falling edge } else if (!sensor.poll_) { // falling edge
if (sensor.type() == AnalogType::COUNTER) { if (sensor.type() == AnalogType::COUNTER) {
sensor.set_value(old_value + sensor.factor()); sensor.set_value(old_value + sensor.factor());
} else if (sensor.type() == AnalogType::RATE) { // dafault uom: Hz (1/sec) with factor 1 } else if (sensor.type() == AnalogType::RATE) { // dafault uom: Hz (1/sec) with factor 1
sensor.set_value(sensor.factor() * 1000 / (sensor.polltime_ - sensor.last_polltime_)); sensor.set_value(sensor.factor() * 1000 / (sensor.polltime_ - sensor.last_polltime_));
} else if (sensor.type() == AnalogType::TIMER) { // default seconds with factor 1 } else if (sensor.type() == AnalogType::TIMER) { // default seconds with factor 1
sensor.set_value(sensor.factor() * (sensor.polltime_ - sensor.last_polltime_) / 1000); sensor.set_value(sensor.factor() * (sensor.polltime_ - sensor.last_polltime_) / 1000);

View File

@@ -361,10 +361,10 @@ bool DallasSensor::command_info(const char * value, const int8_t id, JsonObject
JsonObject dataSensor = output.createNestedObject(sensor.name()); JsonObject dataSensor = output.createNestedObject(sensor.name());
dataSensor["id_str"] = sensor.id_str(); dataSensor["id_str"] = sensor.id_str();
if (Helpers::hasValue(sensor.temperature_c)) { if (Helpers::hasValue(sensor.temperature_c)) {
dataSensor["temp"] = (float)(sensor.temperature_c) / 10; dataSensor["temp"] = Helpers::round2((float)(sensor.temperature_c), 10, EMSESP::system_.fahrenheit() ? 2 : 0);
} }
} else if (Helpers::hasValue(sensor.temperature_c)) { } else if (Helpers::hasValue(sensor.temperature_c)) {
output[sensor.name()] = (float)(sensor.temperature_c) / 10; output[sensor.name()] = Helpers::round2((float)(sensor.temperature_c), 10, EMSESP::system_.fahrenheit() ? 2 : 0);
} }
} }
@@ -378,11 +378,11 @@ bool DallasSensor::get_value_info(JsonObject & output, const char * cmd, const i
output["id_str"] = sensor.id_str(); output["id_str"] = sensor.id_str();
output["name"] = sensor.name(); output["name"] = sensor.name();
if (Helpers::hasValue(sensor.temperature_c)) { if (Helpers::hasValue(sensor.temperature_c)) {
output["value"] = (float)(sensor.temperature_c) / 10; output["value"] = Helpers::round2((float)(sensor.temperature_c), 10, EMSESP::system_.fahrenheit() ? 2 : 0);
} }
output["type"] = F_(number); output["type"] = F_(number);
output["min"] = -55; output["min"] = Helpers::round2(-55, 0, EMSESP::system_.fahrenheit() ? 2 : 0);
output["max"] = 125; output["max"] = Helpers::round2(125, 0, EMSESP::system_.fahrenheit() ? 2 : 0);
output["unit"] = EMSdevice::uom_to_string(DeviceValueUOM::DEGREES); output["unit"] = EMSdevice::uom_to_string(DeviceValueUOM::DEGREES);
output["writeable"] = false; output["writeable"] = false;
return true; return true;
@@ -444,10 +444,10 @@ void DallasSensor::publish_values(const bool force) {
JsonObject dataSensor = doc.createNestedObject(sensor.id_str()); JsonObject dataSensor = doc.createNestedObject(sensor.id_str());
dataSensor["name"] = sensor.name(); dataSensor["name"] = sensor.name();
if (has_value) { if (has_value) {
dataSensor["temp"] = (float)(sensor.temperature_c) / 10; dataSensor["temp"] = Helpers::round2((float)(sensor.temperature_c), 10, EMSESP::system_.fahrenheit() ? 2 : 0);
} }
} else if (has_value) { } else if (has_value) {
doc[sensor.name()] = (float)(sensor.temperature_c) / 10; doc[sensor.name()] = Helpers::round2((float)(sensor.temperature_c), 10, EMSESP::system_.fahrenheit() ? 2 : 0);
} }
// create the HA MQTT config // create the HA MQTT config
@@ -463,11 +463,7 @@ void DallasSensor::publish_values(const bool force) {
snprintf(stat_t, sizeof(stat_t), "%s/dallassensor_data", Mqtt::base().c_str()); snprintf(stat_t, sizeof(stat_t), "%s/dallassensor_data", Mqtt::base().c_str());
config["stat_t"] = stat_t; config["stat_t"] = stat_t;
if (EMSESP::system_.fahrenheit()) { config["unit_of_meas"] = EMSdevice::uom_to_string(DeviceValueUOM::DEGREES);
config["unit_of_meas"] = FJSON("°F");
} else {
config["unit_of_meas"] = FJSON("°C");
}
char str[50]; char str[50];
snprintf(str, sizeof(str), "{{value_json['%s'].temp}}", sensor.id_str().c_str()); snprintf(str, sizeof(str), "{{value_json['%s'].temp}}", sensor.id_str().c_str());

View File

@@ -52,6 +52,9 @@ const std::string EMSdevice::tag_to_mqtt(uint8_t tag) {
} }
const std::string EMSdevice::uom_to_string(uint8_t uom) { const std::string EMSdevice::uom_to_string(uint8_t uom) {
if (EMSESP::system_.fahrenheit() && (uom == DeviceValueUOM::DEGREES || uom == DeviceValueUOM::DEGREES_R)) {
return read_flash_string(DeviceValue::DeviceValueUOM_s[DeviceValueUOM::FAHRENHEIT]);
}
return read_flash_string(DeviceValue::DeviceValueUOM_s[uom]); return read_flash_string(DeviceValue::DeviceValueUOM_s[uom]);
} }
@@ -306,11 +309,7 @@ void EMSdevice::list_device_entries(JsonObject & output) {
// add uom // add uom
if (!uom_to_string(dv.uom).empty() && uom_to_string(dv.uom) != " ") { if (!uom_to_string(dv.uom).empty() && uom_to_string(dv.uom) != " ") {
if (EMSESP::system_.fahrenheit() && (dv.uom == DeviceValueUOM::DEGREES || dv.uom == DeviceValueUOM::DEGREES_R)) { details.add(EMSdevice::uom_to_string(dv.uom));
details.add(EMSdevice::uom_to_string(DeviceValueUOM::FAHRENHEIT));
} else {
details.add(EMSdevice::uom_to_string(dv.uom));
}
} }
} }
} }
@@ -1024,7 +1023,7 @@ bool EMSdevice::get_value_info(JsonObject & output, const char * cmd, const int8
// add uom if it's not a " " (single space) // add uom if it's not a " " (single space)
if (!uom_to_string(dv.uom).empty() && uom_to_string(dv.uom) != " ") { if (!uom_to_string(dv.uom).empty() && uom_to_string(dv.uom) != " ") {
json["uom"] = fahrenheit ? "°F" : uom_to_string(dv.uom); json["uom"] = uom_to_string(dv.uom);
} }
json["writeable"] = dv.has_cmd; json["writeable"] = dv.has_cmd;