Weekday and month sensor

Is there anyway I can have a sensor/template_sensors that say what weekday it is and one that says what mount it is? How do I do that?

Try this;

Use a template sensor and the built-in now() method.

I can’t get my head around hot this now() method works, but thanks I will se If I findy any more help about

1 Like

{{ now().weekday() }} used in a template sensor returns the index of the current day of week with 0 being Monday and 6 being Sunday. You can then use this value with an array of localized day names to create an appropriate outpit.

‘{% if now().weekday() in (1,) %} Monday {% elif now().weekday() in (2,) %} Tuesday…. {% endif %}’
‘{% if now().month in (1,) %} January……. {% endif %}’


Thanks @juan11perez , I will try this when I get back home!

Btw, unless you are a frequent time traveller or want to gain programming experience, displaying the current date in Home Assistant adds no value. Maybe you can explain your intention on that topic, because:

  • to identity if the current (or any) day is a workday or a national/regional holiday the workday sensor can be used
  • to use time and date in automation, the time trigger or time conditions are available.

@juan11perez Thanks this works, kind of. HomeAssistant think that it is day 6 today, Saturday but it is Sunday :thinking:

@m0wlheld The purpose is to send day an month (and more stuff) to a google sheet with the help of IFTTTT

The returned value is meant to be used as an array index, which usually start at 0. So - as I already wrote - 0 is Monday, 1 is Tuesday … 6 is Sunday.

@juan11perez Thanks! Got it to work with both!

welcome. glad it worked

Thanks for the tip about the workday sensor (which is brilliant, btw), but I don’t see how the time trigger or conditions can be used to take the date into account. The documentation only refers to hours, minutes, and seconds.

For anyone it can help:

Today is {% if now().weekday() in (0,) %}Monday{% elif now().weekday() in (1,) %}Tuesday{% elif now().weekday() in (2,) %}Wednesday{% elif now().weekday() in (3,) %}Thursday{% elif now().weekday() in (4,) %}Friday{% elif now().weekday() in (5,) %}Saturday{% elif now().weekday() in (6,)%}Sunday{% endif %}.


Thats a super long way of writing it out. Could be handled much better with this:

Today is {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()] }}


And while both options work in the template editor, neither of them is easily converted into a sensor - for me, at least, that is:

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

results in:

Error loading /home/homeassistant/.homeassistant/configuration.yaml: while parsing a block mapping
  in "/home/homeassistant/.homeassistant/sensors/day_of_the_week.yaml", line 4, column 7
expected <block end>, but found '<scalar>'
  in "/home/homeassistant/.homeassistant/sensors/day_of_the_week.yaml", line 4, column 29

That’s what keeps driving me crazy about working with templates.
The fact that they work in the template editor means basically: nothing at all!

1 Like

thats because you are using single quotes to encapsulate your template. You need to mix quotes. Single on the inside, double on the outside. Or double on the inside single on the outside.

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

The next thing to note is this will not update every day because it doesn’t have an entity to tether to. I suggest loading the time and date platform and using that. So your final solution would be:

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

Also as a side note in regards to this comment:

You can ensure that it works from the template editor by using a second line instead of making it single like, like so:

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

This method does not require the outside quotes, so you can write the code as-is.


Thanks - appreciate the explanation and the background :+1:

I read a lot of posts from you over the time and learned a lot. But I still often fail at the basics.
E.g. mixing quotes makes sense so that the system doesn’t get confused about what belongs together, but discovering the reason of why it fails in my case remains a big hurdle for me. Especially transferring something that works in the template editor to a piece of working config.

The error reading can be painful. They are all python errors, so you need python experience to understand them. Best bet to start learning them is to take a few free python tutorials online.

1 Like

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**!