Ui-lovelace custom:button-card template not work

Tags: #<Tag:0x00007f326ad762b8> #<Tag:0x00007f326ad76178> #<Tag:0x00007f326ad76088>

Hi,

Why in custom:button-card not work this code

 label: >- 
            {{ relative_time(states.switch.tasmota2.last_changed) }}
- type: "custom:button-card"
          show_state: true
          color_type: card
          color: rgb(23, 65, 97)
          name: Lamp Julian
          icon: mdi:string-lights
          entity: switch.tasmota2
          label: >- 
            {{ relative_time(states.switch.tasmota2.last_changed) }}
          show_label: true
          styles: 
            state:
              - color: gray
              - font-size: 9px
            label:
              - color: gray
              - font-size: 9px
          state:
            - value: 'available'
              styles:
                state:
                - color: green
            - value: 'unavailable'
              styles:
                state:
                - color: white
                card:
                - background: #2980b9
                - filter: brightness(40%)
                name:
                - color: wihite

I need to read and display the time since last status update in seconds minutes hours …

How to solve this problem ?

relative_time is a jinja2 function, which means it isn’t available from the JS frontend. This is not the exact same use case, but it could be possible to use raw JS (although I haven’t managed to get the state to update once a minute yet). Refreshing the dashboard shows the right duration at least:

  - type: 'custom:button-card'
    entity: switch.biltak_motorvarmare
    name: Motorvärmare
    icon: 'mdi:car'
    show_state: true
    state_display: |
      [[[
        if (entity.state === 'on') {
          function humanReadableDuration(duration) {
              var hours = Math.floor(duration / 1000 / 60 / 60);
              var minutes = Math.floor((duration / 1000 / 60 / 60 - hours) * 60);

              var result = '';
              if (hours > 0) {
                  result += hours + ' h ';
              }
              
              return result + minutes + ' min';
          }

          var timeDiff = new Date().getTime() - new Date(entity.last_changed).getTime();
          return 'Stäng av (på i ' + humanReadableDuration(Math.abs(timeDiff)) + ')';
        }
        else
          return 'Lägg på';
      ]]]

Here’s a hack for getting the card to refresh every 10th second: https://github.com/custom-cards/button-card/issues/436#issuecomment-782891871