djalexnl
(Alexander de Boer)
1
Hi there,
I have created a template sensor from Agenda items…
If an agenda item Summary on a specific day contains “search term1” (lower case) it wil count it up. Works perfectly!
sensor:
- name: Agenda test
unique_id: agenda_test
state: "{{ agenda_test.events | selectattr('summary', 'search', 'search term1', true) | list | count() | lower }}"
Can i do the same with two or more search terms? and how do i accomplish that?
So if contains “search term1” or “search term2” or “search term3” and then count them up.
How do i accomplish that?
Thanks in advanced!
Are the terms mutually exclusive or can any given summary contain more than one search term?
Here's an example you can play with in the Template editor
{% set agenda_test = namespace(events=
[{'summary': 'term1'},
{'summary': 'term2'},
{'summary': 'term3'},
{'summary': 'term4'},
{'summary': 'term5 term1'},
{'summary': 'term5'} ])%}
sensor:
- name: Agenda test Exclusive
unique_id: agenda_test
state: >
{{ agenda_test.events
| selectattr('summary', 'search', 'term1|term5', true)
| list | count() }}
- name: Agenda test Non Exclusive
unique_id: agenda_test
state: >
{% set term_list = ['term1', 'term5']%}
{%- set ns = namespace(count=0)%}
{%- for event in agenda_test.events %}
{%- for term in term_list %}
{%- if event.summary is search(term) %}
{%- set ns.count = ns.count + 1 %}
{%- endif %}{%- endfor %}{%- endfor %}
{{ ns.count }}
1 Like
djalexnl
(Alexander de Boer)
3
Thank you so much… could not find this part with the | part…
I hope someone else finds this usefull in the future.
Again big Thankssss!!!
sensor:
- name: Agenda test
unique_id: agenda_test
state: "{{ agenda_test.events | selectattr('summary', 'search', 'search term1|search term2|Search term3', true) | list | count() | lower }}"