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

feat: support sensors which are timestamps (like uptime sensor) #677

Merged
merged 3 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Comment on lines +453 to +455
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just thinking aloud but I was wondering if device_class === timestamp implies that it's the "uptime" type of sensor? Because that change might not make sense for sensors other than uptime types.

But since I don't know the cases where those sensors are used, I won't argue with it and assume that it's a good default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We didn't want to argue. :-) Still...

I have not found any other standard HA integration with timestamp device class. So, uptime seems to be the most common use case.

However, there are people using custom sensors, such as "last time that file changed". Also for those sensors, timeAgo is a decent default for a dashboard display.

Then, there might be power users that really want to see the actual timestamp. They would have to change the filter then.

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