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.