Skip to content
This repository has been archived by the owner on Nov 4, 2023. It is now read-only.

Commit

Permalink
feat: support sensors which are timestamps (like uptime sensor) (#677)
Browse files Browse the repository at this point in the history
closes #671
  • Loading branch information
akloeckner authored Mar 29, 2021
1 parent 4126d1a commit b417812
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions scripts/globals/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions scripts/globals/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ export const playSound = function (sound) {
audio.play();
};

export const timeAgo = function (time) {
export const timeAgo = function (time, withoutSuffix = false) {
const momentInTime = moment(new Date(time));
return momentInTime.fromNow();
return momentInTime.fromNow(withoutSuffix);
};

export const debounce = function (func, wait, immediate) {
Expand Down

0 comments on commit b417812

Please sign in to comment.