For loop through sensors in a 'platform' - possible?

Is it possible to do something along the lines of:

{% for sensor in platform['london_underground'] %}
  {{ sensor.entity_id }}
{% endfor %}

The problem I have is that I need to know how many lines do not have a state of ‘Good Service’ but the sensors created by this platform have no way of isolating them (e.g. they do not have any common pre or suffix in their entity_id)

Yes I could check each one individually but there are 14 of them so it’d be nice to do it in a loop.

Thanks, but I really the count of lines which are not ‘Good Service’.

My wall panel tablet displays a list but only has room for six so if it is more than six I need to display something different.

I’m very grateful (as always) but I must defend myself here…

Original post:

Second post:

I think they say the same :wink: (except the missing word ‘need’ in the first post.)

But as I said, thanks again, I can’t try it out now but that looks like the perfect answer.

Rather than debate the differences between your two posts, I’ve chosen to retract my responses. This is for the best; they clearly missed the mark given that you felt the need to ‘defend’ yourself.

Do the sensors have any unique attributes?

@123 Hey, you didn’t need to do that. I thought I made it obvious I was grateful for the solution.
I just didn’t understand why you thought I’d changed the question.

I’m sorry if I offended you. It was certainly not meant.

@petro no, nothing unique… I actually think they are particularly badly named sensors but I think it is too late to expect that to change.

They are all sensor.LINE_NAME
For example

Seems like you do, attribution. This should get all entity_id’s that are from london underground that do not have ‘Good Service’ as a state.

{{ states.sensor | selectattr('attributes.attribution','eq','Powered by TfL Open Data') | selectattr('state','!=','Good Service') | map(attribute='entity_id') | join(', ') }}
1 Like

Sorry to revive this after almost three years but I am revisiting it…

Why does this not work in the template dev tools but it does in ‘real’ HA?


The whole sensor code:

  - binary_sensor:
      - name: Too Many Disrupted Underground Lines
        unique_id: too_many_disrupted_underground_lines
        state: >
          {{ states.sensor | selectattr('attributes.attribution', 'eq', 'Powered by TfL Open Data')
                           | selectattr('state', '!=', 'Good Service')
                           | list
                           | count > 5 }}
        attributes:
          count: >
            {{ states.sensor | selectattr('attributes.attribution', 'eq', 'Powered by TfL Open Data')
                             | selectattr('state', '!=', 'Good Service')
                             | list
                             | count }}
          lines: >
            {{ states.sensor | selectattr('attributes.attribution', 'eq', 'Powered by TfL Open Data')
                             | selectattr('state', '!=', 'Good Service')
                             | map(attribute = 'object_id')
                             | list
                             | join(', ') }}

It’s iterating through all sensors, you have to check to see if it exists before accessing it. In the past, this was silently ignored, now it’s raised as an error.

          {{ states.sensor | selectattr('attributes.attribution', 'defined')
                           | selectattr('attributes.attribution', 'eq', 'Powered by TfL Open Data')
                           | selectattr('state', '!=', 'Good Service')
                           | list
                           | count > 5 }}
1 Like

That was quick.
Even by your standards.

Thanks!!