Pico Remote automation / script

Appologies that this probably falls into RTFM but I am super lost and after many days of going in circles I had to get some pointers.

I have a monoprice zone amp and a bunch of lutron pico remotes. All I am trying to do is map a button press to an action. (in a somewhat efficient way because I will end up with 12 remotes and 6 zones)
I was able to get a remote talking to a zone using automations and a WHOLE bunch of chained IF / THEN logic brute force style.

I know theres a better way. I am guessing that scripts is probably how but I just don’t event know where I am going wrong. or if it is even what I should be using.

When I make an automation with the following logic, it works. but I think it will end up too messy to do at scale.

- id: '1662140620804'
  alias: Audio Test Remote
  description: ''
  trigger:
  - platform: state
    entity_id:
    - sensor.remotes_test
  condition: []
  action:
  - if:
    - condition: state
      entity_id: sensor.remotes_test
      state: '2'
    then:
    - service: media_player.toggle
      data: {}
      target:
        entity_id: media_player.zone_11
  - if:
    - condition: state
      entity_id: sensor.remotes_test
      state: '4'
    then:
    - if:
      - condition: state
        entity_id: media_player.zone_11
        attribute: source
        state: '1'
      then:
      - service: media_player.select_source
        data:
          source: '2'
        target:
          entity_id: media_player.zone_11
      else:
      - service: media_player.select_source
        data:
          source: '1'
        target:
          entity_id: media_player.zone_11
  - if:
    - condition: state
      entity_id: sensor.remotes_test
      state: '8'
    then:
    - service: media_player.volume_up
      data: {}
      target:
        entity_id: media_player.zone_11
  - if:
    - condition: state
      entity_id: sensor.remotes_test
      state: '16'
    then:
    - service: media_player.volume_down
      data: {}
      target:
        entity_id: media_player.zone_11

My thought for how to turn this into a script call function (sorry if thats the wrong way to describe) is the following

Automation

- id: "1662220652179"
  alias: Audio test remote script test
  description: "test to send var to script"
  trigger:
    - platform: state
      entity_id: sensor.remotes_test
      from: "0"
  condition: []
  action:
    service: script.audio_remote
    data:
      variables:
        button: "{{ states.sensor.remotes_test.state }}"
        speaker: "{{ media_player.zone_11 }}"
        source: "{{media_player.zone_11.source}}"
  mode: single

Script

audio_remote:
  alias: audio remote script
  mode: parallel
  variables:
    button_pressed: "{{ button }}"
    speaker_zone: "{{ speaker }}"
    zone_soucre: "{{ source }}"
  sequence:
    # button play/pause (1) pressed
    - if: button_pressed == "1"
      then:
        - if: zone_source == 1
          then:
            - service: media_player.select_source
              data:
                source: "2"
              target:
                entity_id: speaker_zone
          else:
            - service: media_player.select_source
              data:
                source: "1"
              target:
                entity_id: speaker_zone
    # button center (2) pressed
    - if: button_pressed == "2"
      then:
        - service: media_player.toggle
          data: {}
          target:
            entity_id: speaker_zone
    # button next (4) pressed
    - if: button_pressed == "4"
      then:
        - if: zone_source == 1
          then:
            - service: media_player.select_source
              data:
                source: "2"
              target:
                entity_id: speaker_zone
          else:
            - service: media_player.select_source
              data:
                source: "1"
              target:
                entity_id: speaker_zone
    # button volume up (8) pressed
    - if: button_pressed == "8"
      then:
        - service: media_player.volume_up
          data: {}
          target:
            entity_id: speaker_zone
    # button volume down (16) pressed
    - if: button_pressed == "16"
      then:
        - service: media_player.volume_down
          data: {}
          target:
            entity_id: speaker_zone

As I say even just a point in the right direction is much appreciated, I will attest to being in too deep and having not enough a grasp on the architecture as a whole but this just seems really cumbersome to do in HA so im sure there is probably some base concept that I am not grasping because I would think it is a pretty basic function to map a physical button to an action and it seems automations is how you have to do it? Anyway really apprecaite any tips or help!

Yes, you can use a map (dictionary) to translate one value to another but for your application, button presses represent different actions (i.e. different service calls) so the neatest way to handle that is with a choose and some templating.

