Entity Card timestamp formatting

Ok, been pulling my hair out over this.

I have a REST API that returns the last time my backup script finished in Unix a timestamp. I would like to set up a card in HA that displays how long it’s been since that happened. I also have custom css in play to alert me when it’s been too long since a backup. I’ve tried putting the relative_time function in the lovelace config, as well as templates section of the configuration.yaml. I’ve also tried using the timestamp_local in both places for a less-than-ideal solution. Thus far, no-go. I have also tried implementing a few things from the Epic Timestamp thread, but nothing is sticking.

Here is what I have, currently:


  - platform: rest
    name: "Backup Status"
      - backupTimeStamp
    resource: https://tools.blahblah.net/cgi/local_stats.pl?query=backup
    timeout: 10
    scan_interval: 10
    value_template: 'OK'
  - platform: template
        friendly_name: 'backup_timestamp'
        value_template: '{{ states.sensor.backup_status.attributes["backupTimeStamp"] }}'
        entity_id: sensor.backup_timestamp

lovelace Entity Card Configuration:

entity: sensor.backuptimestamp
icon: 'hass:refresh'
name: Backup Timestamp
style: |
  ha-card {
    background: {% if states("sensor.backuptimestamp")| int + 604800 < as_timestamp(now()) %} #730101 {% else %} #017301 {% endif %};
type: entity

This is what I currently have displaying.

What am I missing?

What you see is the epoch time, that is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT). So you have to convert this to a human readable date.
Look at this for more information:

I actually don’t know how to do this in a template sensor, but maybe this is the right hint to google something…

Add device_class: timestamp to your template sensor

Well, I wasn’t able to get relative_time working. I think that the docs could use more examples in the formatting section.

I found a workable brute-force-ish solution here.