From 221afa98b05b85924a271c8c1dcb74ac7b19567f Mon Sep 17 00:00:00 2001 From: Florian <1technophile@users.noreply.github.com> Date: Thu, 18 Mar 2021 15:07:59 -0500 Subject: [PATCH] Change uptime calculation method (#901) and revert #897 --- main/ZmqttDiscovery.ino | 2 +- main/main.ino | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/main/ZmqttDiscovery.ino b/main/ZmqttDiscovery.ino index 2ae11d7..80478be 100644 --- a/main/ZmqttDiscovery.ino +++ b/main/ZmqttDiscovery.ino @@ -174,7 +174,7 @@ void pubMqttDiscovery() { createDiscovery("sensor", //set Type subjectSYStoMQTT, "SYS: Uptime", (char*)getUniqueId("uptime", "").c_str(), //set state_topic,name,uniqueId "", "", "{{ value_json.uptime }}", //set availability_topic,device_class,value_template, - "", "", "min", //set,payload_on,payload_off,unit_of_meas, + "", "", "s", //set,payload_on,payload_off,unit_of_meas, 0, //set off_delay "", "", true, "", //set,payload_avalaible,payload_not avalaible ,is a gateway entity, command topic "", "", "", "", false // device name, device manufacturer, device model, device mac, retain diff --git a/main/main.ino b/main/main.ino index dcb9b82..6b292a5 100644 --- a/main/main.ino +++ b/main/main.ino @@ -1318,12 +1318,26 @@ void loop() { #endif } +/** + * Calculate uptime and take into account the millis() rollover + */ +unsigned long uptime() { + static unsigned long lastUptime = 0; + static unsigned long uptimeAdd = 0; + unsigned long uptime = millis() / 1000 + uptimeAdd; + if (uptime < lastUptime) { + uptime += 4294967; + uptimeAdd += 4294967; + } + lastUptime = uptime; + return uptime; +} + #if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__) void stateMeasures() { StaticJsonBuffer jsonBuffer; JsonObject& SYSdata = jsonBuffer.createObject(); - unsigned long uptime = millis() / 60000; //minutes uptime - SYSdata["uptime"] = uptime; + SYSdata["uptime"] = uptime(); SYSdata["version"] = OMG_VERSION; Log.trace(F("retrieving value of system characteristics Uptime (min):%u" CR), uptime); # if defined(ESP8266) || defined(ESP32)