custom-cards / button-card

❇️ Lovelace button-card for home assistant
MIT License
1.93k stars 233 forks source link

helpers.relativeTime(date) doesn't work in variable #736

Closed basbruss closed 1 year ago

basbruss commented 1 year ago

Checklist

Describe the bug

return helpers.relativeTime(entity.state)

It only works when it is used directly in a YAML key as name or label It does not work when it's included in a variable.

Version of the card Version: v4.0.0-dev17

To Reproduce This is the configuration I used:

      - type: custom:button-card
        template: card_title
        entity: sensor.sun_next_dawn
        label: "test relative time"
        name: >
          [[[
            return helpers.relativeTime(entity.state)
          ]]]
      - type: custom:button-card
        template: card_title
        entity: sensor.sun_next_dawn
        label: "test relative time"
        name: >
          [[[
            return variables.test
          ]]]
        variables:
          test: >
            [[[
              return helpers.relativeTime(entity.state)
            ]]]

It renders the error

TypeError: e.trim is not a function
    at ki._getTemplateOrValue (button-card.js?hacstag=14619432540017:440:2813)
    at button-card.js?hacstag=14619432540017:440:2770
    at Array.forEach (<anonymous>)
    at ki._getTemplateOrValue (button-card.js?hacstag=14619432540017:440:2747)
    at button-card.js?hacstag=14619432540017:440:2770
    at Array.forEach (<anonymous>)
    at ki._getTemplateOrValue (button-card.js?hacstag=14619432540017:440:2747)
    at button-card.js?hacstag=14619432540017:440:2770
    at Array.forEach (<anonymous>)
    at ki._getTemplateOrValue (button-card.js?hacstag=14619432540017:440:2747)

Screenshots

Expected behavior Both cards should return the same values

Desktop (please complete the following information):

Smartphone (please complete the following information):

Additional context Add any other context about the problem here.

RomRider commented 1 year ago

I can't reproduce, that is strange...

          - type: custom:button-card
            entity: switch.skylight
            name: >
              [[[
                return variables.test
              ]]]
            variables:
              test: >
                [[[
                  return helpers.relativeTime(entity.last_changed)
                ]]]

image

RomRider commented 1 year ago

Are there any other templates in the config template template: card_title?

basbruss commented 1 year ago

Somehow by many times refreshing the frontend cache and restarting the dev-container the issue dissappeared