Elapsed time (#909)

* new elapsed attribute giving the elapsed time since the previous msg

* configurable elapsed attribute

* Update deviceReceive.js

* Update deviceReceive.js
This commit is contained in:
Laurent 2019-01-22 20:02:34 +01:00 committed by Koen Kanters
parent d324a91a5f
commit 9e5fb04314
3 changed files with 18 additions and 3 deletions

View File

@ -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'
```
# Optional: Add an elapsed attribute to MQTT messages, contains milliseconds since the previous msg
elapsed: false
```

View File

@ -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);
};

View File

@ -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
*