Response variable

I am playing around with the new calendar.list_events service call. I have the below script and its running without issue. I am wondering how to view the contents of the response_variable “Rockies”. Ultimately I would like to email this on a weekly basis.

service: calendar.list_events
data:
 start_date_time: "2022-03-22T20:00:00.000Z"
 end_date_time: "2022-03-22T22:00:00.000Z"
target:
 entity_id: calendar.colorado_rockies_schedule
response_variable: Rockies

From this section of the Calendar documentation:

- service: calendar.list_events
  target:
    entity_id: calendar.school
  data:
    duration:
      hours: 24
  response_variable: agenda
- service: notify.gmail_com
  data:
    target: [email protected]
    title: Daily agenda for {{ now().date() }}
    message: >-
      Your agenda for today:
      <p>
      {% for event in agenda.events %}
      {{ event.start}}: {{ event.summary }}<br>
      {% endfor %}
      </p>

Don’t forget that you have to reference the response_variable in the same script that creates it.

1 Like

The issue I was having is that the date was set to the past. When I change the date to from today to the future it worked. Thanks for the help.

It is not working with new (beta) calendar.get_events call service.
Response of the service is nested inside name of calendar.
What is the correct path in the object now?

I don’t know.

I’m not testing the beta version and the beta documentation doesn’t yet contain a description of the new service call.

I suggest you wait until the documentation is available or ask its author. Here’s the Pull Request:

According to the beta release notes, the existing service call, calendar.list_events, will be deprecated and won’t be removed until version 2024.6.0.

1 Like

If you don’t know the name of the calendar, you can surround the for loop like so

{% for key, value in agenda.items() %}
  {% for event in value.events %}
    ...
  {% endfor %}
{% endfor %}

If you know the name, then it is easier

{% for event in agenda['calendar.school'].events %}
    ...
{% endfor %}
2 Likes

but how do you test this in the developer tools ? Can you call a service into a variable like you can w/ the template resource?

The following worked for me w/ list_events but switching to get_events returns a nested response that I am unsure how to test in something like developer tools… any help would be appreciated

template:
  - trigger:
      - platform: event
        event_type: household_events
    sensor:
      - name: Household events
        unique_id: household_events
        state: "{{ trigger.event.data.scheduled_events.events | count() }}"
        attributes:
          scheduled_events: "{{ trigger.event.data.scheduled_events }}"
        icon: mdi:calendar



  - trigger:
        - platform: time_pattern
          minutes: /1
    action:
      - service: calendar.list_events
        data:
          duration:
            hours: 120
            minutes: 0
            seconds: 0
          start_date_time: "{{ today_at() }}"
        target:
          entity_id: calendar.google_household
        response_variable: scheduled_events
    sensor:
      - name: Calendar Scheduled Events
        unique_id: calendar_scheduled_events
        state: "{{ scheduled_events.events | count() }}"
        attributes:
          scheduled_events: "{{ scheduled_events.events }}"
        icon: mdi:calendar'

  1. Call the service from the services tab in developer tools. Don’t use templates in the service caller, manually enter todays date for that field.

  2. Copy the contents of the response.

  3. Find an online yaml to JSON converter tool.

  4. set the json contents in a variable.

    {% set scheduled_events = ***PASTE JSON HERE**** %}
    
  5. Test your template below that.

    {% set scheduled_events = ***PASTE JSON HERE**** %}
    {{ scheduled_events.events | count }}
    

THANK YOU! I couldn’t figure it out after the beta change but that worked perfectly! My script is now back to working.

1 Like

Thank you I appreciate the answer ! Any plans to eliminate the yaml to json step in the template tool? just thinking it might nice to be able to call a service and assign the output to a variable within the tool… thoughts? crazy talk? :slight_smile:

1 Like

2023.12 added a copy button… but it’s yaml, so the steps remain the same.

1 Like

When I add this to output for a notification it works - if i use it for TTS it doesn’t work ? (with calendar.get_events)

  message: |-
    eingetragene Termine für heute:  
    {% for event in agenda['calendar.xxx'].events %}
      {{ event.summary }}
    {% endfor %}