Loop through entities of the same type to retrieve same attribute from all

Hi All

Brand new to HA and have some coding experience but really only in things like Javascript etc… VERY new to YAML…

I am trying to find a way to loop through a set of entities and retrieve the “message” attribute from them all… I have a small query that will find all the calendar entitie_id that I want. Each entity contains a message attribute. I was hoping I could somehow store the results of the first query in an array and use it to loop through each one and retrieve the attribute data… The issue is the result set is outputted as a string so if I try and loop the new variable it will just iterate each letter in a new line.

Example query to get all the calendar entities

{% set calendars = states.calendar | rejectattr('attributes.friendly_name', 'search', '[email protected]') | map(attribute='entity_id')| unique | join(", ") %}

Sudo Logic:-

  1. Create variable which is array of entity IDs from the first search.
  2. create loop which will find the attribute from each entity and uses the array to loop through each entity
  3. Use the output from each attribute and display on a card.

When I started this I was more ambitious, I wanted show the calendar events based on the user that was logged in. That proved very complex as there doesn’t seem to be a way to identify the person that is logged in so it makes it very hard to them look up the correct calendar and display that data. Instead I thought I would just pull all the calendar “message attributes” and show them all but even that is getting me stuck.

Thanks

Templates based on the state object of a calendar entity can be unreliable if you have overlapping or all-day events. You will be better off using the calendar.list_events service call.

In HA, states are always strings. Attributes can be other data types.

You can use a State-Switch custom card to modify the card shown based on the active/logged in user.

1 Like

Hi Didgeridrew! (PS love the name…)

Thanks so much for taking the time to reply. I will say this, I started this HA workas a means to push lots of my house IOT data into my Grafana and InfluxDB and play with lovely graphs but I have become totally hooked on HA as a program itself! It’s soo flexible but I guess that does mean it is quite complex…

Anyway… Thanks again for replying, I will spend some time to try and read through your suggestions…

The SS card looks good but looks like you have to set it manually. I was using a mushroom template card with

‘Hello {{user}}’ in the Primary Info and was hoping to use the secondary info to pull the next event from a certain users calendar. It seems to recognise ‘{{user}}’ in the card but not in the template.

Like this

image

The work around I have found is to share everything into a family calendar in Google (using the good integration) and then pull the data from one place. This just means everyone sees the same data rather then customised to their own calendar.

Thanks again for replying

That should be do-able… as long as you keep a consistent naming method for the template sensors.

image

type: custom:mushroom-template-card
primary: Hello, {{user}}
secondary: |-
  {% set ent = 'sensor.today_s_events_' + user %}
  {% if states(ent) == '0' %}
    No Events Left Today
  {% else %}
    {% set e = state_attr(ent, 'scheduled_events')%} 
    Next in your calendar you have: {{ e[0].summary}}- {{ (e[0].start|as_datetime).time()}}
  {% endif %}
icon: mdi:home
Today's Events Template sensor

You will need one of these for each person.

template:
  - trigger:
      - platform: calendar
        event: end
        entity_id: calendar.username
      - platform: homeassistant
        event: start
    action:
      - service: calendar.list_events
        data:
          start_date_time: "{{ now() }}"
          end_date_time: "{{ today_at('23:59:59')}}"
        target:
          entity_id:
            - calendar.username
        response_variable: agenda1
    sensor:
      - name: "Today's Events UserName"
        unique_id: custom_multical_todays_events
        state: |
          {{ (agenda1.events ) | sort(attribute='start') | list | count}}
        attributes:
          scheduled_events: |
            {{ (agenda1.events ) | sort(attribute='start') | list }}
        icon: mdi:calendar

Wow that is amazing!

Thanks for taking the time to write this. I am going to have a go and post the result back when I am done.

hi @Didgeridrew

Thanks again for taking the time to create this example, I have finally got around to trying to see if I can understand this but alas…

OK so here is what I think I need to do… Let me know if I am way off…

  1. create a helper “template sensor” with the name calendar.X for each user using your code?
  2. where your code shows calendar.username replace with the “google calendar sensor for each calendar of a user?”
  3. In the dashboard create a mushroom card using your code again (this time the first code section)

I am obviously going wrong somewhere… as I get the following

image

I can see agenda1 is the response variable at the top of the code but I can’t work out how the two bits of code (template and card) link together?

The recently added Template sensor Helper is only capable of very basic template sensors, it does not currently support one’s that use actions or attributes. To use those you set up the sensor in your configuration.yaml file.

You replace it with the your calendar entities. If a user has more than one calendar you will need to combine them:

template:
  - trigger:
      - platform: calendar
        event: end
        entity_id: 
          - calendar.keir 
      - platform: calendar
        event: end
        entity_id: 
          - calendar.keir_work
      - platform: homeassistant
        event: start
    action:
      - service: calendar.list_events
        data:
          start_date_time: "{{ now() }}"
          end_date_time: "{{ today_at('23:59:59')}}"
        target:
          entity_id:
            - calendar.keir
        response_variable: agenda1
      - service: calendar.list_events
        data:
          start_date_time: "{{ now() }}"
          end_date_time: "{{ today_at('23:59:59')}}"
        target:
          entity_id:
            - calendar.keir_work
        response_variable: agenda2
      - variables:
          all_events: >
            {{ agenda1.events + agenda2.events }}
          events: >
            {{ all_events }}
    sensor:
      - name: "Today's Events Keir"
        unique_id: keir_todays_events
        state: "{{ events | count}}"
        attributes:
          scheduled_events: "{{ events | sort(attribute='start') | list }}"
        icon: mdi:calendar

Any ideas why I might be getting some validation errors from the IDE? Looks correct from what I can see but I think there must be some issue as after a HA restart it doesn’t seem to have created the sensors.

My mistake, each calendar needs it’s own trigger. I have fixed it in my earlier post.

thank you! I will give it go and try and learn YAML a bit better/at all! :slight_smile: