diff --git a/docs/configuration/configuration.md b/docs/configuration/configuration.md index 138b3b50..9dcd6aab 100644 --- a/docs/configuration/configuration.md +++ b/docs/configuration/configuration.md @@ -58,4 +58,6 @@ advanced: # Optional: Add a last_seen attribute to MQTT messages, contains date/time of last Zigbee message # possible values are: disable (default), ISO_8601, epoch last_seen: 'disable' -``` \ No newline at end of file + # Optional: Add an elapsed attribute to MQTT messages, contains milliseconds since the previous msg + elapsed: false +``` diff --git a/lib/extension/deviceReceive.js b/lib/extension/deviceReceive.js index 64817aa4..651fe277 100644 --- a/lib/extension/deviceReceive.js +++ b/lib/extension/deviceReceive.js @@ -13,6 +13,7 @@ class DeviceReceive { this.state = state; this.publishDeviceState = publishDeviceState; this.coordinator = null; + this.elapsed = {}; } onZigbeeStarted() { @@ -113,15 +114,24 @@ class DeviceReceive { } // Add last seen timestamp + const now = Date.now(); switch (settings.get().advanced.last_seen) { case 'ISO_8601': - payload.last_seen = new Date().toISOString(); + payload.last_seen = new Date(now).toISOString(); break; case 'epoch': - payload.last_seen = Date.now(); + payload.last_seen = now; break; } + if (settings.get().advanced.elapsed) { + if (this.elapsed[device.ieeeAddr]) { + payload.elapsed = now - this.elapsed[device.ieeeAddr]; + } + + this.elapsed[device.ieeeAddr] = now; + } + this.publishDeviceState(device, payload, cache); }; diff --git a/lib/util/settings.js b/lib/util/settings.js index 052dbb9c..90f1c93a 100644 --- a/lib/util/settings.js +++ b/lib/util/settings.js @@ -39,6 +39,9 @@ const defaults = { */ last_seen: 'disable', + // Optional: Add an elapsed attribute to MQTT messages, contains milliseconds since the previous msg + elapsed: false, + /** * https://github.com/Koenkk/zigbee2mqtt/issues/685#issuecomment-449112250 *