Need help with a group of devices monitoring appliances

Hi,

I have flashed few Sonoff S31 with ESPHome and attached these to appliances in the house, these plugs can monitor voltage & current which can help establish if the appliance in running or not. Here is the syntax for the group:

appliances:
  name: Appliances Running
  all: false
  entities:
    - sensor.switch_01_current
    - sensor.switch_02_current
    - sensor.switch_03_current
    - sensor.switch_04_current

The Template section, I see following:

{{ expand('group.appliances')}}
The output contains:

[<template TemplateState(<state sensor.switch_01_current=0.03; state_class=measurement, unit_of_measurement=A, device_class=current, friendly_name=Switch-01 Current @ 2022-04-05T17:11:41.584347-07:00>)>, <template TemplateState(<state sensor.switch_02_current=0.00; state_class=measurement, unit_of_measurement=A, device_class=current, friendly_name=Washer Current @ 2022-04-05T13:18:19.886718-07:00>)>, <template TemplateState(<state sensor.switch_03_current=0.05; state_class=measurement, unit_of_measurement=A, device_class=current, friendly_name=Switch-03 Current @ 2022-04-05T19:12:43.382789-07:00>)>, <template TemplateState(<state sensor.switch_04_current=0.00; state_class=measurement, unit_of_measurement=A, device_class=current, friendly_name=Switch-04 Current @ 2022-04-05T16:00:02.349939-07:00>)>]

So the above output contains all the entities with current measurement that I need but since they all have a different name, I cannot write a for loop. My thought process was to create a group to which I can add more appliance later and create automation like if an appliance is running at peak load hours then send a text out and so on.

Is there a way to write this into a for loop so that a template can check for each entity in the group and then make a call depending on the logic?

Thanks.

Use a repeat block (check the script docs) and selectattr to select sensors with the attribute(s) that you require (in the Jinja2 docs).