No, I ultimately want to process the response data within the blueprint (YAML) to extract the relevant event information. Conversion to JSON ia an interesting way to go, but is there a way of doing that within the blueprint?
The documentation only gives two examples, and the first just outputs the response data unprocessed.
The second example has
{% for event in agenda["calendar.school_calendar"]["events"] %}
{{ event.start}}: {{ event.summary }}<br>
{% endfor %}
… which implies that the events can be extracted without recourse to JSON conversion, as does your template sensor example. I will use it within an automation blueprint, but should be able to use code similar to that of your most_current
variable.
I am not trying to do anything extraordinary! I just need a leg-up because my template text string manipulation skills are primitive. I therefore need to be able to test code first in the template editor, starting with my example of response data.
So I tried this in the template editor
{% set cal_events = "
calendar.zzz_test_calendar:
events:
- start: '2024-02-22T18:15:00+00:00'
end: '2024-02-22T23:15:00+00:00'
summary: 'Overlapping event 1 '
description: 'Temp #21#'
- start: '2024-02-22T18:30:00+00:00'
end: '2024-02-22T18:45:00+00:00'
summary: 'Overlapping event 2 '
description: 'temp #22#'"
%}
{%- set fms = namespace(event=[]) %}
{%- for key, value in cal_events.items() %}
{%- for event in value.events %}
{%- set fms.event = (fms.event + [event]) %}
{%- endfor %}
{%- endfor %}
{% set x = (fms.event + [{'summary': 'now','start': now().isoformat()}]) | sort(attribute='start') %}
{% set ns = namespace(ind='') %}
{% for ev in x %}
{% if ev.get('summary') == 'now' %}
{% set ns.ind = loop.index0 %}
{% else %}
{% continue %}
{% endif %}
{% endfor %}
{{ x[ns.ind + (-1 if ns.ind > 0 else 1)] }}
The output is
‘str object’ has no attribute ‘items’
I get a similar response if I try to access the calendar attributes, so I guess copying and pasting the sample text from an automation trace has somehow altered it? What should it look like to simulate response data?
If I use your JSON as the starting point, then I do get a sensible answer.
{% set cal_events =
{
"calendar.zzz_test_calendar": {
"events": [
{
"start": "2024-02-22T18:15:00+00:00",
"end": "2024-02-22T23:15:00+00:00",
"summary": "Overlapping event 1 ",
"description": "Temp #21#"
},
{
"start": "2024-02-22T18:30:00+00:00",
"end": "2024-02-22T18:45:00+00:00",
"summary": "Overlapping event 2 ",
"description": "temp #22#"
}
]
}
}
%}
{%- set fms = namespace(event=[]) %}
{%- for key, value in cal_events.items() %}
{%- for event in value.events %}
{%- set fms.event = (fms.event + [event]) %}
{%- endfor %}
{%- endfor %}
{% set x = (fms.event + [{'summary': 'now','start': now().isoformat()}]) | sort(attribute='start') %}
{% set ns = namespace(ind='') %}
{% for ev in x %}
{% if ev.get('summary') == 'now' %}
{% set ns.ind = loop.index0 %}
{% else %}
{% continue %}
{% endif %}
{% endfor %}
{{ x[ns.ind + (-1 if ns.ind > 0 else 1)] }}
Output:
Result type: dict
{
"start": "2024-02-22T18:30:00+00:00",
"end": "2024-02-22T18:45:00+00:00",
"summary": "Overlapping event 2 ",
"description": "temp #22#"
}
So is the response data actually in JSON format, and it is the trace program that is converting it to YAML??? If the answer to that question is yes, then I am on the right track and just need a primer on extracting the attributes from a dict.