How do I write a template for a new binary sensor that simply checks whether a specific event name is on the calendar for today, and then sets true/false?
This works:
service: calendar.list_events
data:
start_date_time: "{{ today_at() }}"
end_date_time: "{{ today_at('23:59:59') }}"
target:
entity_id: calendar.dpw
response_variable: agenda
This does not work:
# Binary Sensors
binary_sensor:
- platform: template
sensors:
test:
friendly_name: 'my_test'
value_template:
{% set agenda_list = agenda.events %}
{% if 'string' in agenda_list %}
True
{% else %}
False
{% endif %}
Automation to update daily
alias: Calendar 1-Day Agenda to Event
trigger:
- platform: time
at: "00:01:00"
condition: []
action:
- service: calendar.list_events
data:
start_date_time: "{{ today_at() }}"
end_date_time: "{{ today_at('23:59:59') }}"
target:
entity_id: calendar.my_calendar
response_variable: agenda
- event: custom_calendar_agenda
event_data:
calendar: my_calendar
cal_events: "{{ agenda }}"
mode: single
Template binary sensor:
template:
- trigger:
- platform: event
event_type: custom_calendar_agenda
event_data:
calendar: my_calendar
binary_sensor:
- name: Calendar Agenda Search "My String"
unique_id: custom_calendar_agenda_search_01_001
state: |
{{ trigger.event.data.cal_events['events']
| map(attribute='summary')
| select('search', 'my string')
| list | count > 0 }}
Thank you, but this throws an error for the state:
value:
UndefinedError: ‘trigger’ is undefined
Where are you seeing that error? You can’t test templates that reference the trigger
variable in the Template editor unless you provide the value for the variable.
Goobaroo
(Jason McLeod)
December 31, 2023, 3:18am
5
Looks like calendar.list_events
has been deprecated for calendar.get_events
. This adds another layer to the data returned.
I was also unable to get select('search', 'my string')
to work. search
wasn’t a valid filter for select. I had to use equalto
alias: Calendar 1-Day Agenda to Event
trigger:
- platform: time
at: "00:01:00"
condition: []
action:
- service: calendar.get_events
data:
start_date_time: "{{ today_at() }}"
end_date_time: "{{ today_at('23:59:59') }}"
target:
entity_id: calendar.my_calendar
response_variable: agenda
- event: custom_calendar_agenda
event_data:
calendar: my_calendar
cal_events: "{{ agenda }}"
mode: single
Binary Sensor:
template:
- trigger:
- platform: event
event_type: custom_calendar_agenda
event_data:
calendar: my_calendar
binary_sensor:
- name: Calendar Agenda Search "My String"
unique_id: custom_calendar_agenda_search_01_001
state: |
{{ trigger.event.data.cal_events['calendar.my_calendar']['events']
| map(attribute='summary')
| select('equalto', 'my string')
| list | count > 0 }}