I need help setting a variable

I have installed the snarky-snark HACS variable integration. I am trying to call the service calendar.get_events and then assign the resulting response to a variable. All of this is contained in a single script. The problem I’m having is that due to the complexity of that response I am getting an error. Can someone plese give me the format to use to do that assignment?

Here is a sample of the resulting response from the calendar.get_events call:

{% set action_response = {“calendar.house_party”:{“events”:},“calendar.sheryl_calendar”:{“events”:[{“start”:“2025-03-27T07:30:00-04:00”,“end”:“2025-03-27T08:30:00-04:00”,“summary”:“Crossfit”,“description”:" “},{“start”:“2025-03-27T13:00:00-04:00”,“end”:“2025-03-27T14:30:00-04:00”,“summary”:“Line Dance Union Township”,“description”:” “},{“start”:“2025-03-27T18:00:00-04:00”,“end”:“2025-03-27T19:00:00-04:00”,“summary”:“Line Dancing Lessons”,“description”:” ",“location”:“Little Miami Brewing Company”}]}} %}

I have tried a simple value: “{{ agenda_sheryl }}” that does not work I have tried a couple of value_templates with no success.

All help appreciated.

Thanks,
Mike

What are you using as the ID for the response variable?

agenda_sheryl

Okay, it would help us give you specific advise if you shared the actual action you have tried.

One possible issue is that states are always strings and are limited to 255 characters… your example output is nearly twice that when converted to a string.

You can store the values as data types other than strings with significantly larger limits by putting them in attributes instead of the state.

You are querying 2 calendars, the events for each can be accessed using the following:

{{ agenda_sheryl['calendar.house_party'].events }}

{{ agenda_sheryl['calendar.sheryl_calendar'].events }}

So one comment, I actually have a second script that queries 5 calendars. Ultimately I am trying to pass this data to a “briefing template” which is a Jinja template with a bunch of text intermingled with other data, such as weather, recommended clothing, daily Bible verse, etc. This is the only part I can’t get working.

That siad, here is what I have tried:

value: “{{ agenda_sheryl[0].summary if agenda_sheryl|count > 0 else ‘No events’ }}”

value: “{{ agenda_sheryl.calendar.sheryl_calendar.events }}”

value: >-
{% if agenda_sheryl and agenda_sheryl.calendar.sheryl_calendar and agenda_sheryl.calendar.sheryl_calendar.events %}
{{ agenda_sheryl.calendar.sheryl_calendar.events }}
{% else %}
# Assign an empty list if there’s no data
{% endif %}

value: >-
{% if agenda_sheryl is defined and agenda_sheryl.calendar is defined and agenda_sheryl.calendar.sheryl_calendar is defined and agenda_sheryl.calendar.sheryl_calendar.events is defined %}
{{ agenda_sheryl.calendar.sheryl_calendar.events }}
{% else %}

{% endif %}

Best practice would be to not set them to a “variable” entity, instead query the data when it is needed.

As shown in the examples in my previous post, you need to use the bracket notation to extract each calendar’s object from the response variable. If you let us know how you want the calendar data returned for use in your briefing, we can help with a more specific template.

If you are set on using a variable entity, you could do something like:

actions:
  - action: calendar.get_events
    ....
  - variables:
       combined_ev: |
         {% set ns = namespace(ev=[])%}
         {% for cals in agenda %}
           {% set ns.ev = ns.ev + agenda_sheryl[cals].events %}
         {% endfor %}
         {{ ns.ev }}
  - action: var.set
    data_template:
      entity_id: your_entity 
      value: "{{ combined_ev | count }}" 
      attributes:
          events_list: "{{ combined_events }}"

Well, since I have not yet been able to get the calendar data into the template yet, I don’t have that specific part of the tempate built. But the gist of it is that it is a “morning briefig” that plays first thing in the morning when my wife or I go into the kitchen to get our morning coffee. The templat eselects from a bunch of random “Good moning” statements. It then gives the current weather forecast, recommends clothing such as a coat or umbrella. Next will come calendar information, and then finnaly the daily Bible verse. The plan is to grow the template as I add more devices and integrations into Home Assistant.

That said, here is the general calendar intent. First of all I have 8 calendars, altogether. (There are reasons for keeping them separate, so I don’t want to combine them.) They are as follows:

        - calendar.anniversary
        - calendar.birthday
        - calendar.holidays_us
        - calendar.us_awareness_days
        - calendar.vacation

        - calendar.mike_calendar
        - calendar.sheryl_calendar
        - calendar.house_party
        - 

