Why does this custom button card text not render correctly, and how can I fix it?

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")) + "%";
    ]]]

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) ]]]"

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 :sweat_smile:

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

My apologies for completely missing that. :slight_smile:

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):

Screenshot 2025-02-07 at 14.34.28

      - 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

Nice, I like it.

1 Like