Weekday and month sensor

I know this thread is pretty dated, but the easiest way to get the day of week in a template is as follows:

{{ now().strftime('%A') }}

It is leveraging Python’s built in datetime format string to extract the day. No need to create a list etc.

4 Likes

Will this always return the week day in English or will this be localized?

The template returns the day of the week in English, it is not localized.
Insert the template into the development tools where you can see it.
Using the template, it is not a problem to create days of the week in your native language.

No it doesn’t. It uses the language that python is using and that’s always english, you have to use this post

Yes that was covered in a previous post in this thread.

2 Likes

Ok, thanks.
And my apologies for asking something that was already in a post.

It wasn’t marked as a solution, no apologies needed. Some of these old threads have been deserted by OP who marks the solution. Sometimes it’s best to mark a new solution as years go on, and you’re post pointed to that need.

1 Like

Thanks for all help this worked for me

service: notify.alexa_media_hallway
data:
message: >-
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 %}.
{{[‘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] }}
day of {{[‘January’,‘February’,‘March’,‘April’,‘May’,‘June’,‘July’,‘August’,‘September’,‘October’,‘November’,‘December’][now().month-1] }}
The time is now
{{ now().hour }} hundred hours and {{ now().minute }} minutes . Text `

How home assistant implements template sensors has changed so many of the examples early in the thread no longer work. I refactored @123’s example above to work with the current implementation.

- sensor:
    - name: "Day of the Week"
      state: "{{ now().strftime('%A') }}"

    - name: "Month"
      state: "{{ now().strftime('%B') }}"

    - name: "Day of the Month"
      state: >
        {% 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] }}

The examples I posted still work (they’re now referred to as Template Sensors in “legacy” format) but the entity_id option should be removed because it was deprecated many versions ago.

there more than one to skin cat

hes how I did mine

    - unique_id: 87100fb6-9c8f-41bf-8a87-aca4cc81bc06
      name: "Washing Count"
      state: >
        {% set i = (states("counter.washingload") | int) + 1 %}
        {%- macro suffix(d) %}
        {%- set sfx = {1:'st',2:'nd',3:'rd'} %}
        {{- 'th' if 11 <= d <= 13 else sfx.get(d%10, 'th') }}
        {%- endmacro %}
        {{- i }}{{ suffix(i) }}

I used to me which number of washing loads have been DONE

Yes there is…

    - unique_id: 87100fb6-9c8f-41bf-8a87-aca4cc81bc06
      name: "Washing Count"
      state: >
        {{ (states("counter.washingload") | int + 1) | ordinal }}
    - name: "Day of the Month"
      state: >
        {{ now().day | ordinal }}

Is no one aware of the ordinal filter?

Hmmm… looks like it’s missing in the docs.

4 Likes

thanks bro like it

So to be clear, the correct layout as dated now 11-12-2022 in configyaml should be right?

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

      maand:
        value_template: "{{ now().strftime('%B') }}"

      datum:
        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] }}

No wait, we needed entity_id: sensor.date so it updates everyday rigtht? Othewise it would’t update tomorrw. Or does it even when it is deprecated many versions ago?

that’s how it used to work, it’ll now update each minute when you use now(). YOu’re also using the old template format. You can still use it but you can also move to the new layout.

template:
- trigger:
  - platform: time
    at: "00:00"
  sensor:
  - name: Dag
    state: "{{ now().strftime('%A') }}"
  - name: Maand
    state: "{{ now().strftime('%B') }}"
  - name: Datum
    state: "{{ now().day | ordinal }}"

EDIT: This new format allows us to add triggers to it, so it’ll update when the trigger happens. You can also add another trigger to it so that it updates on home assistant restart.

1 Like

Dear Petro, thanks, that works! > CURRENTLY TESTING OUTSIDE TEMPLATE EDITOR
Could you be so kind to point me how to offset the day? I’d like to make 14 additional sensors showing each displaying one day back. I tried

now().day-1

for example, but that seems not to be supported anymore. I’m confused wher to find this offset information.

now().day - 1 will work, but it’ll go negative when crossing months.

{{ (now() - timedelta(days=1)).day | ordinal }}
{{ (now() - timedelta(days=1)).strftime('%A') }}
1 Like

I can confidently say that I wasn’t aware of its existence. It’s not in the list of built-in Jinja2 filters and, like you said, isn’t found in the Templating documentation.

Searching the forum for “ordinal” produced many results but (almost) none using the word as a Jinja filter. However, it’s used as a filter in this two year-old post (where it’s author states he got it from discord … so possibly from a developer). What I haven’t done is search Github to determine when, exactly, it was introduced. Anyway, it’s been undocumented for quite awhile.

It was added a very long time ago on when a series of template filters/functions were added. I’ve been using it for years. Sometime in 2019 IIRC.

EDIT: The guy who helped him was not a dev. It was @mono who used to frequent the template channel

Oooh wait I was wrong, it’s only working in the template editor. Not in configyaml.
In configyaml I input this:

template:
- trigger:
  - platform: time
    at: "00:00"
  sensor:
  - name: Dag
    state: "{{ now().strftime('%a') }}"
  - name: Maand
    state: "{{ now().strftime('%b') }}"
  - name: Datum
    state: "{{ now().day | ordinal }}"
  - name: Dag_datum
    state: "{{ now().strftime('%a') }}", "{{ now().day | ordinal }}"
  - name: Datum-1
    state: "{{ (now() - timedelta(days=1)).strftime('%a') }}, {{ (now() - timedelta(days=1)).day | ordinal }}"
  - name: Datum-2
    state: "{{ (now() - timedelta(days=2)).strftime('%a') }}, {{ (now() - timedelta(days=2)).day | ordinal }}"
  - name: Datum-3
    state: "{{ (now() - timedelta(days=3)).strftime('%a') }}, {{ (now() - timedelta(days=3)).day | ordinal }}"
  - name: Datum-4
    state: "{{ (now() - timedelta(days=4)).strftime('%a') }}, {{ (now() - timedelta(days=4)).day | ordinal }}"
  - name: Datum-5
    state: "{{ (now() - timedelta(days=5)).strftime('%a') }}, {{ (now() - timedelta(days=5)).day | ordinal }}"
  - name: Datum-6
    state: "{{ (now() - timedelta(days=6)).strftime('%a') }}, {{ (now() - timedelta(days=6)).day | ordinal }}"
  - name: Datum-7
    state: "{{ (now() - timedelta(days=7)).strftime('%a') }}, {{ (now() - timedelta(days=7)).day | ordinal }}"
  - name: Datum-8
    state: "{{ (now() - timedelta(days=8)).strftime('%a') }}, {{ (now() - timedelta(days=8)).day | ordinal }}"
  - name: Datum-9
    state: "{{ (now() - timedelta(days=9)).strftime('%a') }}, {{ (now() - timedelta(days=9)).day | ordinal }}"
  - name: Datum-10
    state: "{{ (now() - timedelta(days=10)).strftime('%a') }}, {{ (now() - timedelta(days=10)).day | ordinal }}"
  - name: Datum-11
    state: "{{ (now() - timedelta(days=11)).strftime('%a') }}, {{ (now() - timedelta(days=11)).day | ordinal }}"
  - name: Datum-12
    state: "{{ (now() - timedelta(days=12)).strftime('%a') }}, {{ (now() - timedelta(days=12)).day | ordinal }}"
  - name: Datum-13
    state: "{{ (now() - timedelta(days=13)).strftime('%a') }}, {{ (now() - timedelta(days=13)).day | ordinal }}"
  - name: Datum-14
    state: "{{ (now() - timedelta(days=14)).strftime('%a') }}, {{ (now() - timedelta(days=14)).day | ordinal }}"

But got error in the file editor:
bad indentation of a mapping entry (672:40)

669 | … Datum
670 | … : “{{ now().day | ordinal }}”
671 | … Dag_datum
672 | … : “{{ now().strftime(’%a’) }}”, “{{ now().day | ordinal }}”
------------------------------------------^
673 | … Datum-1
674 | … : "{{ (now() - timedelta(days=1)).strftime(’%a’) }}, {{ (now( …

and my config checker says:
Configuration invalid!

Error loading /config/configuration.yaml: while parsing a block mapping
in “/config/configuration.yaml”, line 671, column 5
expected , but found ‘,’
in “/config/configuration.yaml”, line 672, column 40

It seem to work until the “date”…

You’re missing

template: