Solved: Loop based on entity ending in a number

Ok first real post… Wondering is this possible.

in the system we have entities with the real name of
sensor.alertsensor_alert_1
sensor.alertsensor_alert_2
sensor.alertsensor_alert_3

What I want to do is set the # part of the sensor with a for loop.

{% set n_alerts = states('sensor.alertsensor') | int(0)  %}
  {% for alert_n in range(n_alerts) %}
    Alert {{ alert_n + 1 }}
    {% if (states('sensor.alertsensor_alert_' ~ alert_n ) == 'on') and ((as_timestamp(now()) - as_timestamp(state_attr('sensor.alertsensor_alert_ ~ alert_n', 'alert_effective'))| float) > 3600) %}
     {{ state_attr('sensor.alertsensor_alert_'~alert_n , 'display_title') }}
    {% else %}
     Alerts
    {% endif %}
  {% endfor %}

I have tried

{% set n_alerts = states('sensor.alertsensor') | int(0)  %}
  {% for alert_n in range(n_alerts) %}
    Alert {{ alert_n + 1 }}
    {% if (states('sensor.alertsensor_alert_' [alert_n] ) == 'on') and ((as_timestamp(now()) - as_timestamp(state_attr('sensor.alertsensor_alert_ [alert_n]', 'alert_effective'))| float) > 3600) %}
     {{ state_attr('sensor.alertsensor_alert_'[alert_n] , 'display_title') }}
    {% else %}
     Alerts
    {% endif %}
  {% endfor %}

Normally it world look like so.

 {% if (states('sensor.alertsensor_alert_1' ) == 'on') and ((as_timestamp(now()) - as_timestamp(state_attr('sensor.alertsensor_alert_1', 'alert_effective'))| float) > 3600) %}
     {{ state_attr('sensor.alertsensor_alert_1' , 'display_title') }}
    {% else %}
     Alerts
    {% endif %}

I think I need some other kind of set =

Just not sure

Thanks Taras for responding. I will admit right away I don’t know to much about python, jinja or coding.

Template Sensor

to use expand don’t you have to group all the sensors?

More detail might be helpful.
There is one main sensor that counts the alerts 1 - 5 as they come in
alert 1, alert 2 and so on called sensor.alertsensor. All it does is count. Its like a feed.
Then alert 1 is formatted a attributed by a large template sensor called
sensor.alertsensor_alert_1 and the data is stored there and is unique.
if a 2nd alert comes in it goes through the exact same process but in a second template sensor
sensor.alertsensor_alert_2 and the data is stored there and is unique.
and so on all the way to 5.

The defined large template sensors all have a real # at the end assuming because the data is unique.
The feed alerts are created by a large templates sensors that is repeated 5 times
The 5 large templates are literally the same code except is # (1,2,3,4,5) id referenced through out the entire template.

For the sake of less code I wanted to count the alerts from sensor.alertsensor then store its number in variable and pass it to a normalized version of sensor.alertsensor_[alert_#] and still store it correctly in the sensor.alertsensor_alert_1,2,3,4,5 as needed.

The other part is the the alert data itself is unique so it might be best to just keep it the way it is.

I just was not sure of how best to do this or if it could be done.

I could just be over thinking it.

I figured out it was best to just keep it the same.