parautenbach
(Pieter Rautenbach)
February 6, 2025, 7:20pm
21
I’m a bit baffled myself. I don’t see anything wrong.
Here is a full label definition I use that works just fine:
label: >
[[[
var b = entity.attributes.brightness;
return Math.round(parseFloat(b ? (b/255)*100 : "0")) + "%";
]]]
parautenbach
(Pieter Rautenbach)
February 6, 2025, 8:29pm
22
This definitely works.
label: "[[[ return helpers.formatDateTime(entity.last_changed) ]]]"
label: "[[[ return helpers.formatDateTime(states['binary_sensor.some_entity'].last_changed) ]]]"
label: "[[[ return helpers.relativeTime(entity.last_changed) ]]]"
label: "[[[ return helpers.relativeTime(states['binary_sensor.some_entity'].last_changed) ]]]"
teeeeee
February 6, 2025, 10:25pm
23
Yes, but thats basically the example I gave in the OP.
The issue arises when you try to convert it to a string to prefix it with some other text (in my case, wanting to display “Last Updated” before it".
I should have looked there, someone already posted a very similar solution to mine
The issue is that relativeTime()
doesn’t return a string (that you can prefix with another string), but an object (a “lit html template”) that gets rendered by the underlying button-card code. The reason for this is that it’s not a static value, but it gets updated as the time passes.
1 Like
parautenbach
(Pieter Rautenbach)
February 7, 2025, 12:36pm
25
My apologies for completely missing that.
robertklep:
The issue is that relativeTime()
doesn’t return a string (that you can prefix with another string), but an object (a “lit html template”) that gets rendered by the underlying button-card code. The reason for this is that it’s not a static value, but it gets updated as the time passes.
Thanks for the insight. I missed that relativeTime()
is an active object.
Since this was still bugging me, I’d also like to offer a native custom button card solution (just to show what’s possible):
- type: custom:button-card
name: "Test"
entity: input_boolean.test
show_label: true
size: 20%
label: "Label:"
custom_fields:
t: "[[[ return helpers.relativeTime(entity.last_changed) ]]]"
styles:
card:
- font-size: 12px
grid:
- grid-template-areas: '"n n" "i i" "l t"'
- grid-template-rows: 1fr
- grid-template-columns: 1fr 1fr
label:
- align-self: right
- justify-self: end
- padding-right: 1ex
custom_fields:
t:
- align-self: left
- justify-self: start
2 Likes