Weekday and month sensor

Here you all go. I wrote these templates for a finetuning, focusing on the notify service.
I want it to be as Google Assistant as possible.

My templates:

- platform: template
      value_template: "{{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()] }}"
      entity_id: sensor.time

- platform: template
      value_template: "{{ ['January','February','March','April','May','June','July','August','September','October','November','December'][now().month-1] }}"
      entity_id: sensor.time

- platform: template
      value_template: "{{ ['1st','2nd','3rd','4th','5th','6th','7th','8th','9th','10th','11th','12th','13th','14th','15th','16th','17th','18th','19th','20th','21th','22th','23th','24th','25th','26th','27th','28th','29th','30th','31th' ][ now().day-1] }}"

This can all be rewritten into my notify-script, that looks like this:

{% if now().strftime("%H")|int < 12 %}Good morning, {% elif now().strftime("%H")|int < 18 %}Good afternoon, {% else %}Good evening, {% endif %}{{ ["Master","Sir","Master Wayne","Sire"] | random }}.

The time is {{states.sensor.time.state}}.

Today is a {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()] }} and the date is the {{states.sensor.dateofthemonth.state}} of {{states.sensor.month.state}}.

It's currently {{states.sensor.dark_sky_summary.state}} and {{states.sensor.dark_sky_temperature.state}} degrees at home. Today will be {{states.sensor.dark_sky_hourly_summary.state|replace('.','')}}, with a high of {{states.sensor.dark_sky_daytime_high_temperature_0d.state|round(1)}} degrees.

{% if states.binary_sensor.workday_sensor %}It will take {{ states.sensor.drive_to_work.attributes.duration_in_traffic }} to drive to work today in the current traffic, since you're {{ states.sensor.drive_to_work.attributes.distance|replace('km','')|float }} kilometers away.{% endif %}

Have a{{ [" pleasant", " amiable", " charming", " cheerful", " delightful", "n enjoyable", " lovely", " pleasing", " satisfying"] | random }} day!

This results in a pleasant notification with a few randomly chosen names and titles after my pleasure:

Good afternoon, Master Wayne.

The time is 15:05.

Today is a Wednesday and the date is the 15th of May.

It’s currently Partly Cloudy and 14.0 degrees at home. Today will be Mostly cloudy until tomorrow afternoon, with a high of 14.0 degrees.

It will take 1 min to drive to work today in the current traffic, since you’re 0.8 kilometers away.

Have a** satisfying day**!


As you’ll notice, I have a preconfigured Dark Sky sensor, a Google Travel Time sensor and a datetime sensor.
Beware the Dark Sky sensor only works if you have enabled

  - 0

in the dark sky sensor configuration.

Here is mine:

- platform: darksky
  api_key: !secret dark_sky_api_key
  latitude: !secret latitude
  longitude: !secret longitude
  units: si
    - 0
    - summary
    - icon
    - precip_type
    - precip_intensity
    - precip_probability
    - temperature
    - temperature_high
    - apparent_temperature
    - dew_point
    - wind_speed
    - wind_bearing
    - cloud_cover
    - humidity
    - pressure
    - visibility
    - ozone
    - hourly_summary
    - daily_summary
    - uv_index

If you use sensor.time to trigger the template sensor, it will trigger every minute. This is needlessly frequent for a sensor that only needs to recalculate once a day.

Use sensor.date which will trigger once at the beginning of each day. To simplify the templates, use strftime. It can provides the names of the day and month (exclusively in English in Home Assistant).

