Show date in timestamp if it's not today

I use a sensor in configuration to get the date and time of the last time that the door was opened and then be able to show that timestamp with a label using a custom button card.
in configuration

door_last_changed_formatted:
        value_template: "{{ as_timestamp(states.binary_sensor.door.last_changed) | timestamp_custom('%d.%m - %H:%M') }}"

and in the card

label: |
  [[[
    return states['sensor.door_last_changed_formatted'].state;
  ]]]

I’d like to be able to hide the date and show only the time if the date is the same with today. Would that be possible to compare the timestamp with the current date and hide it? Should I change the format of the timestamp? I like to show it in the label as it is now (’%d.%m - %H:%M’).

I think I found a solution and i’m sharing it in case any newbie like me needs it.
In configuration I created a sensor.date and time and date sensors for the last time my door status changed:

- platform: time_date
  display_options:
    - "time"
    - "date"

sensor:
  - platform: template
    sensors:
      current_date:
        friendly_name: "Current Date"
        value_template: "{{ as_timestamp(states('sensor.date')) | timestamp_custom('%d/%m') }}"
      dooropen_last_date_changed:
        value_template: "{{ as_timestamp(states.binary_sensor.door_status.last_changed) | timestamp_custom('%d/%m') }}"
      dooropen_last_time_changed:
        value_template: "{{ as_timestamp(states.binary_sensor.door_status.last_changed) | timestamp_custom('%H:%M') }}"

Then I use this label in a custom button card to show only the time if the date is the same with current_date or else show both date and time:

label: >
  [[[ if (states['sensor.current_date'].state == states['sensor.dooropen_last_date_changed'].state)
    return states['sensor.dooropen_last_time_changed'].state
  else return states['sensor.dooropen_last_date_changed'].state+" - "+states['sensor.dooropen_last_time_changed'].state ]]]

Screenshot 2021-09-21 at 13.53.18 Screenshot 2021-09-21 at 13.53.38
If there is a better way I’d like to know it.

2 Likes