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