You don’t need a data template, just the value template. Since now()
has a weekday()
method, you can use that to directly map into a list of abbreviated localized weekday names:
# Abbr. localized date: Fr 31.07.
- platform: template
sensors:
date2:
friendly_name: "Datum"
entity_id: sensor.time
value_template: >
{% set days = ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So'] %}
{{ now().timestamp() | timestamp_custom(days[now().weekday()] ~ ' %d.%m.') }}
icon_template: mdi:calendar-range
We simply prepend the mapped localized day name to the custom timestamp here.
The weekday()
method of a Python date
or datetime
object returns the day of the week as an integer, where Monday is 0 and Sunday is 6. Thus, it can be easily used as a list index for the days
list (lists start with index 0).
Here’s a little challenge: Try to rewrite the above so it returns the abbreviated German weekdays plus a German month name, like Fr 31. Juli.
Hints:
- Set up a list called
months
with the German month names. -
now()
also has an instance attribute calledmonth
→now().month
. It returns the month numbers 1–12. - Remember, list indexes start at zero.
- The
~
operator in Jinja (the templating engine) concatenates strings, converting values to type string on the fly.
Have fun!
Edit: Strictly speaking, it is not necessary to update a date info every minute, as we do by specifying sensor.time
as entity ID to watch.
So if you have sensor.date
defined, use that instead. It will update only once per day, at midnight. This will save another few of the precious CPU cycles …