From da5ba16cbcc49800fe11a1449e5a7757f4416356 Mon Sep 17 00:00:00 2001 From: Andreas' Laptop Date: Sat, 27 Mar 2021 23:17:43 +0100 Subject: [PATCH 1/2] feat: support sensors which are timestamps (notably the uptime sensor) * use the default `filter` option for sensors * parse with `timeAgo` in case we have a timestamp * include the word `ago`, because timestamps are indeed ago * add hint how to exclude the word `ago` for the uptime sensor Localization should be handled separately, and globally for all places where `moment` is used. In order to allow the user to profit from this further localization, `moment()` should be then be made available by `utils.js`. closes #671 --- README.md | 7 +++++-- scripts/globals/constants.js | 8 ++++++++ scripts/globals/utils.js | 4 ++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7a3fa512..282e447d 100644 --- a/README.md +++ b/README.md @@ -328,8 +328,11 @@ Tile Object. [Click here for some real-life examples](TILE_EXAMPLES.md) value: '&sensor.bathroom_temp.state', /* unit: Override default unit of measurement */ unit: 'kWh', - /* filter: Function for filtering/formatting the entity value */ - filter: function (value) {return value}, + /* filter: Function for filtering/formatting the entity value + * Is used by default to format a timestamp as time ago, e.g., for the uptime sensor. + */ + filter: function (value, item, entity) {return Math.round(value);}, + filter: function (value, item, entity) {return timeAgo(value, true);}, // true = exclude the word 'ago' /** type: DEVICE_TRACKER **/ /* slidesDelay: Delay before slide animation starts (optional) */ slidesDelay: 2, diff --git a/scripts/globals/constants.js b/scripts/globals/constants.js index 777d4bfe..1d0d454c 100644 --- a/scripts/globals/constants.js +++ b/scripts/globals/constants.js @@ -448,6 +448,14 @@ export const TILE_DEFAULTS = { return this.$scope.callScript(item, entity); }, }, + [TYPES.SENSOR]: { + filter (value, item, entity) { + if (entity?.attributes?.device_class === 'timestamp') { + return timeAgo(value, false); + } + return value; + }, + }, [TYPES.SWITCH]: { action (item, entity) { return this.$scope.toggleSwitch(item, entity); diff --git a/scripts/globals/utils.js b/scripts/globals/utils.js index e4ca54df..4cbfa1e9 100644 --- a/scripts/globals/utils.js +++ b/scripts/globals/utils.js @@ -54,9 +54,9 @@ export const playSound = function (sound) { audio.play(); }; -export const timeAgo = function (time) { +export const timeAgo = function (time, omitAgo) { const momentInTime = moment(new Date(time)); - return momentInTime.fromNow(); + return momentInTime.fromNow(omitAgo ?? false); }; export const debounce = function (func, wait, immediate) { From d611b914f4384ac0771423fcb681d81f24b0f8ca Mon Sep 17 00:00:00 2001 From: Andreas' Laptop Date: Mon, 29 Mar 2021 21:21:34 +0200 Subject: [PATCH 2/2] address review --- scripts/globals/utils.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/globals/utils.js b/scripts/globals/utils.js index 9a247e61..22c6cca3 100644 --- a/scripts/globals/utils.js +++ b/scripts/globals/utils.js @@ -56,9 +56,9 @@ export const playSound = function (sound) { audio.play(); }; -export const timeAgo = function (time, omitAgo) { +export const timeAgo = function (time, withoutSuffix = false) { const momentInTime = moment(new Date(time)); - return momentInTime.fromNow(omitAgo ?? false); + return momentInTime.fromNow(withoutSuffix); }; export const debounce = function (func, wait, immediate) {