Input_text.set_value data_template not working anymore

I had an automation that filled in the events for today in an input_text entity so I could show the events inside a markdown in a readable fashion. This always worked, but somehow the events are now empty.

When I try this out in the devtool it is hard to capture the errors as you cannot both do get_events, put it in a variable and run the set_value service, but the problem seems to be inside the set_value because that one gives errors in the devtools about the data…

the part that is run is the following:

  - service: calendar.get_events
    data:
      duration:
        hours: 13
        minutes: 0
        seconds: 0
    response_variable: familiecal
    target:
      entity_id:
        - calendar.home_assistant_all_day_excluded
  - service: input_text.set_value
    data_template:
      value: >-
        {% for event in familiecal.events %}<ha-icon
        icon="mdi:calendar-heart-outline"></ha-icon> {{as_timestamp(event.start,
        default=0) | timestamp_custom('%d/%m %H:%M') }} {{
        event.summary}}{{'<br>'}}  {% endfor %}
    target:
      entity_id: input_text.familielist

If I test the set_value part in the devtools it results in the message that the syntax is wrong:

service: input_text.set_value
target:
  entity_id:
    - input_text.familielist
  device_id: []
  area_id: []
data_template:   
  value: >-     
    {% for event in familiecal.events %}
      <ha-icon icon="mdi:calendar-heart-outline"></ha-icon>
      {{as_timestamp(event.start, default=0) | timestamp_custom('%d/%m %H:%M') }}
      {{event.summary}}{{'<br>'}}  
    {% endfor %} 

Deze service vereist een veld value, die moet worden aangegeven onder data:
(Loosely translated to: This service requires a field value, this must be given under data:)

I think the problem resides inside the fact that I changed the list_events to get_events which supports multiple calendars and will always include the calendar listed inside the output. Can somebody point me to how I can change the set_value to take this into account?

The output of get_events:

calendar.home_assistant_all_day_excluded:
  events:
    - start: "2023-12-08T12:00:00+01:00"
      end: "2023-12-08T18:00:00+01:00"
      summary: Ziggo monteur komt langs
    - start: "2023-12-08T16:30:00+01:00"
      end: "2023-12-08T17:15:00+01:00"
      summary: "Jaylah: zwemles bij Rudy 16:30-17:15"
      location: Zwembad

vs the output of list_events:

events:
  - start: "2023-12-08T12:00:00+01:00"
    end: "2023-12-08T18:00:00+01:00"
    summary: Ziggo monteur komt langs
  - start: "2023-12-08T16:30:00+01:00"
    end: "2023-12-08T17:15:00+01:00"
    summary: "Jaylah: zwemles bij Rudy 16:30-17:15"
    location: Zwembad

data_template: is obsolete — use data: instead.

Example in the docs:

yeah oke, but then it still fails to save something due to the new way of listing calendar events which begins with the calendar name even when you only select 1 calendar, so it still doesn’t see the familiecal.events as there is a part in front of it

Yeah i know, that is why i have tried to split up the automation in hours so the output is smaller :slight_smile: thx for this code, will try it out in a bit :slight_smile:

Ooh, I missed that change! @FGOD1983 — if you go down this route, make sure you’re running 2023.9 or later.

That would be even better! At the time I didn’t found a solution to the limit of the characters or a better way to get what I want in the markdown, if this solves both then that would be fantastic!

As for the solution to the set_value, that did indeed fix the problem and it now fills the input_text again with the info needed:

<ha-icon icon="mdi:calendar-heart-outline"></ha-icon> 08/12 12:00 Ziggo monteur komt langs<br> <ha-icon icon="mdi:calendar-heart-outline"></ha-icon> 08/12 16:30 Jaylah: zwemles bij Rudy 16:30-17:15<br>

I am trying this now but noticed the data duration set to 13 hours, this was mainly my example as this should be everything from now to midnight, is there a way I can do that in that trigger sensor template too?

This is the approach I typically use

  - trigger:
      - platform: time_pattern
        hours: /1
    action:
      - variables:
          target: calendar.home_assistant_all_day_excluded
      - service: calendar.list_events
         target:
          entity_id: "{{ target }}"
        data:
          start_date_time: "{{ today_at() }}"
        response_variable: raw
      - variables:
          familiecal: "{{ raw[target] }}"
    sensor:
      - name: Family Events
        unique_id: family_events
        state: "{{ familiecal.events | count }}"
        attributes:
          scheduled_events: "{{ familiecal.events }}"
        icon: mdi:calendar
1 Like

yeah noticed the mistake too and already corrected it in my configuration :slight_smile: but then the question is how you can format the time left to hours without displaying the h of hours :sweat_smile:

This would display the time left in hours but also outputs the h which the duration cannot handle in the hours part:

{{ (today_at() + timedelta(days=1) - now()).total_seconds() | timestamp_custom('%-Hh', false)}}

that would only do the data until midnight?

