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 %}