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.