Remote Receiver - using group for binary sensors

I am using Remote Receiver to receive signals from a custom universal remote.

For some automations, I need to trigger when any of buttons that relate to a specific activity are pressed. Rather than having to add up to 40 remote_receiver binary sensors in a single trigger, it is neater to have these related buttons in a group. eg:a group called TV Buttons:



ESPHOME code examples just of the top and bottom sensors in the group are below:

  - platform: remote_receiver
    name: "0 TV - TV"
    pioneer:
      rc_code_1: !secret universal1_0_tv
  - platform: remote_receiver
    name: "Guide Sleep TV - TV - Guide"
    pioneer:
      rc_code_1: !secret universal1_guidesleep_tv

This is fine for setting off the trigger when i push the “0 TV - TV” button (binary_sensor.irreceiver_0_tv_tv). However, I need the actual binary sensor that triggered it. In my example, I can see this recored as tha last action for the group:

I decided to capture the group name and the sensorname in trigger variables:

platform: state
entity_id:
  - binary_sensor.tv_buttons
from: "off"
to: "on"
id: TV Device
variables:
  group_trigger_name: "{{ trigger.entity_id }}"
  sensor_trigger_name: >-
    {{
    (expand(trigger.to_state.attributes.entity_id)|map(attribute='name')|sort(attribute='last_changed',
    reverse = true)|list)[0] }}

“group_trigger_name” is fine. However, the “sensor_trigger_name” is always being set to the last binary_sensor defined in the group i.e. irreceiver_guide_sleep_tv_tv_guide
This is despite that never pushed and no state change.

I have tried changing reverse to false and still the same. I have also added "| selectattr(‘state’, ‘eq’, ‘off’) " to get the specific last change (and tried on too even though I know the status tends to toggle so fast that the trigger cannot capture the “on” state in time - i initially just selected the binary sensor that was currently on_, without sorting, but it was only quick enough twice to capture it). It always returns irreceiver_guidesleep_tv_tv_guide

Not sure what is wrong with the sensor_trigger_name code.

Think I may have it working but need to test with some other buttons to be sure.

Looks like I had the expand definition wrong

        {{ ( expand( trigger.entity_id )
        | sort(reverse=true, attribute='last_changed')
        | map(attribute='name')
        | list )[0] }}

Update: this aspect is working now. However, even though individual binary sensors are logging as On and Off when the ir signal is sent, the group is not always responding to this. It can sometimes take 2, 3 or 4 button presses (all showing up as On/Off state changes in the single binary sensor) for the the group state to change in line with it. Clearly, the group is set up correctly as it does eventually respond in line with this button but I am wondering if the speed of On/Off in the ESPHome Remote Receiver is sometimes too quick for the group to pick up the change. Anyone had similar issues with Remote Receiver sensors? Not sure if there is some latency tweak I can do.