Skip to content

Commit e4c6cbe

Browse files
committed
Support last-changed and last-updated states on all entities (#101)
1 parent 9be239a commit e4c6cbe

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ attribute value instead of the state value. `icon` lets you display an icon inst
8787

8888
Note that `hold_action` and `double_tap_action` are currently **not** supported on additional entities.
8989

90+
#### Special attributes
91+
92+
Some special data fields from HA can be displayed by setting the `attribute` field to the following values:
93+
94+
| Value | Description |
95+
| --------------- | ------------------------------------------------------------------ |
96+
| `last-changed` | Renders the `last_changed` state of the entity if available |
97+
| `last-updated` | Renders the `last_updated` state of the entity if available |
98+
9099
### Secondary Info
91100

92101
The `secondary_info` field can either be _any string_ if you just want to display some text,

src/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { css, html, LitElement } from 'lit';
22
import { handleClick } from 'custom-card-helpers';
33

4+
import { LAST_CHANGED, LAST_UPDATED, TIMESTAMP_FORMATS } from './lib/constants';
45
import { checkEntity, entityName, entityStateDisplay, entityStyles } from './entity';
56
import { getEntityIds, hasConfigOrEntitiesChanged, hasGenericSecondaryInfo, hideUnavailable, isObject } from './util';
67
import { style } from './styles';
@@ -119,7 +120,14 @@ class MultipleEntityRow extends LitElement {
119120
if (config.toggle === true) {
120121
return html`<ha-entity-toggle .stateObj="${stateObj}" .hass="${this._hass}"></ha-entity-toggle>`;
121122
}
122-
if (config.format && ['relative', 'total', 'date', 'time', 'datetime'].includes(config.format)) {
123+
if (config.attribute && [LAST_CHANGED, LAST_UPDATED].includes(config.attribute)) {
124+
return html`<ha-relative-time
125+
.hass=${this._hass}
126+
.datetime=${stateObj[config.attribute?.replace('-', '_')]}
127+
capitalize
128+
></ha-relative-time>`;
129+
}
130+
if (config.format && [TIMESTAMP_FORMATS].includes(config.format)) {
123131
const value = config.attribute ? stateObj.attributes[config.attribute] : stateObj.state;
124132
const timestamp = new Date(value);
125133
if (!(timestamp instanceof Date) || isNaN(timestamp.getTime())) {

src/lib/constants.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ export const UNAVAILABLE = 'unavailable';
77
export const UNKNOWN = 'unknown';
88
export const UNAVAILABLE_STATES = [UNAVAILABLE, UNKNOWN];
99

10+
export const LAST_CHANGED = 'last-changed';
11+
export const LAST_UPDATED = 'last-updated';
12+
13+
export const TIMESTAMP_FORMATS = ['relative', 'total', 'date', 'time', 'datetime'];
14+
1015
export const SECONDARY_INFO_VALUES = [
1116
'entity-id',
1217
'last-changed',

0 commit comments

Comments
 (0)