and what does the state look like then?
then it’s working, and you have no events
Verify the template works in the template editor and the get_events service call are getting events
Remove the commented out lines or use Jinja comment delimiters {# #}
, since you are using multi-line quote indicators.
Edited config.
calendar:
- platform: caldav
username: !secret icloud_user
password: !secret icloud_password
url: https://caldav.icloud.com
calendars:
- Spielplan_U17_24-25
- Spielplan_Damen_24-25
- Spielplan_U20_24-25
template:
- trigger:
- platform: time_pattern
minutes: /1
action:
- service: calendar.get_events
data:
duration:
days: 9
target:
entity_id:
- calendar.spielplan_u17_24_25
- calendar.spielplan_damen_24_25
- calendar.spielplan_u20_24_25
response_variable: agenda
- variables:
ev_list: |
{% set ns = namespace(cal_events=[]) %}
{%- for key, value in agenda.items() %}
{%- for event in value.events %}
{%- set ns.cal_events = ns.cal_events + [event] %}
{%- endfor %}
{%- endfor %}
{{ ns.cal_events | sort(attribute='start') | list }}
sensor:
- name: Naechste_Spiele
unique_id: "naechste_spiele"
state: "{{ ev_list | count }}"
attributes:
list: "{{ ev_list }}"
test: "0.10"
icon: mdi:calendar
service call get_events works in dev-tools and copied this output into the template editor I get the error: ‘ev_list’ is undefined
the variable ‘ev_list’ isn’t get by the sensor.
Then there would be errors in your logs about that
I don’t think they converted the variable definition from YAML to Jinja in the Template tool.
The Template tool does not understand YAML, in order to test the “state” and “list” templates you will need to define ev_list
in Jinja as follows:
{% set ns = namespace(cal_events=[]) %}
{%- for key, value in agenda.items() %}
{%- for event in value.events %}
{%- set ns.cal_events = ns.cal_events + [event] %}
{%- endfor %}
{%- endfor %}
{% set ev_list = ns.cal_events | sort(attribute='start') | list %}
That should solve the ‘ev_list’ is undefined
error. Also make sure the variable name copied from the Action tool matches “agenda”.
I have tested it in a new, clean homeassistant installation.
Yes, there is an error in the logs:
Template variable warning: ‘ev_list’ is undefined when rendering ‘{{ ev_list }}’
And you won’t get that error if you have this action
- variables:
ev_list: |
{% set ns = namespace(cal_events=[]) %}
{%- for key, value in agenda.items() %}
{%- for event in value.events %}
{%- set ns.cal_events = ns.cal_events + [event] %}
{%- endfor %}
{%- endfor %}
{{ ns.cal_events | sort(attribute='start') | list }}
So you’re either looking at the wrong error, you’re not properly making the variable as @Didgeridrew said when you transfer it over to the template editor.
In the template tool it works fine, if I copy the answer from the action in the template tool.
But in the yaml configuration it didn’t work.
You can’t use the exact same template in both places. YOu have to use
in your script.
And remove those
The two lines are removed.
And I understand, that there is a difference in code for yaml and for template editor.
With the code for yaml I get an empty ev_list.
I don’t understand why it works in action- and template-editor, but not in yaml?
Here is my code again:
calendar:
- platform: caldav
username: !secret icloud_user
password: !secret icloud_password
url: https://caldav.icloud.com
calendars:
- Spielplan_U17_24-25
- Spielplan_Damen_24-25
- Spielplan_U20_24-25
template:
- trigger:
- platform: time_pattern
minutes: /1
action:
- service: calendar.get_events
data:
duration:
hours: 216 # 9 Tage
target:
entity_id:
- calendar.spielplan_u17_24_25
- calendar.spielplan_damen_24_25
- calendar.spielplan_u20_24_25
response_variable: agenda
- variables:
ev_list: |
{% set ns = namespace(cal_events=[]) %}
{%- for key, value in agenda.items() %}
{%- for event in value.events %}
{%- set ns.cal_events = ns.cal_events + [event] %}
{%- endfor %}
{%- endfor %}
{{ ns.cal_events | sort(attribute='start') | list }}
- sensor:
- name: Naechste_Spiele
unique_id: "naechste_spiele"
state: "{{ ev_list | count }}"
attributes:
list: "{{ ev_list }}"
test: "0.01"
icon: mdi:calendar
this should be
sensor:
- name: Naechste_Spiele
unique_id: "naechste_spiele"
state: "{{ ev_list | count }}"
attributes:
list: "{{ ev_list }}"
test: "0.01"
icon: mdi:calendar
Thank you for your help and time.
That damnd little dash!
Hello
Since October my template has stopped working, and the data is not updated
templates.yaml:
- trigger:
- platform: time_pattern
seconds: "/5"
action:
- service: calendar.get_events
data:
duration:
hours: 2160
minutes: 0
seconds: 0
start_date_time: "{{ today_at() }}"
target:
entity_id:
- calendar.booking
- calendar.airbnb
response_variable: calendars
- variables:
ev_list: |
{% set ns = namespace(cal_events=[]) %}
{%- for key, value in calendars.items() | unique(attribute='start') %}
{%- for event in value.events %}
{%- set ns.cal_events = ns.cal_events + [event] %}
{%- endfor %}
{%- endfor %}
{{ ns.cal_events | sort(attribute='start') | list }}
in_1_unix: |
{% set dif = 16*3600 %}
{{ as_timestamp(ev_list[0].start) + dif | float }}
out_1_unix: |
{% set dif = 11*3600 %}
{{ as_timestamp(ev_list[0].end) + dif | float }}
in_2_unix: |
{% set dif = 16*3600 %}
{{ as_timestamp(ev_list[1].start) + dif | float }}
out_2_unix: |
{% set dif = 11*3600 %}
{{ as_timestamp(ev_list[1].end) + dif | float }}
actual: |
{% if as_timestamp(now()) >= in_1_unix and as_timestamp(now()) <= out_1_unix %}
{{ 1 }}
{% else %}
{{ 0 }}
{% endif %}
sensor:
- name: reservas
unique_id: reservas
state: |
{% if actual == 1 %}
{{ "reserva_actual" }}
{% else %}
{{ "no_reserva_actual" }}
{% endif %}
attributes:
scheduled_events: "{{ ev_list }}"
in_1_unix: |
{{in_1_unix}}
in_1: |
{% from 'formatter.jinja' import format_date %}
{{format_date(in_1_unix)}}
out_1: |
{% from 'formatter.jinja' import format_date %}
{{format_date(out_1_unix)}}
in_2: |
{% from 'formatter.jinja' import format_date %}
{{format_date(in_2_unix)}}
out_2: |
{% from 'formatter.jinja' import format_date %}
{{format_date(out_2_unix)}}
icon: mdi:calendar'
custom_templates/formatter.jinja:
{% macro format_date(date_unix) %}
{% set weekdays = ['Lunes','Martes','Miércoles','Jueves','Viernes','Sábado','Domingo'] %}
{% set months = ["enero", "febrero","marzo","abril","mayo","junio","julio","agosto","septiembre","octubre","noviembre","diciembre"] %}
{% set dt = date_unix | timestamp_custom("%Y-%m-%d %H:%M:%S") | as_datetime %}
{{weekdays[dt.weekday()] }}, {{ dt.day }} de {{ months[dt.month-1] }} {{ dt.year }}, {{ dt.hour }}:{{ dt.minute }}{{ dt.second }}
{% endmacro %}
calendar.booking entity attributes:
message: CLOSED - Not available
all_day: true
start_time: 2024-11-14 00:00:00
end_time: 2024-11-18 00:00:00
location:
description:
offset_reached: false
friendly_name: booking
calendar.airbnb entity attributes:
message: Reserved
all_day: true
start_time: 2024-12-06 00:00:00
end_time: 2024-12-09 00:00:00
location:
description: Reservation URL: https://www.airbnb.com/hosting/reservations/details/....
Phone Number (Last 4 Digits): xxx
offset_reached: false
friendly_name: airbnb
I get sensor.reservas entity old attributes. How is it possible? I’ve reload all YAML configuration, and I still get the old data:
scheduled_events:
- start: '2024-10-31'
end: '2024-11-03'
summary: CLOSED - Not available
- start: '2024-11-14'
end: '2024-11-18'
summary: CLOSED - Not available
in_1_unix: 1730386800
in_1: Jueves, 31 de octubre 2024, 16:00
out_1: Domingo, 3 de noviembre 2024, 11:00
in_2: Jueves, 14 de noviembre 2024, 16:00
out_2: Lunes, 18 de noviembre 2024, 11:00
icon: mdi:calendar'
friendly_name: reservas
Check your logs for errors. The only thing that stands out for me is that you’re always assuming there will be at least 2 events. And you didn’t build in any safety when there is less than 2 events. Errors in your logs would show this.
Yes, I have to review my code. Thanks