audio_remote:
  alias: audio remote script
  mode: parallel
  variables:
    button_pressed: "{{ button | int(0) }}"
    speaker_zone: "{{ speaker }}"
    zone_source: "{{ source | int(0) }}"
  sequence:
    - choose:
        - conditions: "{{ button_pressed in [1, 4] }}"
          # button play/pause (1) or next (4) pressed
          sequence:
            - service: media_player.select_source
              data:
                source: "{{ iif(zone_source == 1, 2, 1) }}"
              target:
                entity_id: speaker_zone
        - conditions: "{{ button_pressed == 2 }}"
          # button center (2) pressed
          sequence:
            - service: media_player.toggle
              target:
                entity_id: speaker_zone
        - conditions: "{{ button_pressed in [8, 16] }}"
          # button volume up (8) or down (16) pressed
          sequence:
            - service: "media_player.volume_{{ iif(button_pressed == 8, 'up', 'down') }}"
              target:
                entity_id: speaker_zone
      default: []
1 Like

Very many thanks for the help and point in the right direction! I knew it must have been my missing some basic function haha.

All but the zone select is working now.

It is changing the value but only in the not true case.

e.g.

 source: "{{ iif(zone_source == 1, 2, 1) }}"

The Zone will not change to 2 if already on 1 … but if you change to 2 manually, it will change to 1 on the button press.

I tried messing around with sending / receving int’s vs text but always the same.

Also does one need to restart the server every time you make a change to the scripts file? sometimes it seems that saves don’t get picked up by the system and so I never know if a change I made isnt working or didnt actually save…

Thanks again in advance!

You can simply execute Configuration > YAML > Reload Scripts. However you should first execute Configuration > YAML > Check Configuration to ensure the modifications you made didn’t introduce YAML syntax errors.

Is the media_player’s source literally a number?

Yes, kind of… in state attributes its listed as source: ‘1’ with my sources being - ‘1’ and -‘2’… so I assume it is storing this as text not as an integer ?

I did solve it thought, at least one way…

I wasn’t passing the specific attribute value to the script (assuming that is what solved it) rather before was sending this

source: "{{media_player.zone_11.source}}"

So I changed to this in automations,

      zone_source: "{{state_attr('media_player.zone_11', 'source')}}"

And have it going to the following in the script

              data:
                source: "{{ iif(zone_source == '1', '2', '1') }}"
              target:
                entity_id: "{{ zone }}"

You added quotes and that was sufficient to mark your own post as the Solution? OK.

Script

audio_remote:
  alias: audio remote script
  mode: parallel
  variables:
    button_pressed: "{{ button | int(0) }}"
    #speaker_zone: "{{ speaker }}"
    #zone_source: "{{ source }}"
  sequence:
    - choose:
        - conditions: "{{ button_pressed == 1 }}"
          # button play/pause pressed (not currently working)
          sequence:
            - service: media_player.volume_mute
              data:
                is_volume_muted: "{{ iff(zone_mute == true, false, true)}}"
              target:
                entity_id: "{{ zone }}"
        - conditions: "{{ button_pressed == 4 }}"
          # button next (4) pressed (working)
          sequence:
            - service: media_player.select_source
              data:
                source: "{{ iif(zone_source == 1, '2', '1') }}"
              target:
                entity_id: "{{ zone }}"
        - conditions: "{{ button_pressed == 2 }}"
          # button center (2) pressed (working)
          sequence:
            - service: media_player.toggle
              target:
                entity_id: "{{ zone }}"
        - conditions: "{{ button_pressed in [8, 16] }}"
          # button volume up (8) or down (16) pressed (working)
          sequence:
            - service: "media_player.volume_{{ iif(button_pressed == 8, 'up', 'down') }}"
              target:
                entity_id: "{{ zone }}"
      default: []

Automation

  alias: Audio test remote script test
  description: "test to send var to script"
  trigger:
    - platform: state
      entity_id: sensor.remotes_test
      from: "0"
  condition: []
  action:
    service: script.audio_remote
    data:
      #variables:
      button: "{{states.sensor.remotes_test.state}}"
      zone: media_player.zone_11
      zone_source: "{{state_attr('media_player.zone_11', 'source')}}"
  mode: single

You’re welcome!