Invalid Timestamp - Template sensor

Hey!

I am trying to make a template sensor with the last_changed attribute of one of my motion sensors, so i can display it in lovelace.

This is what I have:

  - platform: template
    sensors:
      living_room_motion_sensor_last_changed:
        friendly_name: 'Living room Motion sensor last changed'
        value_template: "{{ states.binary_sensor.living_room_motion_sensor.last_changed }}"
        device_class: timestamp

The problem is it says “Invalid timestamp”. It has the correct timestamp in it and I can display the timestamp in lovelace, but for some reason it doesn’t recognize it as a timestamp and therefore doesn’t interpret it correctly in lovelace.

Any ideas?

Two things to try…

Try adding “as_local” to your template. For me the attributes “last_changed” and “last_updated” return a datetime object set for GMT… maybe that is conflicting with something…?

template:
  - sensor:
      - name:  'Living room Motion sensor last changed'
        state: "{{ states.binary_sensor.living_room_motion_sensor.last_changed | as_local }}"
        device_class: timestamp

Try adding “as_timestamp” to your template. Though, the docs say device_class: timestamp should work with datetime objects like “last_changed”.

template:
  - sensor:
      - name:  'Living room Motion sensor last changed'
        state: "{{ as_timestamp(states.binary_sensor.living_room_motion_sensor.last_changed) }}"
        device_class: timestamp

You don’t need a template for that. The entities card can show last changed as secondary_info.

Screenshot 2021-08-25 at 16-28-20 Administration - Home Assistant

Or the glance card can show the last_changed instead of the state (which you can get froom the icon)

Blockquote Try adding “as_timestamp” to your template. Though, the docs say device_class: timestamp should work with datetime objects like “last_changed”.

template:
  - sensor:
      - name:  'Living room Motion sensor last changed'
        state: "{{ as_timestamp(states.binary_sensor.living_room_motion_sensor.last_changed) }}"
        device_class: timestamp

I tried that and it gave me some really weird number. Didn’t try the other one.

In the end I solved it by using the custom:button-card, which can display the last_changed attribute.
Easier this way anyone. No need for any sensors.

in the end I solved it with the custom:button-card, which can show the last_changed attribute as well.

1 Like

Ha! I almost mentioned that as it is what I use for a more compact “glance” type display. Thought I’d stick with recommending core cards as the custom button card can be a bit daunting with its number of options.

Screenshot 2021-08-25 at 16-56-25 Overview - Home Assistant