If you want now to midnight, then use…

  - trigger:
      - platform: time_pattern
        hours: /1
    action:
      - variables:
          target: calendar.home_assistant_all_day_excluded
      - service: calendar.list_events
         target:
          entity_id: "{{ target }}"
        data:
          start_date_time: "{{ now() }}"
          end_date_time: "{{ today_at() + timedelta(days=1) }}"
        response_variable: raw
      - variables:
          familiecal: "{{ raw[target] }}"
    sensor:
      - name: Family Events
        unique_id: family_events
        state: "{{ familiecal.events | count }}"
        attributes:
          scheduled_events: "{{ familiecal.events }}"
        icon: mdi:calendar
1 Like

The main idea is to display the coming events for the rest of the day until midnight with a refresh every hour (which was why I used the automation to gather the info every hour with an hour less in the duration to setup the input_text)

Thx guys, this greatly helped both my issue with the new get_events and the issues i had with the character limits of the input_text:grin:

The initial fix for the problem this post was about would be to ditch the data_template and put the calendar name in between [] for as stated by @123

familiecal['calendar.home_assistant_all_day_excluded'].events

But as stated by @123 and @petro here, it would be easier to use a trigger based sensor and use that sensor in the markdown to show the appointments as that would remove the issues I had earlier with the character limits of text_input and the need for an over complicated automation which was insane big with all the hourly triggers and exceptions.

This works like a charm :slight_smile:

As for the code now, this is the markdown:

{% if (states('sensor.family_events') | float(0) == 0) and (states('sensor.willem_events') | float(0) == 0) %}
  <header><b><u>Calendar Appointments for Today:</b></u></header>No Appointments
{% elif (states('sensor.family_events') | float(0) > 0) and (states('sensor.willem_events') | float(0) == 0) %}
  <font color = "#039be5" > <ha-alert alert-type="info" title="Calendar Appointments for Today:">
    <b><u>Family Agenda:</b></u> <br>
    {% for event in state_attr('sensor.family_events', 'scheduled_events') %}<ha-icon icon="mdi:calendar-heart-outline"></ha-icon> {{as_timestamp(event.start, default=0) | timestamp_custom('%d/%m %H:%M') }} {{event.summary}}{{'<br>'}}{% endfor %}
  </ha-alert>
{% elif (states('sensor.family_events') | float(0) == 0) and (states('sensor.willem_events') | float(0) > 0) %}
  <font color = "#039be5" > <ha-alert alert-type="info" title="Calendar Appointments for Today:">
    <b><u>Personal Agenda:</b></u> <br>
    {% for event in state_attr('sensor.willem_events', 'scheduled_events') %}<ha-icon icon="mdi:calendar-account-outline"></ha-icon> {{as_timestamp(event.start, default=0) | timestamp_custom('%d/%m %H:%M') }} {{event.summary}}{{'<br>'}}{% endfor %}
  </ha-alert>
{% elif (states('sensor.family_events') | float(0) > 0) and (states('sensor.willem_events') | float(0) > 0) %}
  <font color = "#039be5" > <ha-alert alert-type="info" title="Calendar Appointments for Today:">
    <b><u>Personal Agenda:</b></u> <br>
    {% for event in state_attr('sensor.willem_events', 'scheduled_events') %}<ha-icon icon="mdi:calendar-account-outline"></ha-icon> {{as_timestamp(event.start, default=0) | timestamp_custom('%d/%m %H:%M') }} {{event.summary}}{{'<br>'}}{% endfor %}
    <b><u>Family Agenda:</b></u> <br>
    {% for event in state_attr('sensor.family_events', 'scheduled_events') %}<ha-icon icon="mdi:calendar-heart-outline"></ha-icon> {{as_timestamp(event.start, default=0) | timestamp_custom('%d/%m %H:%M') }} {{event.summary}}{{'<br>'}}{% endfor %}
  </ha-alert>
{% endif %}

And this is the full config now in configuration.yaml:

template:
- trigger:
  - platform: time_pattern
    hours: /1
  action:
    - variables:
        target: calendar.home_assistant_all_day_excluded
    - service: calendar.get_events
      target:
        entity_id: "{{ target }}"
      data:
        start_date_time: "{{ now() }}"
        end_date_time: "{{ today_at() + timedelta(days=1) }}"
      response_variable: raw
    - variables:
        familiecal: "{{ raw[target] }}"
  sensor:
    - name: Family Events
      unique_id: family_events
      state: "{{ familiecal.events | count }}"
      attributes:
        scheduled_events: "{{ familiecal.events }}"
      icon: mdi:calendar
- trigger:
  - platform: time_pattern
    hours: /1
  action:
    - variables:
        target: calendar.personal_calendar
    - service: calendar.get_events
      target:
        entity_id: "{{ target }}"
      data:
        start_date_time: "{{ now() }}"
        end_date_time: "{{ today_at() + timedelta(days=1) }}"
      response_variable: raw
    - variables:
        WillemCal: "{{ raw[target] }}"
  sensor:
    - name: Willem Events
      unique_id: willem_events
      state: "{{ WillemCal.events | count }}"
      attributes:
        scheduled_events: "{{ WillemCal.events }}"
      icon: mdi:calendar
1 Like