The top 5 calendars contain events that span the day (or multiple days in the case of vacations). The bottom 3 are items that have a specific start and end time. I would like the template to break them down by calendar, and state the following:

“Your personal calendar agenda for today includes the following:”
(a list, item by item of event name and start time) - or,
“You have no personal calendar events, today.”

“Your house party event for today is:”
(the event name and start time)- or,
“You have no house party events, today”

"Today is the anniversary of: "
husband and wife (that is how it is in the calendar, so simply the message attribute)
or no text if it is empty

“Today is the birthday of:”
(name of person listed)
or no text if it is empty

“Today is the US Holiday called:”
(holiday name)
or no text if it is empty

“Today is:”
(US Awareness day)
or no text if it is empty

“Today is your vacation:”
(name lsited for vaction event subject)
or no text if it is empty

If none of the 5 above has any data for the day, then it would select from the following random text:

{{ [
‘You have no special days today.’,
‘There are no special days occurring today.’,
‘There is nothing exciting going on today.’
] | random }}

I know this is fairly long, and I have some of the template built. Here is an example, keeping in mind that the variable name is wrong:

{% if mike_calendar_events | length > 0 %}

Your personal calendar agenda for today includes the following:


{% for event in mike_calendar_events %}
{{ event.start }}: {{ event.summary }}

{% endfor %}
{% else %}

Personal Calendar:


You have no personal calendar events.

{% endif %}
{% else %}
no calendar data available
{% endif %}

Let me know what else you would like to know.

Then you should use an attribute for each calendar.

triggers:
...
condition: []
actions:
  - action: calendar.get_events
    data:
      end_date_time: "{{ today_at('23:59') }}"
    target:
      entity_id:
        - calendar.anniversary
        - calendar.birthday
        - calendar.holidays_us
        - calendar.us_awareness_days
        - calendar.vacation
        - calendar.mike_calendar
        - calendar.sheryl_calendar
        - calendar.house_party
    response_variable: agenda
  - action: var.set
    data_template:
      entity_id: your_entity_id 
      value: "{{ now() }}" 
      attributes:
        anniversary: "{{ agenda['calendar.anniversary'].events }}"
        birthday: "{{ agenda['calendar.birthday'].events }}"
        holidays_us: "{{ agenda['calendar.holidays_us'].events }}"
        us_awareness_days: "{{ agenda['calendar.us_awareness_days'].events }}"
        vacation: "{{ agenda['calendar.vacation'].events }}"
        mike_calendar: "{{ agenda['calendar.mike_calendar'].events }}"
        sheryl_calendar: "{{ agenda['calendar.sheryl_calendar'].events }}"
        house_party: "{{ agenda['calendar.house_party'].events }}"

OK, I just added your code as an automation. I then ran it, and checked the traces. Here is what I got:

Executed: March 27, 2025 at 4:40:31 PM
Result:
params:
domain: calendar
service: get_events
service_data:
end_date_time: ‘2025-03-27 23:59:00-04:00’
entity_id:
- calendar.anniversary
- calendar.birthday
- calendar.holidays_us
- calendar.us_awareness_days
- calendar.vacation
- calendar.mike_calendar
- calendar.sheryl_calendar
- calendar.house_party
target:
entity_id:
- calendar.anniversary
- calendar.birthday
- calendar.holidays_us
- calendar.us_awareness_days
- calendar.vacation
- calendar.mike_calendar
- calendar.sheryl_calendar
- calendar.house_party

Executed: March 27, 2025 at 4:40:31 PM
Error: template value should be a string for dictionary value @ data[‘attributes’][‘anniversary’]
Result:
params:
domain: var
service: set
service_data:
entity_id: var.special
value: ‘2025-03-27 16:40:31.081802-04:00’
attributes:
anniversary:
birthday:
holidays_us:
us_awareness_days:
vacation:
mike_calendar:
sheryl_calendar:
- start: ‘2025-03-27T18:00:00-04:00’
end: ‘2025-03-27T19:00:00-04:00’
summary: Line Dancing Lessons
description: ’ ’
location: >-
Little Miami Brewing Company Event Center, 310 Mill St, Milford, OH
45150, USA
house_party:
target: {}
running_script: false

It’s been years since I used the “variables” custom integration…

Based on the error message it doesn’t look like it will accept an empty list. The integration’s docs show using a multi-line quote delimiter after attributes like attributes: >- so you could try that.

Otherwise you would need to do something to guard against the empty list for each calendar like:

...
anniversary: |
  {% set events = agenda['calendar.anniversary'].events %}
  {{ events if events|count > 0 else '' }}
...