How to get and parse HA sensor with nested (multiple values) state?

Hey all,

I’ve made a calendar sensor listing all the events of the next week. I’ve use this ref to create a sensor template.

Alright, now I’ve got a sensor that has the events nested in a attribute. How can I itterate / loop though it to render it on a display?

I’ve tried to make a text_sensor:

text_sensor: 
- platform: homeassistant
  name: Calendar next weeek
  id: calendar_next_week
  entity_id: sensor.calendar_next_week
  attribute: scheduled_events

I thought I could deserialize it in the lambda. But I guess I text sensors have a max amount of characters. Because the value just ends somewhere.

So, what would be the best way to import AND parse all this data? I would like to it.printf() the start, summery and location values of each event.

Can I use a home assist sensor for this? Or would I make a REST API call in the display lambda each minute?

You could use a for loop in a markdown card. Maybe something like

type: markdown
content: >
  ## Events

  {% for event in state_attr('sensor.calendar_next_week','scheduled_events') %}
    {{ event.start }} - {{ event.summary}} - {{ event.location }}
  {% endfor %}

If it’s possible for there to be no events, you could wrap the for loop in a check to confirm it’s not empty to prevent errors or to display some special message in that case.

  {% if state_attr('sensor.calendar_next_week','scheduled_events') != None %}
    {% for ... %}
    {% endfor %}
  {% else %}
    No events this week!
  {% endif %}

Hey @atlflyer , thanks. But sorry, maybe I wasn’t that clear. I need the lambda/C++ code in the display component of ESPHome.

I was thinking if something like this:

display:
  - platform: ...
    # ...
    lambda: |-
        // at this point calendar_next_week has to be an object/array 
        int counter=0;
        for( const auto& item : id(calendar_next_week).state) {
          it.printf(20, 20 + ( counter * 30), id(normal_font),  item['start'] );
          it.printf(60, 20 + ( counter * 30), id(normal_font),  item['summary'] );
          counter++;
        }

Thing is, I don’t know how to get the data as an object from sensor.calendar_next_week into the ESP32 board. So, text_sensor: has a character limit. And I don’t know if there is an other way to import the data.

Sorry about that. I use an RSS reader that lumps various forum categories together, and didn’t notice that detail of your question. Unfortunately, I don’t have any ideas for your actual problem :flushed:

1 Like

:slight_smile: No problem at all :slight_smile:

I’m sure someone has the answer

Hi @Timm ,
it seems I am looking for the same solution.

My configuraion.yaml looks like:

template:
  - trigger:
      - platform: time_pattern
        minutes: "/1"
    action:
      - service: calendar.get_events
        data:
          duration:
            hours: 24
            minutes: 0
            seconds: 0
          start_date_time: "{{ today_at() }}"
        target:
          entity_id: calendar.juergen_calender
        response_variable: raw_events
      - variables:
          scheduled_events: "{{ raw_events['calendar.juergen_calender'] }}"
    sensor:
      - name: Calendar Scheduled Events
        unique_id: calendar_scheduled_events
        state: "{{ scheduled_events.events | count() }}"
        attributes:
          scheduled_events: "{{ scheduled_events.events }}"
        icon: mdi:calendar'

In esphome I added a text_sensor

text_sensor:
# get the calendar data over to ESP
  - platform: homeassistant
    id: calendar_scheduled_events
    entity_id: sensor.calendar_scheduled_events
    on_value:
      then:
        - lambda: 'id(data_updated) = true;'
        - lambda: |-
            ESP_LOGI("main", "Scheduled Events: %s", id(calendar_scheduled_events).state.c_str());
            id(data_updated) = true;

For testing I have 6 events in this, and the logfile shows “6” :slight_smile:

Deep in my lambda display code I can see the “6” as well, but I have absolutely no clue on how to run thru this array and print the individual entries.

Did you find a way?

thanks
Juergen