diff --git a/src/driver/drv_bl_shared.c b/src/driver/drv_bl_shared.c index 90178a1a1..a8aa3054a 100644 --- a/src/driver/drv_bl_shared.c +++ b/src/driver/drv_bl_shared.c @@ -463,6 +463,12 @@ bool Channel_AreAllRelaysOpen() { } return true; } +float BL_ChangeEnergyUnitIfNeeded(float Wh) { + if (CFG_HasFlag(OBK_FLAG_MQTT_ENERGY_IN_KWH)) { + return Wh * 0.001f; + } + return Wh; +} void BL_ProcessUpdate(float voltage, float current, float power, float frequency) { @@ -547,7 +553,7 @@ void BL_ProcessUpdate(float voltage, float current, float power, dailyStats[0] = 0.0; actual_mday = ltm->tm_mday; - MQTT_PublishMain_StringFloat(counter_mqttNames[3], dailyStats[1], roundingPrecision[PRECISION_ENERGY]); + MQTT_PublishMain_StringFloat(counter_mqttNames[3], BL_ChangeEnergyUnitIfNeeded(dailyStats[1]), roundingPrecision[PRECISION_ENERGY]); stat_updatesSent++; #if WINDOWS #elif PLATFORM_BL602 @@ -592,15 +598,15 @@ void BL_ProcessUpdate(float voltage, float current, float power, { root = cJSON_CreateObject(); cJSON_AddNumberToObject(root, "uptime", g_secondsElapsed); - cJSON_AddNumberToObject(root, "consumption_total", energyCounter ); + cJSON_AddNumberToObject(root, "consumption_total", BL_ChangeEnergyUnitIfNeeded(energyCounter) ); cJSON_AddNumberToObject(root, "consumption_last_hour", DRV_GetReading(OBK_CONSUMPTION_LAST_HOUR)); - cJSON_AddNumberToObject(root, "consumption_stat_index", energyCounterMinutesIndex); + cJSON_AddNumberToObject(root, "consumption_stat_index", BL_ChangeEnergyUnitIfNeeded(energyCounterMinutesIndex)); cJSON_AddNumberToObject(root, "consumption_sample_count", energyCounterSampleCount); cJSON_AddNumberToObject(root, "consumption_sampling_period", energyCounterSampleInterval); if(NTP_IsTimeSynced() == true) { - cJSON_AddNumberToObject(root, "consumption_today", dailyStats[0]); - cJSON_AddNumberToObject(root, "consumption_yesterday", dailyStats[1]); + cJSON_AddNumberToObject(root, "consumption_today", BL_ChangeEnergyUnitIfNeeded(dailyStats[0])); + cJSON_AddNumberToObject(root, "consumption_yesterday", BL_ChangeEnergyUnitIfNeeded(dailyStats[1])); ltm = localtime(&ConsumptionResetTime); if (NTP_GetTimesZoneOfsSeconds()>0) { @@ -663,7 +669,8 @@ void BL_ProcessUpdate(float voltage, float current, float power, if (MQTT_IsReady() == true) { - MQTT_PublishMain_StringFloat(counter_mqttNames[1], DRV_GetReading(OBK_CONSUMPTION_LAST_HOUR), roundingPrecision[PRECISION_ENERGY]); + MQTT_PublishMain_StringFloat(counter_mqttNames[1], + BL_ChangeEnergyUnitIfNeeded(DRV_GetReading(OBK_CONSUMPTION_LAST_HOUR)), roundingPrecision[PRECISION_ENERGY]); EventHandlers_ProcessVariableChange_Integer(CMD_EVENT_CHANGE_CONSUMPTION_LAST_HOUR, lastSentEnergyCounterLastHour, DRV_GetReading(OBK_CONSUMPTION_LAST_HOUR)); lastSentEnergyCounterLastHour = DRV_GetReading(OBK_CONSUMPTION_LAST_HOUR); stat_updatesSent++; @@ -723,20 +730,27 @@ void BL_ProcessUpdate(float voltage, float current, float power, { if (MQTT_IsReady() == true) { - MQTT_PublishMain_StringFloat(counter_mqttNames[0], energyCounter, roundingPrecision[PRECISION_ENERGY]); + MQTT_PublishMain_StringFloat(counter_mqttNames[0], + BL_ChangeEnergyUnitIfNeeded(energyCounter), roundingPrecision[PRECISION_ENERGY]); + EventHandlers_ProcessVariableChange_Integer(CMD_EVENT_CHANGE_CONSUMPTION_TOTAL, lastSentEnergyCounterValue, energyCounter); lastSentEnergyCounterValue = energyCounter; noChangeFrameEnergyCounter = 0; stat_updatesSent++; - MQTT_PublishMain_StringFloat(counter_mqttNames[1], DRV_GetReading(OBK_CONSUMPTION_LAST_HOUR), roundingPrecision[PRECISION_ENERGY]); + + MQTT_PublishMain_StringFloat(counter_mqttNames[1], + BL_ChangeEnergyUnitIfNeeded(DRV_GetReading(OBK_CONSUMPTION_LAST_HOUR)), roundingPrecision[PRECISION_ENERGY]); + EventHandlers_ProcessVariableChange_Integer(CMD_EVENT_CHANGE_CONSUMPTION_LAST_HOUR, lastSentEnergyCounterLastHour, DRV_GetReading(OBK_CONSUMPTION_LAST_HOUR)); lastSentEnergyCounterLastHour = DRV_GetReading(OBK_CONSUMPTION_LAST_HOUR); stat_updatesSent++; if(NTP_IsTimeSynced() == true) { - MQTT_PublishMain_StringFloat(counter_mqttNames[3], dailyStats[1], roundingPrecision[PRECISION_ENERGY]); + MQTT_PublishMain_StringFloat(counter_mqttNames[3], + BL_ChangeEnergyUnitIfNeeded(dailyStats[1]), roundingPrecision[PRECISION_ENERGY]); stat_updatesSent++; - MQTT_PublishMain_StringFloat(counter_mqttNames[4], dailyStats[0], roundingPrecision[PRECISION_ENERGY]); + MQTT_PublishMain_StringFloat(counter_mqttNames[4], + BL_ChangeEnergyUnitIfNeeded(dailyStats[0]), roundingPrecision[PRECISION_ENERGY]); stat_updatesSent++; ltm = localtime(&ConsumptionResetTime); snprintf(datetime,sizeof(datetime), "%04i-%02i-%02i %02i:%02i:%02i", diff --git a/src/httpserver/hass.c b/src/httpserver/hass.c index 0eb765b16..2bc402dcc 100644 --- a/src/httpserver/hass.c +++ b/src/httpserver/hass.c @@ -385,7 +385,12 @@ HassDeviceInfo* hass_init_power_sensor_device_info(int index) { const char* device_class_value = counter_devClasses[index - OBK_CONSUMPTION_TOTAL]; if (strlen(device_class_value) > 0) { cJSON_AddStringToObject(info->root, "dev_cla", device_class_value); //device_class=energy - cJSON_AddStringToObject(info->root, "unit_of_meas", "Wh"); //unit_of_measurement + if (CFG_HasFlag(OBK_FLAG_MQTT_ENERGY_IN_KWH)) { + cJSON_AddStringToObject(info->root, "unit_of_meas", "kWh"); //unit_of_measurement + } + else { + cJSON_AddStringToObject(info->root, "unit_of_meas", "Wh"); //unit_of_measurement + } //state_class can be measurement, total or total_increasing. Energy values should be total_increasing. cJSON_AddStringToObject(info->root, "stat_cla", "total_increasing"); diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 3e8475204..dbc1ed511 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -2435,7 +2435,7 @@ const char* g_obk_flagNames[] = { "[WiFi] Quick connect to WiFi on reboot (TODO: check if it works for you and report on github)", "[Power] Set power and current to zero if all relays are open", "[MQTT] [Debug] Publish all channels (don't enable it, it will be publish all 64 possible channels on connect)", - "error", + "[MQTT] Use kWh unit for energy consumption (total, last hour, today) instead of Wh", "error", "error", "error", diff --git a/src/new_pins.h b/src/new_pins.h index aa7470894..3e54f8d7a 100644 --- a/src/new_pins.h +++ b/src/new_pins.h @@ -925,8 +925,9 @@ typedef struct pinsState_s { #define OBK_FLAG_WIFI_FAST_CONNECT 37 #define OBK_FLAG_POWER_FORCE_ZERO_IF_RELAYS_OPEN 38 #define OBK_FLAG_MQTT_PUBLISH_ALL_CHANNELS 39 +#define OBK_FLAG_MQTT_ENERGY_IN_KWH 40 -#define OBK_TOTAL_FLAGS 40 +#define OBK_TOTAL_FLAGS 41 #define LOGGER_FLAG_MQTT_DEDUPER 1 #define LOGGER_FLAG_POWER_SAVE 2