Template time format still english instead german

Hi all,

I want to play a little bit with templating. I have a markdown card to show me the Actual weekday and date in german.
Sadly it shows it in english despite the language in my profile is set to german. Also I have in my configuration.yaml the timezone set to Europe/Berlin and have entered the correct longitude/lattitude.

Any clues what the solution could be?

Thanks a lot!

So what did you try ?
Where is your template ?
How can anyone help when they don’t know what you actually want, i.e. A sample output

Sorry. I thought I just oversaw something and didn’t had to blow up the post.

Here we go.

This is part of the initial code:

{% if now().month in (1,) %}
 ## {{ now().day }} januar 
 {% elif now().month in (2,) %}
## {{ now().day }} februar

This puts out just the date and the month (correctly in German due the manual translation)

I want a complete sentence to be display ("It is Sunday, the 01. June - of course in the correct German translation)

Therefore I tried (with help from the guys from the discord server) this code only:


{{now().strftime('%A') + " " + now().strftime('%d') |int| ordinal + ". " + now().strftime('%B') }}

Which display Weekday and date but only in English.

Therefore I tried a mix of both codes which gives me at least the month in German due the manual translation again


{% elif now().month in (6,) %}
## Es ist {{now().strftime('%A') + " " + now().strftime('%d') |int| ordinal + ". " + now().strftime('%B') }} Juni

Long story short - this is what I’m looking for but I need everything in German (Monday=Montag)

I have no idea what this means.

So is it okay if I just give you the ‘part’ of the answer ?

Now I could look up the various parts but it seems like you want someone to do the templating AND the reference work.
I am assuming that you want the format to be “Es ist” ‘Montag’ “, der.” ‘01’ “.” ‘Juni’ "
With the single quoted items substituted for the actual calendar terms ?

You are aware that the python timestamp objects only return in English and will therefore need to be translated with dictionary arrays ?

With part of the initial code I just wanted to shortcut it.

As it lists basically January and February it just goes down till December. But here is the whole one:


      - type: markdown                  ## Date
        content: |
         {% if now().month in (1,) %}
          ## {{ now().day }} Januar
          {% elif now().month in (2,) %}
          ## {{ now().day }} Februar
          {% elif now().month in (3,) %}
          ## {{ now().day }} März 
          {% elif now().month in (4,) %}
          ## {{ now().day }} April 
          {% elif now().month in (5,) %}
          ## {{ now().day }} Mai
          {% elif now().month in (6,) %}
          ## {{ now().day }} Juni 
          {% elif now().month in (7,) %}
          ## {{ now().day }} Juli 
          {% elif now().month in (8,) %}
          ## {{ now().day }} August
          {% elif now().month in (9,) %}
          ## {{ now().day }} September
          {% elif now().month in (10,) %}
          ## {{ now().day }} Oktober 
          {% elif now().month in (11,) %}
          ## {{ now().day }} November 
          {% elif now().month in (12,) %}
          ## {{ now().day }} Dezember 
          {% endif %}
        style: |
         ha-card {
            margin: 12px 14px -5px 14px;
            box-shadow: none;
            --paper-card-background-color: rgba(0, 0, 0, 0.0)
          }
          h2 {
            font-size: 18px;
            font-weight: 400;
            padding-left: 10px;
            border-left: 3px solid rgba(81, 134, 236);
          }

Now I could look up the various parts but it seems like you want someone to do the templating AND the reference work.

I didn’t ask for someone providing me a full code. As I’m started with looking deeper into templating an python I just have not a real Idea where to start and why I’m asking in this forum.

I am assuming that you want the format to be “Es ist” ‘Montag’ “, der.” ‘01’ “.” ‘Juni’ "
With the single quoted items substituted for the actual calendar terms ?

Exactly.

You are aware that the python timestamp objects only return in English and will therefore need to be translated with dictionary arrays ?

No I’m not.

Looked up my previous post and it’s written confusing. Apologies for that. English is not my first language. And it’s also pretty late here.

Understood, no worries, I was up late you were at least an hour ahead of me.
I’ll take a look in the next couple of hours.

1 Like

I’d just create two lists for the weekday and month names. Don’t know of any easier inbuilt methods.

This works for me in the template editor:

{% set weekdays = ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"] %}
{% set months = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"] %}
{{ "Es ist " + weekdays[now().weekday()] + ", der " + now().strftime("%-d") + ". " + months[now().month - 1] + " " +  now().strftime("%Y")}}

And gives me:

Es ist Montag, der 1. Juni 2020

If you want to keep the leading zero in front of the day, change this :

now().strftime("%-d")

to this

now().strftime("%d")

4 Likes

Beaten to the punch by our German speaking Swiss.
:rofl:
Dang, that was gonna be my puzzle for this morning
:sob:

1 Like

I don’t think Germans would call our language German :rofl:

I’m sorry, didn’t wanna ruin your morning :frowning_face: But I’m sure there are enough other ouzzles here in the forim, that are a bit more challenging :sunglasses:

1 Like

Maybe, but I’m getting old and anyway I like to start the week easy.
:rolf:

Why ? Do you have a ‘bad’ accent ? Can’t be any worse than … (I’ve heard guys from Munchen complain about people from the South East of Bavaria ! (Maybe it’s anyone from the South East of ANY country ? :rofl: :rofl: :rofl:, nah, only kidding ! )

2 Likes

Thats it! Thanks a lot @Mutt and @Burningstone !

image

I read a little about to define the german translations and played a little with it but was not successful. I think I dont have the understanding yet how the reference between the code to display the date and the “translation” is made.

If I understand the entire code correctly: “weekdays” and “months” can be named anything, correct? I just put the definition behind it?
Also strftime is only used to display the “Date Number” and the year, right?
And: How is the beginning and end of the code defined? Or is this not necessary for python?

P.S. I accept Swiss German as a really cute form of german :smiley:

1 Like

Oooooh that’s a low blow ! :rofl:

Basically you need to gain access to HA’s python sandbox (an area where code can run in a low risk area, where evaluations can only do limited things like printing). To do this we submit them as templates. The template is rendered in Jinja2 a print formatting language, there are 4 things that can be put in Jinja2

  1. raw text - bad, the results can be unpredicatble
  2. comments
  3. evaluations
  4. print statements (bear with me, we put print statements in and this is what we get out)
{# this is a comment, I can put anything in here #}
{{ 'This is a print statement and I can use it to include standing text' }}
{# the following is an evaluation setting a variable to a name #}
{% set brian = 'A Very Naughty Boy !' %}
{# ... and printing that out (for example) : - #}
{{ 'Brian is ' ~ brian }}
{# prints "Brian is A Very Naughty Boy !" #}
{# the tilde charachter (~) concatenates (joins) strings #}
{# square brackets [] can be used to define an array, a list of items from which you can pick #}
{# so your weekdays are : - #}
{% set weekdays = ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"] %}
{# and similarly your months are : - #}
{% set months = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"] %}
{# weekdays[3] takes the 4th element of 'weekdays', 0 is the first #}
{{ "Es ist " + weekdays[now().weekday()] + ", der " + now().strftime("%-d") + ". " + months[now().month - 1] + " " +  now().strftime("%Y")}}
{# This last is merely a print statement assembling all the little bits together #}

now().weekday() returns the day of the week for now(), 0 to 6, 0 = Monday, 1 = Tuesday etc
now().month returns 1 to 12 hence the -1 to align the array
And the rest is just your personal bent on arranging the wording.

Apparently there are 22 different ways of telling the time across Germany with one option being “It is 2 minutes past, 10 minutes to 3” So I won’t comment on your format.

Hope that helps

Yep ! could be called ‘yellow’ or ‘monkey_nuts’

This is your template (enter it into the template editor under developers tools)
If you create (say) a sensor it would look like : -

sensor:
  - platform: template
    sensors:
      today_is:
        entity_id: sensor.date
        friendly_name: Today Is : -
        value_template: >
          {% set yellow = ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"] %}
          {% set monkey_nuts = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"] %}
          {{ "Es ist " + yellow[now().weekday()] + ", der " + now().strftime("%-d") + ". " + monkey_nuts[now().month - 1] + " " +  now().strftime("%Y")}}

the “>” indicates this is a multi-line template and thus a) starts on the line below b) doesn’t need quotes (otherwise put double quotes on the outside and singles on the inside)
[Note: I copied Burnings template, and he’s a bit sloppy with discipline ( it appears ! :roll_eyes: ) ]

This will appear as an entity when you restart.

2 Likes

Woaw…this explains a lot. Thanks @Mutt!

That means basically I can use templates for mathematical things too and convert them into a sensor? I get a raingauge delivered in hopefully less than a monkeynut and need to convert the pulses in rainfall (mm per square meter)

Is there somewhere a documentation readable for Germans with Twins who don’t care about a proper sleep schedule? Means: simple? :smiley:

Not really, the basic stuff is python (I just explained the jinja wrapping). Though Google may offer something. And many examples on this forum.
The sandbox does not support all python (that would be dangerous) but a limited subset.
Mathematical and time options are the most common uses. I would classify yours above to be a bit ‘text biased’ .
The template editor is your best friend, have a play.
Any issues, just ask a question, template specialists are a subset of people here but they are good and ‘mostly’ very helpful - explaining ‘why ?’
We love a good puzzle.
As Marius says (a guy here on the forum) whatever the question, the answer is usually a template

2 Likes

Flo, (I assume that’s Florian ???)
There is another thread that goes into time and calculation in a bit more depth.
Yeah, I know, you will tend to see the same faces popping up in such threads.
It ‘may’ help as a reference if you need something in the future.

Note: Burning got the solution credit on this one too :angry: ( :rofl: ) but read the whole thread (it’s long one) for some of the wrinkles we worked out towards the end.
Mainly to get the idea of what is possible.

1 Like

For that I’d use the Python Script integration. It’s used a bit differently as it just provides a service, but in terms of functionality it’s a bit more flexible, as it’s really Python. Here I posted an example to calculate absolute humidity. It was also solved using templates by others. So there’s no right or wrong here. But I guess it’s good to know about the possible alternatives. :+1:

1 Like

Thats correct :slight_smile: But i get chills to hear the full name as I heard it only from my Mum and Grandmother when I was in big trouble :smiley:

Thanks for the link. Looks interesting and I think is the solution for my other problem I have: Shutting down a notification automation after it send the message. I have a rainsensor (a little to sensitive as it seems) and when it´s on it should send the mentioned message. Sadly that thing switches of to fast which results in a “rainfall of notifications” (pun intended)

Will dig through this and try to understand :slight_smile:

I assume when this raingauge will arrive you guys will have your puzzle. And maybe you´ll be first :slight_smile:

Ah this looks a little familar to the PHP scripts i stole from my other Smarthome Platform forums before :slight_smile:
Aaaand i think I´ll use this as I need the absolute humidity too :slight_smile:

But what is the difference between a service and a sensor in this case? Don´t i need a sensor anyways to start an automation? In my case it will be: If it rains x mm/m² don´t run the irrigation which should start at xxx am/pm

I’ve heard guys from Munchen complain about people from the South East of Bavaria

I’m working on a Bavarian time script. Can I get some help converting 7:45 to dreiviertel acht? :rofl:

1 Like