Date formatting from a value/Retrieve day of the week

Hey there,

I’m not much of a programmer and require some help. I thought, this would be the best place to ask.
How would I go about formatting this array value so that it only displays the day of the week.

Here’s what I’m currently getting from the entity:

datetime.2022.10.29

I would like to convert and format this value so that it only gives me the day of the week (Mon, Tue, Wed…).

ddatetime.2022.10.29 - Copy

Here’s the code:

- sensor:
    - unique_id: weather
      state: template
      attributes:
        weather: >
          {% set d1_datetime = state_attr('weather.forecast_home', 'forecast')[-5]['datetime'] %}
          {% set d1_condition = state_attr('weather.forecast_home', 'forecast')[-5]['condition'] %}
          {% set d1_temperature = state_attr('weather.forecast_home', 'forecast')[-5]['temperature'] %}
          {% set d1_templow = state_attr('weather.forecast_home', 'forecast')[-5]['templow'] %}

          {{ d1_datetime }}
          <font color='#6a7377'>
          {{ d1_condition | title }},
          </font>
          {{ d1_temperature | title }}°C
          <font color='#6a7377' size="2">
          {{ d1_templow | title }}°C
          </font><br>

Any help appreciated. Thanks.

{{ d1_datetime.strftime('%a') }}

%A will give you the full day e.g. Saturday instead of Sat.

1 Like

Appreciate the reply.
I’ve tried that initially but got hit with a null value.

null2.2022.10.29-016

Can you post the text output (not a screen shot) of this in the template editor:

{{  state_attr('weather.forecast_home', 'forecast')[-5]['datetime'] }}

2022-10-30T12:00:00+00:00

I’ve used the following code to display the current weekday:

          {% set weekday = now().strftime('%a') %}
          {{ weekday }}

This gives me:

Sat

And the following code to display the current weekday as an integer:

          {% set weekday_num = now().strftime('%w') %}
          {{ weekday_num }}

This gives me:

6

But I’m having trouble, and wouldn’t know the syntax for creating a for loop that would add +1 to that integer while it is less than 7. If I’m able to do that I can then convert that integer to string format.

I pretty much want to display the days of the week and their forecast without having to use a weather card for a more uniform look and for custom flexibility. I’ve tried utilizing other weather cards to retrieve their attributes as some will contain the weekday as a string value but to no outcome.

Try this:

{{ as_datetime(d1_datetime).strftime('%a') }}
1 Like

That’s it! Thank you so much.

Appreciate the hasty help!

1 Like

Here’s the final result:

finitoweather.2022.10.29-018

I’m trying to do something similar to what you have here. Can you please post the code you used for the sensor to get this output?