Weekday and month sensor

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:

well your error points to it missing in configuration.yaml

Found it:

It was added in December 2018 in version 0.84. It’s mentioned in the release notes but to say “not prominently” would be an understatement.

In fact, it’s not linked to the initial PR but to a follow-up PR that fixes it.

FWIW I was a novice back then and this filter’s stealthy introduction would have definitely flown undetected past my radar. :slightly_smiling_face:

Hmm corrected but still got the error:

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

IIRC it’s the only function/filter/test that’s not included in the docs.

replace ", " with ,

Geeze I was chasing a browser-error gost. That error keeped popping up nomatther what I did, needed a big browser refresh. Dunno why. So the final code i’m using is below. Works in template editor, only thing in practical the state is “unknown”…wait tell me it will only fill in the states tonight at 00:00 right? :scream::man_facepalming: In the previous “old” example it worked right away…

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 }}"
  - 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 }}"
1 Like

Yes, it will only calculate at midnight. Remove the trigger to have it calculate every minute. If you remove the trigger, make sure you put a - in front of the sensor line.

1 Like

Good afternoon, my apologies I’m new to home assistant and would love to implement your exact code. This is exactly what I’m looking for. Would you mint guiding me through the steps to implement this please?

please always use preformatted text when pasting code, so the quotes don’t get all screwed up (also tabs are preserved).
It’s a PITA to copy the code and replace them all manually :wink: