muenze96
(Sebastian Meinke)
September 22, 2023, 9:47pm
1
I want to output a sensor (sensor.toni_handy_next_alarm) as a pure time and a second entity that outputs the date to it.
The sensor looks like this:
So the alarm is set to 18:11 and also only that I want to display. The following code in template.yaml does not work:
- sensor:
time_formatted:
friendly_name: Toni's Wecker
value_template: "{{ states('sensor.toni_handy_next_alarm') | timestamp_custom('%I:%M') }}"
icon_template: mdi:calendar-clock
In addition, I would like to create an entity from the millisecond count that also shows in mm:hh format how much time is left until the next alarm clock
I hope someone can help create the three sensors.
Thank you and best regards
Basti
1 Like
paddy0174
(Patrick)
September 22, 2023, 9:54pm
2
Do yourself a favor, and use Petros excellent macros for all time related:
Hello community!
As you may know, 2023.4 offers a new global template macro option in Home Assistant.
How does this benefit you? The introduction of this feature allows me to easily share all the templates I’ve helped you create over the years in a single easy-to-use template library! All in your native language!
Introducing…
Easy Time Jinja
A 1-stop-shop for easy time calculations.
Get it from HACS!
These templates are now available in HACS, with a caveat. In order to see templat…
Really, it isn’t worth the hassle, if you can do that easy (my opinion).
2 Likes
template:
- sensor:
- name: Toni's Wecker
state: |
{% if states('sensor.toni_handy_next_alarm') not in ['unavailable', 'None'] %}
{{ (states('sensor.toni_handy_next_alarm')|as_datetime|as_local).strftime('%H:%M') }}
{% else %} No Next Alarm{% endif %}
icon: mdi:calendar-clock
- name: Next Alarm Date
state: |
{% if states('sensor.toni_handy_next_alarm') not in ['unavailable', 'None'] %}
{{ (states('sensor.toni_handy_next_alarm')|as_datetime|as_local).strftime('%m-%d') }}
{% else %} No Next Alarm{% endif %}
icon: mdi:calendar-clock
2 Likes
muenze96
(Sebastian Meinke)
September 22, 2023, 10:06pm
4
in which file is this located? template.yaml?
It should go in configuration.yaml
or any file in which you have included the template integration… If you already have a template.yaml
file then, yes, it can likely go there.
muenze96
(Sebastian Meinke)
September 22, 2023, 10:36pm
6
works great! Is it possible to display the weekdays/months in German?
You should take @paddy0174 's advice and set up the Easy Time Macros, since it has translations built-in.
Other options include:
Just discovered the translate() function in Jinja2 that is very little documented, and could be quite useful in string-tidying and replacing operations. I stumbled across it whilst on this topic , looking for a short solution for mapping German month abbreviations to English ones (although note that the name “translate” is not about translating languages, but sets of characters).
Here’s two templates to do that job, first with a sequence of replaces, then with translate(). The dictionary full of…
Here’s a solution for weekday and month names that effectively duplicates the specific timestamp_custom output in Spanish, but it should be easier than this. I’m assuming your abbreviations are just the first three letters of each full name. Pop this in the template editor to adjust how you want, and swap in your timestamp in place of now() as required:
English: {{ now()|as_timestamp()|timestamp_custom("%a %d %h %H:%M") }}
Español: {{ "%s %d %s %02d:%02d" %
(["Lun","Mar","Mié","Jue…
To my knowledge, all functions of the Jinja2 templating language (and python) default to English (and don’t adapt to the operating system’s locale setting).
Your template will need to use mapping to translate ordinal day/month values to day/month names.
{{ "%s %d %s %d Ă %02d:%d" %
(["Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi","Dimanche"][now().weekday()],
now().day,
["Janvier","Fevrier","Mars","Avril","Mai","Juin","Juillet","Aout","Septembre","Octobre","Novembre","Decembre…
1 Like