This code renders correctly:
type: custom:button-card
entity: person.tom
show_state: true
show_name: false
show_label: true
label: "[[[ return helpers.relativeTime(states['device_tracker.tom_phone'].last_changed); ]]]"
Now I would like it to instead say “Last Updated: X time ago”. But this seems to fail, rendering as [object Object]
:
label: "[[[ return 'Last Updated: ' + helpers.relativeTime(states['device_tracker.tom_phone'].last_changed); ]]]"
Trying as well to copy the example from the documentation here also gives the same result:
label: >
[[[
var lastupdated = helpers.relativeTime(states['device_tracker.tom_phone'].last_changed);
return 'Last Updated: ' + (lastupdated);
]]]
Can anyone help with how to achieve this?
Hellis81
(Hellis81)
February 6, 2025, 3:20pm
2
The only thing I can think of is that it’s trying to use plus as mathematics.
Try and replace the +
with ~
tom_l
February 6, 2025, 4:07pm
4
Try this:
label: " 'Last Updated: '[[[ return helpers.relativeTime(states['device_tracker.tom_phone'].last_changed); ]]]"
No unfortunately not, just renders literally:
Interestingly, if I remove the “relative time helper”, then it works (but ofcourse shows absolute rather than relative time):
Although the relative time helper is recommended in the docs here (scroll down a little).
Something to do with the object returned by this helper not being able to be converted to a string?
tom_l
February 6, 2025, 4:29pm
7
Have you tried using helpers.relativeTime(entity.last_changed)
Yes, it just renders as [object Object]
again.
WallyR
(Wally)
February 6, 2025, 4:49pm
9
Put the line into the template in developer tools.
Object object means you do not get a single value returned, but instead a complex object, which might be a list, a dict or some other set of data.
teeeeee
February 6, 2025, 4:52pm
11
But helpers.relativeTime
will not work in the Developer Tools template tester, right? Because it’s a JS function which is from the custom button card?
teeeeee
February 6, 2025, 4:55pm
12
Do you mean still use the device_tracker.tom_phone
syntax? Can you give the full line that I should try in the label
field?
parautenbach
(Pieter Rautenbach)
February 6, 2025, 4:58pm
13
Either will work, granted entity
is the device tracker.
states['device_tracker.tom_phone'].state.last_changed
Or:
entity.state.last_changed
teeeeee
February 6, 2025, 4:59pm
14
Okay. No, both these fail to show any result at all.
parautenbach
(Pieter Rautenbach)
February 6, 2025, 5:04pm
15
I checked the docs now and there might be an error (contradiction).
Please try this format:
Example: return helpers.formatDateTime(entity.attribute.last_changed)
teeeeee
February 6, 2025, 5:08pm
16
With the following:
label: "[[[ return 'Last Updated: ' + helpers.formatDateTime(entity.attribute.last_changed); ]]]"
I receive an error:
parautenbach
(Pieter Rautenbach)
February 6, 2025, 5:33pm
17
Make it plural: attributes
.
This is what I use in my own cards. Seems like that example in the docs is incorrect too.
teeeeee
February 6, 2025, 5:41pm
18
The following code as suggested also gives a similar error:
label: "[[[ return 'Last Updated: ' + helpers.formatDateTime(entity.attributes.last_changed); ]]]"
@parautenbach Could you double check the entire line for me? Make sure I didn’t do something unexpected?
Dirty hack:
label: |-
[[[
return html`
Last Updated:
<ha-relative-time
id="relative-time"
class="ellipsis"
.hass="${this._hass}"
.datetime="${states['device_tracker.tom_phone'].last_changed}"
></ha-relative-time>
`
]]]
It basically re-implements relativeTime()
(source ).
1 Like
teeeeee
February 6, 2025, 7:06pm
20
Nice. Thanks. This solves the problem:
There is an open Github issue thread for this here:
opened 08:33AM - 12 Sep 23 UTC
bug
**Checklist**
- [ x ] I updated the card to the latest version available
- [ x… ] I cleared the cache of my browser
**Describe the bug**
I can only use a single DateTime helper, the use of additional helpers or characters results in [object Object]
**Version of the card**
Version: 4.1.1
**To Reproduce**
This is the configuration I used:
```
type: custom:button-card
entity: input_datetime.datetime
show_icon: false
name: >
[[[ return helpers.formatDate(entity.state) + "<br>" +
helpers.relativeTime(entity.state); ]]]
show_label: true
label: >
[[[ return helpers.formatDate(entity.state) +
helpers.relativeTime(entity.state); ]]]
```
**Screenshots**

**Expected behavior**
I expected/hoped that it is possible to use multiple helpers in one.
**Desktop (please complete the following information):**
- Browser [Safari 16.6.1]
**Smartphone (please complete the following information):**
- Device: [iPad Air 3]
- OS: [16.6.1]
- Browser [Safari]
- Version [16.6.1]
**Additional context**
Home Assistant Supervised core-2023.9.1
It solves the “how can I fix it” part of the question for me, but not the “why” part!
1 Like