Get weekday in my local language?

Hi!

I am making a text-to-speech greeting script that is talking my local language. I want it to tell me the weekday and the only way I have found to do this is using now().strftime(%A). But the problem with this is that it gives me the weekday in English, and not in my local language. I know i can change this in python with locale.setlocale(). But where in home assistant would i set it? Home assistant is running in my local language.

Or is there another way i can get weekdays in my own language?

Thanks!

1 Like

Hi @Dobrethat, i’m running a script outside of the venv with crontab at 00:01 that publishes the local weekday name to a mqtt topic.
in HA then, i use the mqtt sensor

Hi all

I recall this topic to understand if there is any other way to get localized name of weekdays.
My needs is to use it in a message to send to a media player like this in an automation.yaml
action:

  • data_template:
    message: “text with localized day of the week”
    entity_id: media_player.mymedia

Any idea or solution already used?

Thanks

Hello @woody4165,
I am using this:

- service: tts.google_say
  data_template:
    entity_id:
    - media_player.camera_da_letto
    message: >
      {%- set date = as_timestamp(now()) -%}
      {% set giorno = ["Domenica", "Lunedì", "Martedì", "Mercoledì", "Giovedì", "Venerdì", "Sabato"] %}
      {% set mese = ["Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"] %}
      {% set m_ok = date | timestamp_custom("%m") | int %}
      {% set g_ok = date | timestamp_custom("%w") | int %}
      {% set giorno_della_settimana = giorno[g_ok] %} 
      {% set giorno_del_mese = date | timestamp_custom("%d") %}
      {% set mese = mese[m_ok - 1] %}
      Buongiorno! Oggi è {{ giorno_della_settimana }} {{ giorno_del_mese }} {{ mese }}

Of course, you will have to translate days and months! :wink:

1 Like

Grazie.
Italian is the language I was looking for.

FYI the now object has these attributres and you don’t need to convert them to a timestamp.

- service: tts.google_say
  data_template:
    entity_id:
    - media_player.camera_da_letto
    message: >
      {% set giorno = ["Domenica", "Lunedì", "Martedì", "Mercoledì", "Giovedì", "Venerdì", "Sabato"] %}
      {% set mese = ["Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"] %}
      {% set giorno_della_settimana = giorno[now().weekday()] %}
      {% set mese = mese[now().month - 1] %}
      Buongiorno! Oggi è {{ giorno_della_settimana }} {{ now().day }} {{ mese }}
2 Likes

@petro!

Thanks, mate! :wink:

@boggiano You probably already noticed, but posting this for anyone else who might find this topic. The following will actually give the wrong day:

{% set giorno_della_settimana = giorno[now().weekday()] %}

You should use now().isoweekday() instead of now().weekday().

This is because the array giorno has 7 items, but retrieving e.g. the first item requires you to use index 0 instead of 1.

Today for e.g. is a Tuesday, so (my Italian isn’t that great) I’m guessing that should be “Martedi”, but now().weekday() returns 1 on a Tuesday which corresponds to “Lunedi” when used like giorno[now().weekday()]. If you use giorno[now().isoweekday()] you do get the correct day because now().isoweekday() returns 2 on a Tuesday which results in “Martedi”.

Or you could keep using now().weekday() and simply change your giorno array to start with Monday:

["Lunedì", "Martedì", "Mercoledì", "Giovedì", "Venerdì", "Sabato", "Domenica"]
1 Like

Hi,
i’m writing this for helping other people.
i’ve set google calendar native support and used this code to read the next event in calendar and then use it in a entity.

in configuration.yalm:

sensor:
  - platform: template
    sensors:
      template_calendar_veritas:
        value_template: >
          {%- set date = as_timestamp(strptime(states.calendar.CALNAME.attributes.start_time, "%Y-%m-%d %H:%M:%S")) -%}
          {% set giorno = ["Domenica", "Lunedì", "Martedì", "Mercoledì", "Giovedì", "Venerdì", "Sabato"] %}
          {% set mese = ["Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"] %}
          {% set m_ok = date | timestamp_custom("%m") | int %}
          {% set g_ok = date | timestamp_custom("%w") | int %}
          {% set giorno_della_settimana = giorno[g_ok] %}
          {% set giorno_del_mese = date | timestamp_custom("%d") %}
          {% set mese = mese[m_ok - 1] %}
          {{ giorno_della_settimana }} {{ giorno_del_mese }} {{ mese }}: {{ states.calendar.CALNAME.attributes.message }}
        friendly_name: CALNAME
        unit_of_measurement: ''

change CALNAME with the identifier of the calendar you want.

in lovelace:

entity: sensor.template_calendar_CALNAME
icon: 'mdi:calendar'
name: Next Event
type: entity

part of the code found here: