Date in local language

Hello,
i’m new in this forum, sorry if this topic is not in the right place.

My installation is Ubuntu 19.10, docker and HomeAssistant version 107.7, installed this way :

I want to get a day of the week sensor, so I built a command line sensor with command: ‘date +%A’
This works and answers : Saturday.

But I want the answer “samedi” in french.
My ubuntu is french localized, when I ssh on it and type date+%A I well get “samedi”.
It seems the sensor command line environment is not, because of Docker ?
I tried the command env LC_ALL fr_FR.UTF-8;dat +%A but the answer is still Saturday and not “Samedi”.

What should I do ? I suppose there must be something in Docker config, but where ?

Thanks for help

1 Like

Home Assistant does not use language localization (from the operating system). Everything that a template produces will always be in English. To translate a weekday into French (or any other language) you can do something like this:

{% set jours = ["Lundi", "Mardi","Mercredi","Jeudi","Vendredi","Samedi","Dimanche"] %}
{{ jours[now().weekday()] }}
3 Likes

Thanks for help, but am totally newbie in HA, not yet looked at formatting…
I tried to place this in a sensor definition in configuration.yaml like this :

sensor:
  - platform: template
    name: jourFR
    value_template : {%set .....      weekday()] }}

but that doesnt works : I get
"Error loading /config/configuration.yaml: while scanning for the next token
found character ‘%’ that cannot start any token
in “/config/configuration.yaml”, line 24, column 22 " which is the first % before set

I am really sorry, I know this is basic knowledge of HA, but just a little more help would make me win much time !
thanks

If you use the template I suggested in a Template Sensor then it will not update periodically because the template contains no entities, only the now() function. You can learn more about it here: Templates without entities using now()

To ensure this Template Sensor gets updated, we specify an entity_id. The template only needs to be updated once a day, at the beginning of each day. There is an entity that does that and it’s sensor.date which updates at the start of each new day. To use sensor.date, we first have to define it. The instructions can be found here: Time & Date

sensors:
  - platform: time_date
    display_options:
      - 'date'

  - platform: template
    sensors:
      jourFR:
        friendly_name: 'Jour FR'
        entity_id: sensor.date
        value_template: >-
          {% set jours = ["Lundi", "Mardi","Mercredi","Jeudi","Vendredi","Samedi","Dimanche"] %}
          {{ jours[now().weekday()] }}
2 Likes

Thanks, done, it works.
At the moment jourfR is “indisponible” , as you explained I expect it to be updated next night.

This example showed me how to use templates, great thanks.

1 Like

Complement : it is OK just now, I found in home-assistant.log I had made a syntax error…

Nothing new, but to copy&paste for those who are using german format:

date_de:
  entity_id: sensor.date
  friendly_name: "Date du jour"  
  icon_template: mdi:calendar-today          
  value_template: >
    {% set months = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"] %}
    {% set days = ["Montag", "Dienstag", "Mittwoch", "Donnestag", "Freitag", "Samstag", "Sonntag"]  %}
    {{ days[now().weekday()] + ', der ' + now().day | string + '. ' + months[now().month-1] }}
3 Likes

A lot has changed in Home Assistant since April 2020. The most important is that a Template Sensor no longer supports the entity_id option.

In other words, this:

  entity_id: sensor.date

can be safely removed because it is ignored by Home Assistant.

Another change is that Template Sensors support the now() function. If it is present in the template, it causes the template to be evaluated every minute.

Whereas in the past, the examples shown above would be evaluated only when sensor.date changed state, in other words at the start of every new day (midnight), they are now evaluated every minute because they include the now() function. Instead of being evaluated once a day, at midnight, it is now evaluated 1440 times a day.

The most recent version of Home Assistant (2021.4) introduced the concept of Trigger-based Template Sensors. It allows you to use a trigger to specify precisely when a Template Sensor should be evaluated.

The original example can be redesigned as a Trigger-based Template Sensor like this:

template:
  - trigger:
    - platform: time
      at: '00:00:00'
    - platform: homeassistant
      event: start
    sensor:
    - name: 'Jour FR'
      state: >
        {% set jours = ["Lundi", "Mardi","Mercredi","Jeudi","Vendredi","Samedi","Dimanche"] %}
        {{ jours[now().weekday()] }}

It will be updated only at midnight and whenever Home Assistant starts.

Your example can also be redesigned as a Trigger-based Template Sensor (see documentation):

template:
  - trigger:
    - platform: time
      at: '00:00:00'
    - platform: homeassistant
      event: start
    sensor:
    - name: 'Date du jour'
      icon: mdi:calendar-today 
      state: >
        {% set months = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"] %}
        {% set days = ["Montag", "Dienstag", "Mittwoch", "Donnestag", "Freitag", "Samstag", "Sonntag"]  %}
        {{ days[now().weekday()] }}, der {{ now().day | string }}. {{ months[now().month-1] }}
4 Likes

Im not able to get this to work in my template. I have got an entity called 'sensor.afvalwijzer_next_date. It shows the date like 31-08-2023. I want to show only the weekday, like monday etc. I have made this template that works:

sensors:
      ophaaldag:
        friendly_name: "Volgende Ophaaldag"
        value_template: "{{ as_timestamp(strptime(state_attr('sensor.afvalwijzer_next_date', 'year_month_day_date'), '%Y-%m-%d')) | timestamp_custom('%A') }}"

But I want to change the language of the day to dutch. I have tried this, bit it does nog work. How someone can help me out, thanks!

sensors:
      ophaaldag:
        friendly_name: "Volgende Ophaaldag"
        value_template: >
          {% set days = ["Maandag", "Dinsdag","Woensdag","Donderdag","Vrijdag","Zaterdag","Zondag"] %}
          {{ days[as_timestamp(strptime(state_attr('sensor.afvalwijzer_next_date', 'year_month_day_date'), '%Y-%m-%d')) | timestamp_custom('%A')] }

Copying and pasting your template code into the developer tools shows an error about a extra closing bracket:

TemplateSyntaxError: unexpected '}'

Adding an extra one to the end shows another error:

ValueError: Template error: strptime got invalid input 'None' when rendering template '{% set days = ["Maandag", "Dinsdag","Woensdag","Donderdag","Vrijdag","Zaterdag","Zondag"] %} {{ days[ as_timestamp(strptime(state_attr('sensor.afvalwijzer_next_date', 'year_month_day_date'), '%Y-%m-%d')) | timestamp_custom('%A')] }}' but no default was specified

I don’t have the sensor to try myself, but you should definitely use the developer tools to get the template right.

I get the following error in the developer tools:

UndefinedError: ‘list object’ has no attribute ‘Thursday’

Looks like the list of days in dutch is not linked to the day of the sensor?

I have solved it like this:

     ophaaldag:
        friendly_name: "Volgende Ophaaldag"
        value_template: >
          {% set days = ["Zondag", "Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag"] %}
          {% set date_str = state_attr('sensor.afvalwijzer_next_date', 'year_month_day_date') %}
          {% set day_index = as_timestamp(strptime(date_str, '%Y-%m-%d')) | timestamp_custom('%w') | int %}
          {{ days[day_index] if 0 <= day_index < 7 else 'Invalid Day' }}

Not sure if this is the best way but it seems to work.

It’s not the most elegant way. This is shorter:

value_template: >
    {{ ('Maandag','Dinsdag','Woensdag','Donderdag','Vrijdag','Zaterdag','Zondag') 
       [as_datetime(state_attr('sensor.afvalwijzer_next_date', 'year_month_day_date')).weekday()] }} "

Declares a tuple (like a list, but immutable and potentially more efficient / faster to store) of Dutch weekday names, and indexes the correct one from that tuple based on the weekday.

as_datetime() documented here and works because your date string format is well-chosen.
weekday() docs here.

Your previous attempt was trying to index a list based off an English weekday name. For that to work, you’d need days to be a dictionary like:

{'Monday':'Maandag','Tuesday','Dinsdag',...}

Alternatively, if you don’t mind installing something:

Then you can use:

{% from 'easy_time.jinja' import weekday %}
{{ weekday(as_datetime(state_attr('sensor.afvalwijzer_next_date', 'year_month_day_date')).weekday()+1) }}
1 Like

with that in mind, could we ‘smartify’ this too:

{%- set stamp = as_timestamp(strptime(states('sensor.afvalwijzer_next_date'),
                         '%d-%m-%Y',none),none) %}
{%  set maanden = ['Januari','Februari','Maart','April','Mei','Juni','Juli',
                   'Augustus','September','Oktober','November','December'] %}
{%- set dagen = ['Zondag','Maandag','Dinsdag','Woensdag','Donderdag',
                 'Vrijdag','Zaterdag'] %}
{%- set dag = stamp|timestamp_custom('%w',default=none)|int(default=0) %}
{%- set maand = stamp|timestamp_custom('%m',default=none)|int(default=0) %}
{{stamp|timestamp_custom(dagen[dag] ~ ' %-d ' ~ maanden[maand-1],default=none)}}

?

Thanks @Ralf, modified it for Hungarian:


  - platform: template
    sensors:
      date_hu:
        friendly_name: "Magyar dátum"  
        icon_template: mdi:calendar-today          
        value_template: >
            {% set months = ["Január", "Február", "Március", "Április", "Május", "Június", "Július", "Augusztus", "Szeptember", "Október", "November", "December"] %}
            {% set days = ["Hétfő", "Kedd", "Szerda", "Csütörtök", "Péntek", "Szombat", "Vasárnap"]  %}
            {{ days[now().weekday()] + ', ' + now().day | string + '. ' + months[now().month-1] }}

why don’t you add your translations to GitHub - Petro31/easy-time-jinja: Easy Time calculations for Home Assistant templates and use that?
suppose that would be a welcome addition

1 Like