- platform: template
      entity_id: sensor.date
      value_template: "{{ now().strftime('%A') }}"

      entity_id: sensor.date
      value_template: "{{ now().strftime('%B') }}"

      entity_id: sensor.date 
      value_template: >
        {% set suffix = ['st', 'nd', 'rd'] %}
        {% set day = now().day %}
        {% set index = 3 if day // 10 == 1 or day % 10 == 0 else (day % 10) - 1 %}
        {{ day~'th' if index > 2 else day~suffix[index] }}


  • Added qualification that strftime reports names exclusively in English in Home Assistant.
  • Updated dateofthemonth template to properly support exceptions: 1st, 2nd, 3rd, 11th, 12th, 13th, 21st, 22nd, 23rd, 31st.

The only reason the list was used is for people who don’t use English. Yeah the 1st 2 templates ‘%A’ and ‘%B’ are great for english speakers, but any other language will not work.

1 Like

According to the documentation for strftime, they’ll be rendered using the locale’s setting.

Weekday as locale’s full name.
Sunday, Monday, …, Saturday (en_US); Sonntag, Montag, …, Samstag (de_DE)

I haven’t tested it in Home Assistant. If it always defaults to English then I guess Home Assistant doesn’t support locale?

It’s always English in home assistant. It does not change the datetime locale.

1 Like

OK. Then for all those who need a language other than English, they can continue to use the other examples that have been provided (using a list).


I am sorry to raise an old thread, but I get this error when using your exact code. (I had amended it to my own, but then when I got the error I input yours but am still getting the error.

Can you help?

Error loading /config/configuration.yaml: while scanning for the next token
found character '%' that cannot start any token
  in "/config/scripts.yaml", line 363, column 17

Hi, I managed to solve this by moving “Good” to the front, and removing it from the morning, afternoon, evening…

Hi UIGUY, great you got it working. Can you share the code?
It works perfectly and has done for many versions with me.
Can you send an example of your faulty code?

I was wondering how to make a sensor that senses a day of the week relative to today.

So a sensor that would know today is Saturday and then tell me that in 2 days its Monday.

Something like this should work:

  - platform: time_date
      - 'time'
      - 'date'
      - 'date_time'
      - 'date_time_iso'
      - 'time_date'
      - 'time_utc'
      - 'beat'

  - platform: template
        value_template: >
          {{ (7 - now().weekday()) % 7 }}
        entity_id: sensor.date

now().weekday() returns a number representing the day of the week (0 for Monday and 6 for Sunday). Subtract that from 7 and calculate the remainder of the division by (modulo) 7 and you have the number of days until Monday.

Note that you have to refer to an entity that is updated from time to time (such as sensor.time or sensor.date - the latter changes once a day which should be enough) in order to update the sensor (the sensor only updates on state changes, and now() is a function and does not have a state, so the sensor won’t update unless you refer to an entity as shown in the example).

Wow - I have no idea how you came up with that algorithm for the st, nd, rd, and th - that’s absolutely brilliant! I had to plug it all into the developer template to figure it out. It’s now part of my sensors. Thanks, @123!

I’m trying to get the weekday and the next five weekdays on a weather forecast card. If today is Thursday, I want to have Friday to Tuesday displayed as the next five days.

I created 6 sensors, adding the desired number of days on each card:

  - platform: template
        value_template: "{{ ['Maandag','Dinsdag','Woensdag','Donderdag','Vrijdag','Zaterdag','Zondag'][now().weekday()] }}"
        value_template: "{{ ['Maandag','Dinsdag','Woensdag','Donderdag','Vrijdag','Zaterdag','Zondag'][now().weekday()+1] }}"
        value_template: "{{ ['Maandag','Dinsdag','Woensdag','Donderdag','Vrijdag','Zaterdag','Zondag'][now().weekday()+2] }}"


This works, however only for days within the current week: Monday and Tuesday are not displayed. It makes sense, as weekday 8 and 9 do not exist. Any ideas how to resolve this?


  - platform: template
        value_template: "{{ ['Maandag','Dinsdag','Woensdag','Donderdag','Vrijdag','Zaterdag','Zondag'][now().weekday()] }}"
        value_template: "{{ ['Maandag','Dinsdag','Woensdag','Donderdag','Vrijdag','Zaterdag','Zondag'][(now().weekday()+1) % 7] }}"
        value_template: "{{ ['Maandag','Dinsdag','Woensdag','Donderdag','Vrijdag','Zaterdag','Zondag'][(now().weekday()+2) % 7] }}"

That is something I neither would ever have figured out myself nor something I understand.

It works though, so thanks a lot!

If you understand what you did before then simply look up mod(ulo) operation :slight_smile:

Sorry to resurrect (again) an old thread, but is there a way to change the starting day of week to Sunday = 0, instead of Monday? Without going into the exhaustive historical reasons why this is accurate, suffice it to say that I’m struggling to code based on Monday being the first day, which should be Sunday. Maybe it’s just too late at night (CT, USA) and my brain can’t figure out the conversion… Thanks

strftime('%w') returns a value where 0 represents Sunday and 6 is Saturday.


{{ ['Zondag','Maandag','Dinsdag','Woensdag','Donderdag','Vrijdag','Zaterdag'][now().strftime('%w')|int(0)] }}
1 Like

Btw, unless you are a frequent time traveller or want to gain programming experience, displaying the current date in Home Assistant adds no value.

Value is in the eye of the beholder.
I missed a few hours of work the other day because I thought it was Tuesday, when it was Wednesday.
Its easy to loose track of the day of week when you work 8 days on, 6 days off. 14 day work schedule. Its all a blur.
Say that “day of week” has no value again.

1 Like