Extract weekday from calendar-event

I’ve got the connection to GoogleCal working and I have a sensor that gives starting date and time in a kind-of raw format.

Is it possible to extract the day of week that next event is planned?

J

you can do it like this:
{{as_timestamp(states.calendar.calendar_name.attributes.start_time) | timestamp_custom('%A',false) }}
if you want the short day of week, replace %A with %a

1 Like

Sweet! Thanks a bunch!

If I’d like localized daynames for sweden, would that be possible to?

So a question came up about this. If you set the language in HA to Spanish, does the underlying operating system also change? So that when you do a low level date format like this, does Monday return as Lunes?

I researched this a bit and I don’t think it’s possible. There doesn’t appear to be a way to set the language of the system and even then, it’s not clear if python will honor that. In python, you’d need to import locale, set the location and langauge then use datetime objects.

you’d need to build a lookup table for localised names:

{% set weekday = ["Söndag", "Måndag", "Tisdag", "Onsdag", "Torsdag", "Fredag", "Lördag"] %}
{% set calday = as_timestamp(states.calendar.calendar_name.attributes.start_time) | timestamp_custom('%w',false) | int%}
{{weekday[calday]}}
1 Like

Do I place this inside the template? Seems like the parser ain’t too happy 'bout it, should it be placed somewhere else?

it depends what you want to do with it. Is it to be displayed on the front ui?
Is it to be sent in a notification or TTS?
the key bit will be to use data_template or value_template instead of data and the bit I provided goes in there between single quotes ('):

    - service: tts.google_say
      entity_id: media_player.google_home_minis
      data_template:
        message: > 
          {% set weekday = ["Söndag", "Måndag", "Tisdag", "Onsdag", "Torsdag", "Fredag", "Lördag"] %}
          {% set calday = as_timestamp(states.calendar.calendar_name.attributes.start_time) | timestamp_custom("%w",false) | int%}
          {{weekday[calday]}}

It is to be used in the frontend, HADashboard as a final destination.

so what I provided should work, you’ll have to create a template sensor for it and its value will be the message I provided.

Awesome! Many thanks for your time :slight_smile:
Works like a charm :slight_smile:

1 Like