From e6a8b2d30d1efa1c89d3df900cf9b8079a23eb6b Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Tue, 30 Oct 2018 17:04:19 +0100 Subject: [PATCH] Add TotalStartTime to Energy JSON Add TotalStartTime to Energy JSON message (#3971) --- sonoff/_changelog.ino | 1 + sonoff/i18n.h | 1 + sonoff/settings.h | 3 ++- sonoff/sonoff.h | 2 +- sonoff/support.ino | 8 +++++++- sonoff/xdrv_03_energy.ino | 5 +++-- 6 files changed, 15 insertions(+), 5 deletions(-) diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index 7bf6d8aba..c4ac20a7f 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -3,6 +3,7 @@ * Add command SetOption53 0/1 to toggle gui display of Hostname and IP address (#1006, #2091) * Add token %hostname% to command FullTopic (#3018) * Add support for two BMP/BME sensors (#4195) + * Add TotalStartTime to Energy JSON message (#3971) * * 6.2.1.19 20181023 * Fix header file execution order by renaming user_config.h to my_user_config.h diff --git a/sonoff/i18n.h b/sonoff/i18n.h index 62226f91f..3067271e9 100644 --- a/sonoff/i18n.h +++ b/sonoff/i18n.h @@ -127,6 +127,7 @@ #define D_JSON_TIME "Time" #define D_JSON_TODAY "Today" #define D_JSON_TOTAL "Total" +#define D_JSON_TOTAL_START_TIME "TotalStartTime" #define D_JSON_TVOC "TVOC" #define D_JSON_TYPE "Type" #define D_JSON_UPTIME "Uptime" diff --git a/sonoff/settings.h b/sonoff/settings.h index 8e0a22a2f..4d1d61cd4 100644 --- a/sonoff/settings.h +++ b/sonoff/settings.h @@ -322,8 +322,9 @@ struct SYSCFG { uint16_t mcp230xx_int_timer; // 718 uint8_t rgbwwTable[5]; // 71A - byte free_71F[153]; // 71F + byte free_71F[149]; // 71F + uint32_t energy_kWhtotal_time; // 7B4 unsigned long weight_item; // 7B8 Weight of one item in gram * 10 byte free_7BC[2]; // 7BC diff --git a/sonoff/sonoff.h b/sonoff/sonoff.h index ebc2ddb8a..9bcbbb731 100644 --- a/sonoff/sonoff.h +++ b/sonoff/sonoff.h @@ -181,7 +181,7 @@ enum WeekInMonthOptions {Last, First, Second, Third, Fourth}; enum DayOfTheWeekOptions {Sun=1, Mon, Tue, Wed, Thu, Fri, Sat}; enum MonthNamesOptions {Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec}; enum HemisphereOptions {North, South}; -enum GetDateAndTimeOptions { DT_LOCAL, DT_UTC, DT_RESTART }; +enum GetDateAndTimeOptions { DT_LOCAL, DT_UTC, DT_RESTART, DT_ENERGY }; enum LoggingLevels {LOG_LEVEL_NONE, LOG_LEVEL_ERROR, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, LOG_LEVEL_DEBUG_MORE, LOG_LEVEL_ALL}; diff --git a/sonoff/support.ino b/sonoff/support.ino index 67d55a8ab..9e1d82448 100644 --- a/sonoff/support.ino +++ b/sonoff/support.ino @@ -1946,6 +1946,10 @@ String GetDateAndTime(byte time_type) TIME_T tmpTime; switch (time_type) { + case DT_ENERGY: + BreakTime(Settings.energy_kWhtotal_time, tmpTime); + tmpTime.year += 1970; + break; case DT_UTC: BreakTime(utc_time, tmpTime); tmpTime.year += 1970; @@ -1964,7 +1968,8 @@ String GetDateAndTime(byte time_type) snprintf_P(dt, sizeof(dt), PSTR("%04d-%02d-%02dT%02d:%02d:%02d"), tmpTime.year, tmpTime.month, tmpTime.day_of_month, tmpTime.hour, tmpTime.minute, tmpTime.second); - if (Settings.flag3.time_append_timezone && (time_type == DT_LOCAL)) { + if (Settings.flag3.time_append_timezone && (DT_LOCAL == time_type)) { +// if (Settings.flag3.time_append_timezone && ((DT_LOCAL == time_type) || (DT_ENERGY == time_type))) { snprintf_P(dt, sizeof(dt), PSTR("%s%+03d:%02d"), dt, time_timezone / 10, abs((time_timezone % 10) * 6)); // if timezone = +2:30 then time_timezone = 25 } @@ -2225,6 +2230,7 @@ void RtcSecond() } local_time += time_offset; time_timezone = time_offset / 360; // (SECS_PER_HOUR / 10) fails as it is defined as UL + if (!Settings.energy_kWhtotal_time) { Settings.energy_kWhtotal_time = local_time; } } BreakTime(local_time, RtcTime); if (!RtcTime.hour && !RtcTime.minute && !RtcTime.second && RtcTime.valid) { diff --git a/sonoff/xdrv_03_energy.ino b/sonoff/xdrv_03_energy.ino index 341fc71b5..717ff234c 100644 --- a/sonoff/xdrv_03_energy.ino +++ b/sonoff/xdrv_03_energy.ino @@ -393,6 +393,7 @@ boolean EnergyCommand() RtcSettings.energy_kWhtotal = lnum *100; Settings.energy_kWhtotal = RtcSettings.energy_kWhtotal; energy_total = (float)(RtcSettings.energy_kWhtotal + energy_kWhtoday) / 100000; + if (!energy_total) { Settings.energy_kWhtotal_time = LocalTime(); } break; } } @@ -612,8 +613,8 @@ void EnergyShow(boolean json) } if (json) { - snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"" D_RSLT_ENERGY "\":{\"" D_JSON_TOTAL "\":%s,\"" D_JSON_YESTERDAY "\":%s,\"" D_JSON_TODAY "\":%s%s,\"" D_JSON_POWERUSAGE "\":%s"), - mqtt_data, energy_total_chr, energy_yesterday_chr, energy_daily_chr, (show_energy_period) ? speriod : "", active_power_chr); + snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"" D_RSLT_ENERGY "\":{\"" D_JSON_TOTAL_START_TIME "\":\"%s\",\"" D_JSON_TOTAL "\":%s,\"" D_JSON_YESTERDAY "\":%s,\"" D_JSON_TODAY "\":%s%s,\"" D_JSON_POWERUSAGE "\":%s"), + mqtt_data, GetDateAndTime(DT_ENERGY).c_str(), energy_total_chr, energy_yesterday_chr, energy_daily_chr, (show_energy_period) ? speriod : "", active_power_chr); if (!energy_type_dc) { snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"" D_JSON_APPARENT_POWERUSAGE "\":%s,\"" D_JSON_REACTIVE_POWERUSAGE "\":%s,\"" D_JSON_POWERFACTOR "\":%s%s"), mqtt_data, apparent_power_chr, reactive_power_chr, power_factor_chr, (!isnan(energy_frequency)) ? sfrequency : "");