Date String driving me crazy

Hallo everyone.

I have a bunch of template sensors

rest:

  • resource: !secret abfall_api
    scan_interval: 86400
    sensor:
    • name: Leerung_1
      json_attributes_path: ‘$.data[0].abholungen[0]’
      json_attributes:
      • leerungsdatum
        value_template: ‘{{as_timestamp(value_json.data[0].abholungen[0].leerungsdatum) | timestamp_custom (“%Y”)}}’
    • name: Leerung_2
      json_attributes_path: ‘$.data[0].abholungen[1]’
      json_attributes:
      • leerungsdatum
        value_template: ‘{{as_timestamp(value_json.data[0].abholungen[1].leerungsdatum) | timestamp_custom (“%Y”)}}’
    • name: Leerung_3
      json_attributes_path: ‘$.data[0].abholungen[2]’
      json_attributes:
      • leerungsdatum
        value_template: ‘{{as_timestamp(value_json.data[0].abholungen[2].leerungsdatum) | timestamp_custom (“%Y”)}}’

When I try to count the sensors equal to 2022 in this way it works perfectly.

  • platform: template
    sensors:
    leerungen_aktuell:
    friendly_name: “Anzahl der Leerungen Restmüll”
    value_template: >
    {% set leerung = [
    states.sensor.leerung_1,
    states.sensor.leerung_2,
    states.sensor.leerung_3,
    ] %}
    {{ leerung | selectattr(‘state’,‘eq’,‘2022’) | list | count }}

but how can I replace the 2022 in this statement with something like now().year that?

{{ leerung | selectattr(‘state’,‘eq’,‘2022’) | list | count }}

I tried nearly everything to get it work but the sensor shows always 0

I tried these statements:

{{ leerung | selectattr(‘state’,‘eq’,‘{{now().strftime(“%Y”)}}’) | list | count }}

{{ leerung | selectattr(‘state’,‘eq’,‘{{as_timestamp(now())|timestamp_custom(’%Y’)}}') | list | count }}

{{ leerung | selectattr(‘state’,‘eq’,‘{{as_timestamp(now().year)}}’)

Anyone any tips for me.

Thanks a lot.

Have you already tried


now().year|string

?

I tried it now.

{{ leerung | selectattr('state','eq','{{now().year|string}}') | list | count }}

Still 0 as result for the sensor.

{{ leerung | selectattr('state','eq', now().year) | list | count }}

Thanks for your reply, but it doesn’t work. Still get a zero as result

No, I should have been clearer. I meant: it without curly brackets.


{{ leerung | selectattr('state', 'eq', now().year|string)| list | count }}

1 Like

That’s it. Thank you very much. Sorry, bit I’m new to templating. There are a lot of big questionmarks in mmy head.

No need to apologise. :slight_smile:

1 Like