Entity-button card with timestamp in name

Hi all,

I want to add a timestamp to the name parameter in a entity-button card.

My card config:

cards:
  - type: entity-button
    entity: script.mqtt_refresh
    name: Manual refresh
    icon: mdi:refresh
    icon_height: 50px
    tap_action:
      action: call-service
      service: script.turn_on
      service_data:
        entity_id: script.mqtt_refresh

I tried this, but without success

cards:
  - type: entity-button
    entity: script.mqtt_refresh
    name: Manual refresh, last refresh:  {{as_timestamp(states.sensor.ebusd_aussentemperatur.last_changed) | timestamp_custom('%d.%m.%Y %H:%M') }}
    icon: mdi:refresh
    icon_height: 50px
    tap_action:
      action: call-service
      service: script.turn_on
      service_data:
        entity_id: script.mqtt_refresh

Hopefully you can give me a hint how to solve this

Michael

You can’t just use templates wherever you please. Templates only work on template fields. Typically templates cannot be done in the frontend section, i.e. lovelace. The best way to figure out if you can use a template is to look at the docs or if the field name has _template in it.

You cannot show last changed on this entity-button card. It just isn’t possible at the moment. You can however switch to the custom:button-card and use JS templates to place the last_changed in the name. A defualt configuration option for the custom:button-card is to display last-changed info if you use the field show_last_changed: true.

Thanks a lot retro for the hint and explanation, now I got it :ok_hand:

cards:
  - type: custom:button-card
    entity: script.mqtt_refresh
    name: >
      [[[ return 'Manual refresh<br>Last refresh: ' + states['sensor.ebusd_aussentemperatur_last_changed_formatted'].state ]]]
    size: 10%
    icon: mdi:refresh
    icon_height: 50px
    tap_action:
      action: call-service
      service: script.turn_on
      service_data:
        entity_id: script.mqtt_refresh
sensor:
  - platform: template
    sensors:
      ebusd_aussentemperatur_last_changed_formatted:
        value_template: "{{ as_timestamp(states.sensor.ebusd_aussentemperatur.last_changed) | timestamp_custom('%d.%m.%Y %H:%M') }}"

Hi, first time here on the forum and loving Home Assistant! I’m basically trying to do the same, just not getting anything out of it

In my configuration.yaml I have the following:

  - platform: template
    sensors:
      poolsense_last_seen_formatted:
        value_template: "{{ as_timestamp(states.sensor.poolsense_last_seen) | timestamp_custom('%a, %d %b %H:%M') }}"

No error messages at startup, but the value or state stays None. Any recommendations?

syntax is wrong, try this

        value_template: "{{ as_timestamp(states('sensor.poolsense_last_seen')) | timestamp_custom('%a, %d %b %H:%M') }}"

This of course is assuming that states(‘sensor.poolsense_last_seen’) returns a string with the format: “2020-10-05T03:30:00.00000+00:00”

Petro, thanks for the reply.

The entity state returns the following value: 10/6/2020, 6:42:05 AM. Its not in exactly the same format but it is of type timestamp. I have tried your recommendation, but still no luck.

That’s not a valid string timestamp for as_timestamp. You need to use strptime. And because you’re using strptime, you’ll need to account for an unavailable sensor.

        value_template: >
          {% set v = states('sensor.poolsense_last_seen') %}
          {% if v not in ['unknown', 'unavailable'] %}
            {% set fmat = "%m/%d/%Y, %I:%M:%S %p" %}
            {{ as_timestamp(strptime(v, fmat)) | timestamp_custom('%a, %d %b %H:%M', false) }}
          {% endif %}
        availability_template: "{{ states('sensor.poolsense_last_seen') not in ['unknown', 'unavailable'] }}"

Absolutely brilliant - works like a charm. Thanks for the effort Petro - much appreciated!

Good morning.
I’ve a similar problem with button card and as_time stamp:

In The administrative tools → models

{{as_timestamp(states['sensor.record_temperatura_massima'].attributes.quando) | timestamp_custom('%d/%m/%Y alle ore: %H:%M')}}

working fine.

this is an excerpt of the code of an advanced button card

[...]
  info2: |
    [[[
      return `<ha-icon
        icon="mdi:calendar"
        style="width: 12px; height: 12px; color: white;">
        </ha-icon><span> registrata il: <span style="color: var(--text-color-sensor);">${states['sensor.record_temperatura_massima'].attributes.quando}%</span></span>`
    ]]]

[...]

works but in agreement with the “quando” attribute, the output is a timestamp

Why this not working?

[...]
  info2: |
    [[[
      return `<ha-icon
        icon="mdi:calendar"
        style="width: 12px; height: 12px; color: white;">
        </ha-icon><span> registrata il: <span style="color: var(--text-color-sensor);">${as_timestamp(states['sensor.record_temperatura_massima'].attributes.quando) | timestamp_custom('%d/%m/%Y alle ore: %H:%M')}%</span></span>`
    ]]]

[...]

Thanks