Remote automation - last pressed

I am trying to use a dumb TV remote with an IR receiver to control 4 different devices, according to the last color button press.

I want to control an AppleTV, Chromecast, CableTV and ShieldTV, and for the 4 devices, map the arrow buttons (up/down/right/left/enter), volume, and channel up/down.

Let’s say each color button represents an input

  • red = ATV
  • green = Chromecast
  • yellow = cableTV
  • blue = shieldTV

So let’s say the last color button I pressed is RED, I’d want all the remote keys (arrows, volume and channel) to only send commands to the device that corresponds to the RED key (ATV).

How can I do this? I don’t mind using unique scripts for every single command for every device to make it easier, e.g. :

  • script.ATV_vol_up
  • script.Chromecast_vol_up
  • script.ATV_arrow_left

Each remote button press will be captured thanks to its corresponding binary_sensor, e.g. :

  • binary_sensor.irremote_green
  • binary_sensor.irremote_red
  • binary_sensor.irremote_vol_up

I believe some blueprints are using text helpers that take the Last Pressed (in my case the last color button) Input in order to make such a thing work…

Thanks for the help !

I would set up an automation to react on the color buttons and set an input_select.
The value of the input select should then be ATV, Chromecast…

When you later press the up button you should be able to template using the select

{{ 'script.' ~ states('input_select.color') ~ '_vol_up' }} 

I’m not entirely sure that you can trigger an automation with a template like that but I think it works.

But can’t you make it “automatic”?
If chromecast is used then send the commands there, if cableTV is used send commands there.
I did that with our IKEA remote so that it normally sends commands to chromecast but if the DVD player was used then it sends the commands there.

I’ve managed to create an input helper with my 4 inputs, and I’ve created an automation that selects the corresponding input (action: input_select.select_option) according to the IR remote keypress.
Thanks !

So, this makes some assumptions. But I think this can be done in a single automation.

I don’t have all of the devices you have, so I can’t fully test this. I have all Roku devices that I can control from HA and I stole some of my code from that.

Assumptions:

  1. All of your devices use the remote.send_command action to send the button presses.
  2. All of your devices use the same commands for the button presses.
  3. The entity ids of your devices are remote.atv, remote.Chromecast, remote.cableTV, and remote.shieldTV.

Adjustments may need to be made.

I made an input_text helper named remote_device.

There are two sets of triggers. The first set is the four color buttons that use a trigger_id of color.
The rest of the irremote buttons are in the next group and no trigger_id.

The first choose action condition is for trigger_id of color. If so, strip off the front of the trigger.entity_id and save the color in the input_text helper.

Anything not a color button falls into the choose default. Build a couple variables:
device_entity_id - derived from a list of button color and entity_id using the input_text helper to select the correct one.
remote_command - strips off the front of the trigger.entity_id to get the button name. For my Rokus, the vol up and down commands are actually volume_up and volume_down. So, I do a replacement to change those.

Then the action is the remote.send_command using those two variables.

Mode is queued so each subsequent button push will run after the previous completes.

Maybe you can adapt this based on your specific device requirements.

description: "Remote"
mode: queued
triggers:
  - entity_id:
      - binary_sensor.irremote_red
      - binary_sensor.irremote_green
      - binary_sensor.irremote_yellow
      - binary_sensor.irremote_blue
    to: "on"
    id: color
  - entity_id:
      - binary_sensor.irremote_up
      - binary_sensor.irremote_down
      - binary_sensor.irremote_right
      - binary_sensor.irremote_left
      - binary_sensor.irremote_enter
      - binary_sensor.irremote_channel_up
      - binary_sensor.irremote_channel_down
      - binary_sensor.irremote_vol_up
      - binary_sensor.irremote_vol_down
    to: "on"
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - color
        sequence:
          - action: input_text.set_value
            target:
              entity_id: input_text.remote_device
            data:
              value: "{{ trigger.entity_id | replace('binary_sensor.irremote_', '') }}"
    default:
      - variables:
          device_entity_id: |
            {{ ([
              {'button_color': 'red', 'device_entity_id': 'atv'},
              {'button_color': 'green', 'device_entity_id': 'Chromecast'},
              {'button_color': 'yellow', 'device_entity_id': 'cableTV'},
              {'button_color': 'blue', 'device_entity_id': 'shieldTV'}
             ] | selectattr("button_color", "eq", states('input_text.remote_device')) | map(attribute="device_entity_id") | list)[0] }}
          remote_command: "trigger.entity_id | replace('binary_sensor.irremote_', '') | replace('vol_', 'volume_')"
      - action: remote.send_command
        metadata: {}
        data:
          command: "{{ remote_command }}"
        target:
          entity_id: remote.{{ device_entity_id }}

Here’s a quick video of what I achieved: