diff --git a/src/shower.cpp b/src/shower.cpp index f5fd144bb..865877570 100644 --- a/src/shower.cpp +++ b/src/shower.cpp @@ -101,9 +101,17 @@ void Shower::loop() { if (duration_ > SHOWER_MIN_DURATION) { StaticJsonDocument doc; - // char s[50]; - // snprintf(s, 50, "%02u:%02u:%02u", (uint8_t)(duration_ / 3600000UL), (uint8_t)(duration_ / 60000UL), (uint8_t)((duration_ / 1000UL) % 60)); + // duration in seconds doc["duration"] = (duration_ / 1000UL); // seconds + time_t now = time(nullptr); + // if NTP enabled, publish timestamp + if (now > 1576800000) { // year 2020 + // doc["timestamp_s"] = now; // if needed, in seconds + tm * tm_ = localtime(&now); + char dt[25]; + strftime(dt, sizeof(dt), "%FT%T%z", tm_); + doc["timestamp"] = dt; + } Mqtt::queue_publish("shower_data", doc.as()); LOG_INFO("finished with duration %lu seconds", duration_); } @@ -173,7 +181,7 @@ void Shower::set_shower_state(bool state, bool force) { char stat_t[50]; // - // shower_active topic + // shower active // doc["name"] = "Shower Active"; @@ -210,7 +218,7 @@ void Shower::set_shower_state(bool state, bool force) { ha_configdone_ = Mqtt::queue_ha(topic, doc.as()); // publish the config payload with retain flag // - // shower_duaration topic + // shower duaration // doc.clear(); @@ -237,6 +245,32 @@ void Shower::set_shower_state(bool state, bool force) { snprintf(topic, sizeof(topic), "sensor/%s/shower_duration/config", Mqtt::basename().c_str()); Mqtt::queue_ha(topic, doc.as()); // publish the config payload with retain flag + + // + // shower timestamp + // + doc.clear(); + + snprintf(str, sizeof(str), "%s_shower_timestamp", Mqtt::basename().c_str()); + + doc["uniq_id"] = str; + doc["object_id"] = str; + + snprintf(stat_t, sizeof(stat_t), "%s/shower_data", Mqtt::basename().c_str()); + doc["stat_t"] = stat_t; + + doc["name"] = "Shower Timestamp"; + doc["val_tpl"] = "{{value_json.timestamp if value_json.timestamp is defined else 0}}"; + doc["ent_cat"] = "diagnostic"; + + JsonObject dev3 = doc.createNestedObject("dev"); + JsonArray ids3 = dev3.createNestedArray("ids"); + ids3.add(Mqtt::basename()); + + Mqtt::add_avty_to_doc(stat_t, doc.as(), "value_json.timestamp is defined"); // add "availability" section + + snprintf(topic, sizeof(topic), "sensor/%s/shower_timestamp/config", Mqtt::basename().c_str()); + Mqtt::queue_ha(topic, doc.as()); // publish the config payload with retain flag } } diff --git a/src/shower.h b/src/shower.h index bb2013b6a..b2504a6d6 100644 --- a/src/shower.h +++ b/src/shower.h @@ -38,7 +38,9 @@ class Shower { static constexpr uint32_t SHOWER_PAUSE_TIME = 15000; // in ms. 15 seconds, max time if water is switched off & on during a shower static constexpr uint32_t SHOWER_MIN_DURATION = 120000; // in ms. 2 minutes, before recognizing its a shower - static constexpr uint32_t SHOWER_OFFSET_TIME = 5000; // in ms. 5 seconds grace time, to calibrate actual time under the shower + // static constexpr uint32_t SHOWER_MIN_DURATION = 5000; // for testing in ms. 5 seconds + + static constexpr uint32_t SHOWER_OFFSET_TIME = 5000; // in ms. 5 seconds grace time, to calibrate actual time under the shower void shower_alert_start(); void shower_alert_stop();