diff --git a/lib/entities/utils.js b/lib/entities/utils.js index 610c340e1..f113a80e3 100644 --- a/lib/entities/utils.js +++ b/lib/entities/utils.js @@ -433,6 +433,39 @@ function createEntityNameFromCustom(obj, namespace) { } } +function updateTimestamps(entity, state) { + let lc; + let lu; + if (!state) { + //if we don't know anything, all is now. + lc = Date.now(); + lu = Date.now(); + } else { + lc = state.lc || Date.now(); + lu = state.ts || Date.now(); + } + + try { + new Date(lc).getTime(); + } catch (e) { + entityData.adapter.log.debug('Invalid lc time for ' + state._id + ' in ' + entity.entity_id + ': ' + e); + lc = Date.now(); + } + try { + new Date(lu).getTime(); + } catch (e) { + entityData.adapter.log.debug('Invalid lu time for ' + state._id + ' in ' + entity.entity_id + ': ' + e); + lu = Date.now(); + } + + if (lc / 1000 > entity.last_changed) { + entity.last_changed = lc / 1000; + } + if (lu / 1000 > entity.last_updated) { + entity.last_updated = lu / 1000; + } +} + //required to read user name exports.getEnumName = getEnumName; @@ -449,6 +482,7 @@ exports.setJsonAttribute = setJsonAttribute; exports.getSmartName = getSmartName; exports.fillEntityIntoCaches = fillEntityIntoCaches; exports.extractValidEntityIds = extractValidEntityIds; //used to manage subscriptions. +exports.updateTimestamps = updateTimestamps; //used for intelligent update: exports.removeEntity = removeEntity; diff --git a/lib/server.js b/lib/server.js index 2a6947916..56c0fb8bb 100644 --- a/lib/server.js +++ b/lib/server.js @@ -777,12 +777,7 @@ class WebServer { // {id: 2, type: "event", "event": {"event_type": "state_changed", "data": {"entity_id": "sun.sun", "old_state": {"entity_id": "sun.sun", "state": "above_horizon", "attributes": {"next_dawn": "2019-05-17T02:57:08+00:00", "next_dusk": "2019-05-16T19:44:32+00:00", "next_midnight": "2019-05-16T23:21:40+00:00", "next_noon": "2019-05-17T11:21:38+00:00", "next_rising": "2019-05-17T03:36:58+00:00", "next_setting": "2019-05-16T19:04:54+00:00", "elevation": 54.81, "azimuth": 216.35, "friendly_name": "Sun"}, "last_changed": "2019-05-16T09:09:53.424242+00:00", "last_updated": "2019-05-16T12:46:30.001390+00:00", "context": {id: "05356b1a7df54b2f939d3c7f8a3e05b4", "parent_id": null, "user_id": null}}, "new_state": {"entity_id": "sun.sun", "state": "above_horizon", "attributes": {"next_dawn": "2019-05-17T02:57:08+00:00", "next_dusk": "2019-05-16T19:44:32+00:00", "next_midnight": "2019-05-16T23:21:40+00:00", "next_noon": "2019-05-17T11:21:38+00:00", "next_rising": "2019-05-17T03:36:58+00:00", "next_setting": "2019-05-16T19:04:54+00:00", "elevation": 54.71, "azimuth": 216.72, "friendly_name": "Sun"}, "last_changed": "2019-05-16T09:09:53.424242+00:00", "last_updated": "2019-05-16T12:47:30.000414+00:00", "context": {id: "e738dc26af1d48b4964c6d9805179595", "parent_id": null, "user_id": null}}}, "origin": "LOCAL", "time_fired": "2019-05-16T12:47:30.000414+00:00", "context": {id: "e738dc26af1d48b4964c6d9805179595", "parent_id": null, "user_id": null}}} if (entity.context.STATE.getId === id) { updated = true; - if (state.lc / 1000 > entity.last_changed) { - entity.last_changed = state.lc / 1000; - } - if (state.ts / 1000 > entity.last_updated) { - entity.last_updated = state.ts / 1000; - } + utils.updateTimestamps(entity, state); if (entity.context.STATE.getParser) { entity.context.STATE.getParser(entity, 'state', state); @@ -797,12 +792,7 @@ class WebServer { for (const attr of attributes) { updated = true; //only update if newer than already present time. - if (state.lc / 1000 > entity.last_changed) { - entity.last_changed = state.lc / 1000; - } - if (state.ts / 1000 > entity.last_updated) { - entity.last_updated = state.ts / 1000; - } + utils.updateTimestamps(entity, state); if (attr.getParser) { attr.getParser(entity, attr, state);