Skip to content

Commit 2866862

Browse files
committed
add timestamp - #1329
1 parent c234e70 commit 2866862

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/shower.cpp

+38-4
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,17 @@ void Shower::loop() {
101101
if (duration_ > SHOWER_MIN_DURATION) {
102102
StaticJsonDocument<EMSESP_JSON_SIZE_SMALL> doc;
103103

104-
// char s[50];
105-
// snprintf(s, 50, "%02u:%02u:%02u", (uint8_t)(duration_ / 3600000UL), (uint8_t)(duration_ / 60000UL), (uint8_t)((duration_ / 1000UL) % 60));
104+
// duration in seconds
106105
doc["duration"] = (duration_ / 1000UL); // seconds
106+
time_t now = time(nullptr);
107+
// if NTP enabled, publish timestamp
108+
if (now > 1576800000) { // year 2020
109+
// doc["timestamp_s"] = now; // if needed, in seconds
110+
tm * tm_ = localtime(&now);
111+
char dt[25];
112+
strftime(dt, sizeof(dt), "%FT%T%z", tm_);
113+
doc["timestamp"] = dt;
114+
}
107115
Mqtt::queue_publish("shower_data", doc.as<JsonObject>());
108116
LOG_INFO("finished with duration %lu seconds", duration_);
109117
}
@@ -173,7 +181,7 @@ void Shower::set_shower_state(bool state, bool force) {
173181
char stat_t[50];
174182

175183
//
176-
// shower_active topic
184+
// shower active
177185
//
178186
doc["name"] = "Shower Active";
179187

@@ -210,7 +218,7 @@ void Shower::set_shower_state(bool state, bool force) {
210218
ha_configdone_ = Mqtt::queue_ha(topic, doc.as<JsonObject>()); // publish the config payload with retain flag
211219

212220
//
213-
// shower_duaration topic
221+
// shower duaration
214222
//
215223
doc.clear();
216224

@@ -237,6 +245,32 @@ void Shower::set_shower_state(bool state, bool force) {
237245

238246
snprintf(topic, sizeof(topic), "sensor/%s/shower_duration/config", Mqtt::basename().c_str());
239247
Mqtt::queue_ha(topic, doc.as<JsonObject>()); // publish the config payload with retain flag
248+
249+
//
250+
// shower timestamp
251+
//
252+
doc.clear();
253+
254+
snprintf(str, sizeof(str), "%s_shower_timestamp", Mqtt::basename().c_str());
255+
256+
doc["uniq_id"] = str;
257+
doc["object_id"] = str;
258+
259+
snprintf(stat_t, sizeof(stat_t), "%s/shower_data", Mqtt::basename().c_str());
260+
doc["stat_t"] = stat_t;
261+
262+
doc["name"] = "Shower Timestamp";
263+
doc["val_tpl"] = "{{value_json.timestamp if value_json.timestamp is defined else 0}}";
264+
doc["ent_cat"] = "diagnostic";
265+
266+
JsonObject dev3 = doc.createNestedObject("dev");
267+
JsonArray ids3 = dev3.createNestedArray("ids");
268+
ids3.add(Mqtt::basename());
269+
270+
Mqtt::add_avty_to_doc(stat_t, doc.as<JsonObject>(), "value_json.timestamp is defined"); // add "availability" section
271+
272+
snprintf(topic, sizeof(topic), "sensor/%s/shower_timestamp/config", Mqtt::basename().c_str());
273+
Mqtt::queue_ha(topic, doc.as<JsonObject>()); // publish the config payload with retain flag
240274
}
241275
}
242276

src/shower.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ class Shower {
3838

3939
static constexpr uint32_t SHOWER_PAUSE_TIME = 15000; // in ms. 15 seconds, max time if water is switched off & on during a shower
4040
static constexpr uint32_t SHOWER_MIN_DURATION = 120000; // in ms. 2 minutes, before recognizing its a shower
41-
static constexpr uint32_t SHOWER_OFFSET_TIME = 5000; // in ms. 5 seconds grace time, to calibrate actual time under the shower
41+
// static constexpr uint32_t SHOWER_MIN_DURATION = 5000; // for testing in ms. 5 seconds
42+
43+
static constexpr uint32_t SHOWER_OFFSET_TIME = 5000; // in ms. 5 seconds grace time, to calibrate actual time under the shower
4244

4345
void shower_alert_start();
4446
void shower_alert_stop();

0 commit comments

Comments
 (0)