Show formatted date instead of state

I am fairly new to Home Assistant and completely new to YAML. I have this is in the right topic. To not make this post unnecessarily long I only show 1 sensor instead of 7.

So I have this code in a card on my dashboard:

type: custom:auto-entities
card:
  type: glance
  title: Afvalwijzer
  state_color: false
  show_name: true
  show_icon: true
  show_state: true
filter:
  include:
    - entity_id: sensor.afvalwijzer_gft_formatted
      options:
        name: GFT
        icon: mdi:leaf
        attribute: formatted
sort:
  method: state
  numeric: true
  reverse: false
  count: 4
show_empty: false

Problem is that it uses the “state” of the sensors and shows it as 1761865200.0. Each sensor also has a “formatted” attribute shown for example as “12 dec 25”. How do I get this formatted date instead of the “state date” with each sensor on my card?

This is the code in my configuration.yaml:

template:
  - sensor:
      - name: "Afvalwijzer GFT formatted"
        unique_id: afvalwijzer_gft_formatted
        state: >
          {% set d = states('sensor.afvalwijzer_gft') %}
          {% if d not in ['unknown', 'unavailable', 'none', ''] %}
            {{ as_timestamp(strptime(d, '%Y-%m-%d')) }}
          {% else %}
            unavailable
          {% endif %}
        attributes:
          formatted: >
            {% set d = states('sensor.afvalwijzer_gft') %}
            {% if d not in ['unknown', 'unavailable', 'none', ''] %}
              {% set maanden = {
                'January':'jan','February':'feb','March':'mrt','April':'apr',
                'May':'mei','June':'jun','July':'jul','August':'aug',
                'September':'sep','October':'okt','November':'nov','December':'dec'
              } %}
              {% set dag = as_timestamp(strptime(d, '%Y-%m-%d')) | timestamp_custom('%-d', true, 'Europe/Amsterdam') %}
              {% set maand_en = as_timestamp(strptime(d, '%Y-%m-%d')) | timestamp_custom('%B', true, 'Europe/Amsterdam') %}
              {% set jaar = as_timestamp(strptime(d, '%Y-%m-%d')) | timestamp_custom('%y', true, 'Europe/Amsterdam') %}
              {{ dag }} {{ maanden[maand_en] }} {{ jaar }}
            {% else %}
              Geen datum
            {% endif %}

Do a search here for formatting dates. As I recall, there is a huge thread about it.

The glance card does not support attributes so you will have to format your template sensor state as jeff advised. i.e. change this to what you want displayed instead of a timestamp:

        state: >
          {% set d = states('sensor.afvalwijzer_gft') %}
          {% if d not in ['unknown', 'unavailable', 'none', ''] %}
            {{ as_timestamp(strptime(d, '%Y-%m-%d')) }}
          {% else %}
            unavailable
          {% endif %}
1 Like

Great! That’s the solution. Thanks for the tip :slight_smile: