@@ -101,9 +101,17 @@ void Shower::loop() {
101
101
if (duration_ > SHOWER_MIN_DURATION) {
102
102
StaticJsonDocument<EMSESP_JSON_SIZE_SMALL> doc;
103
103
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
106
105
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
+ }
107
115
Mqtt::queue_publish (" shower_data" , doc.as <JsonObject>());
108
116
LOG_INFO (" finished with duration %lu seconds" , duration_);
109
117
}
@@ -173,7 +181,7 @@ void Shower::set_shower_state(bool state, bool force) {
173
181
char stat_t [50 ];
174
182
175
183
//
176
- // shower_active topic
184
+ // shower active
177
185
//
178
186
doc[" name" ] = " Shower Active" ;
179
187
@@ -210,7 +218,7 @@ void Shower::set_shower_state(bool state, bool force) {
210
218
ha_configdone_ = Mqtt::queue_ha (topic, doc.as <JsonObject>()); // publish the config payload with retain flag
211
219
212
220
//
213
- // shower_duaration topic
221
+ // shower duaration
214
222
//
215
223
doc.clear ();
216
224
@@ -237,6 +245,32 @@ void Shower::set_shower_state(bool state, bool force) {
237
245
238
246
snprintf (topic, sizeof (topic), " sensor/%s/shower_duration/config" , Mqtt::basename ().c_str ());
239
247
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
240
274
}
241
275
}
242
276
0 commit comments