Store response varibale in helper

Hi HA Community

I have a similiar question like this: Response variable

Is it possible to store the response in a helper variable?

My automation:

alias: Test
description: ""
trigger: []
condition: []
action:
  - service: calendar.list_events
    data:
      start_date_time: "2023-08-26 00:00:00"
      end_date_time: "2023-08-28 00:00:00"
    target:
      entity_id: calendar.kalender_marc
    response_variable: calendarItems
  - service: input_text.set_value
    data:
      value: "{{ calendarItems }}"
    target:
      entity_id: input_text.calendarData
mode: single

If i try it with the above automation, the variable “calendarData” is always empty.

Thanks and best regards
Buhli

You don’t have a variable calendarData in the automation you posted above. Did you mean calendarItems?

Do you have any calendar events on the 26th or 27th of August?

Hi tom_I

Thanks for your fast reply

I put the calendarData varibale here unter the target:

  - service: input_text.set_value
    data:
      value: "{{ calendarItems }}"
    target:
      entity_id: input_text.[u]calendarData[/u]

calenarItems is the response varibale from the automation

and calendarData is an input_text helper:

Yes, at this days I have few events.

Thanks and best regards
Buhli

Ok, that’s your problem. calendarData is not a variable it is an entity. Entity id’s are all lower case letters. So it should be:

  - service: input_text.set_value
    data:
      value: "{{ calendarItems }}"
    target:
      entity_id: input_text.calendardata

Thank you very much for your reply. I have corrected it, but unfortunately it is still empty.

Automation:

alias: Test
description: ""
trigger: []
condition: []
action:
  - service: calendar.list_events
    data:
      start_date_time: "2023-08-26 00:00:00"
      end_date_time: "2023-08-28 00:00:00"
    target:
      entity_id: calendar.kalender_marc
    response_variable: calendarItems
  - service: input_text.set_value
    data:
      value: "{{ calendarItems }}"
    target:
      entity_id: input_text.calendardata
mode: single

Entity:

Is this perhaps not the right way to assign the variable?

value: "{{ calendarItems }}"

Or is the problem maybe the max length of the helper?

image

I have to use an other type of helper?

Thanks and best regards
Buhli

It could very well be the max length. Check your log for errors regarding this. The maximum you can set this to is 255.

Yes it is the max length. I have tested it with a smaller range (one event ) and there it worked

I tested it with the entity, but there i see only the events from today.

My goal is to show the next three days on a lovelace card. I know there is already a calendar card. But I don’tt like this calendar.

Or Is there another way how I can archiev this?

Thanks and best regards
Buhli

You can store the data as an attribute of a trigger-based template sensor which will maintain the data structure and allow more than 255 char.

What type of card are you planning to use?

Example

thanks for your posts.

@123 Thank you very much for your tutorial. It works. Now I have all the appointments in this sensor. Can you maybe tell me, how I can now display this informations on a card? If I just select the entity, I only see count of events.

Thanks and best regards
Buhli

It depends on the card.

I suggest using the Markdown Card. Look at the end of the linked example (in my previous post) and you will find a template that can be used in a Markdown card.

@123, HI Taras

Thank you very much, it works :slight_smile:

Is it also possible that I can store from two calendars the events in one response variable?

alias: Test Calendar
description: ""
trigger:
  - platform: time_pattern
    minutes: "30"
condition: []
action:
  - service: calendar.list_events
    data:
      duration:
        hours: 72
        minutes: 0
        seconds: 0
    target:
      entity_id:
        - calendar.kalender_marc
    response_variable: scheduled_events
  - service: calendar.list_events
    data:
      duration:
        hours: 72
        minutes: 0
        seconds: 0
    target:
      entity_id:
        - calendar.marc_office
    response_variable: scheduled_events
  - event: marc_scheduled_events
    event_data:
      scheduled_events: "{{ scheduled_events }}"
mode: single

I have tried both calendards in one action under the entity_id, but he did not accept that.

Thanks and best regards
Buhli

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

@123 Hi Taras,

yes I wil do.

I want to add another calendar. I have created a second automation and adjusted the yaml file accordingly.

Now I have the problem, one sensor has events and the other not.

If I run automation 2: Sensor 2 has events and sensor 1 not

If I run automation 1: Sensor 1 has events and sensor 2 not


My second automation:

alias: Test Calendar Office
description: ""
trigger:
  - platform: time_pattern
    minutes: "30"
condition: []
action:
  - service: calendar.list_events
    data:
      duration:
        hours: 72
        minutes: 0
        seconds: 0
    response_variable: office_scheduled_events
    target:
      entity_id: calendar.office
  - event: office_scheduled_events
    event_data:
      office_scheduled_events: "{{ office_scheduled_events }}"
mode: single

My yaml:

template:
  - trigger:
      - platform: event
        event_type: 
        - marc_scheduled_events
        - office_scheduled_events

    sensor:
      - name: Marc Scheduled Events
        unique_id: marc_scheduled_events
        state: "{{ trigger.event.data.scheduled_events.events | count() }}"
        attributes:
          scheduled_events: "{{ trigger.event.data.scheduled_events.events }}"
        icon: mdi:calendar
        
      - name: Office Scheduled Events
        unique_id: office_scheduled_events
        state: "{{ trigger.event.data.office_scheduled_events.events | count() }}"
        attributes:
          office_scheduled_events: "{{ trigger.event.data.office_scheduled_events.events }}"
        icon: mdi:calendarevents: "{{ trigger.event.data.office_scheduled_events.events }}"
        icon: mdi:calendar
        

Can you maybe help here me again?

Thanks and best regards
Buhli

Your two Trigger-based Template Sensors use one Event Trigger that detects two custom events.

Create a separate Event Trigger for each Trigger-based Template Sensor. Each Event Trigger should detect just one custom event.

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

  - trigger:
      - platform: event
        event_type: 
        - office_scheduled_events
    sensor:
      - name: Office Scheduled Events
        unique_id: office_scheduled_events
        state: "{{ trigger.event.data.office_scheduled_events.events | count() }}"
        attributes:
          office_scheduled_events: "{{ trigger.event.data.office_scheduled_events.events }}"
        icon: mdi:calendar

@123 now its works, thank you very much :slight_smile:

Honestly, these events, triggers, etc. still confuse me a bit right now.

in the automation under the event_data I have office_scheduled_events (on the left site of the colon).

event_data:
  office_scheduled_events: "{{ office_scheduled_events }}"

In the yaml file this office_scheduled_events also appears in the following lines:

unique_id: office_scheduled_events
state: "{{ trigger.event.data.office_scheduled_events.events | count() }}"
attributes:
  office_scheduled_events: "{{ trigger.event.data.office_scheduled_events.events }}"
  icon: mdi:calendarevents: "{{ trigger.event.data.office_scheduled_events.events }}"

Does it have to be like that? Are they linked? Or is that just a coincidence?

Thanks and best regards
Buhli

1 Like

In my original example, it was my choice to use the same name (attribute and response variable). They can be different; I prefer to keep them the same.

@123 Hi Taras, thank you very much.

I hope this is my last question - sorry

For my understanding, green and yellow are linked? is that correct?

And here the same? Green and yellow belongs together. But where does the purple part comes from?
image

Sorry for the stupid questions

Thanks and best regards
Buhi

I suggest you review the following subjects:

Event Trigger

Event Trigger - Properties

Hi Taras

Thank you very much for your support.

Best regards
Buhli

1 Like