Solution to Change inputs and turn on AV. Advice needed!

Hello.

I would want to choose input for my AV reveiver from input select menu and turn on the AV ON if its not ON already. Im using Broadlink RM to send the ir commands. I was wondering if there is easier solution for this. The problem is that the ON/OFF button is the same and it sends the same command to broadlink.

Here is my code (it is working but its pretty long)

input_select: 
  pioneer_input:
name: 'Pioneer av input:'
initial: "Off"
icon: mdi:speaker
options:
 - "Off"
 - "Radio"
 - "PS4"
 - "Digiboksi"
 - "Spotify"

script:
  pioneer_radio:
alias: Change to Radio
sequence:
# Power on Pioneer AVR 
 - service: switch.turn_on
   entity_id: switch.av_vahvistin
 - delay: '00:00:10'
# Change Input to Radio
 - service: broadlink.send_packet_192_168_8_xxx
   data:
     packet:
      - "JgAgAQABFowQNQ8VEDUSEhATETUQExE1EBMRNRATETUQNhATEDYQExE1ETQSNBATERIRExE1EBMQExATERMQNRE1ETUQExE1EAADbwABFYwRNRESETUSEhATEDURExE0EhIQNRISEDURNRESETUREhI0ETUQNhATEBQQEhE1ERIRExATERIRNRE1ETQSEhA1EgADbQABFowRNBETETQRExESEDYQExE1EBMQNhATEjQSMxETEDURExA1ETURNRESEBQQExA1ERMQExETEBMRNBE1ETUQExE1EAADbgABFY0QNRETETQQFBATETUQExI0EBMRNRESETUQNRISDzcREhA2ETQQNhESEBQQExA2EBMREhETEBMQNRE1ETUREhE1EAANBQAAAAAAAAAA"     


automation:
  - alias: "Change to Radio when AV is OFF"
trigger:
  platform: state
  entity_id: input_select.pioneer_input
  to: "Radio"
condition:
  condition: state
  entity_id: switch.av_vahvistin
  state: 'off'
action:
  service: script.turn_on
  entity_id: script.pioneer_radio
  - alias: "Change input to Radio when AV is ON"
trigger:
  platform: state
  entity_id: input_select.pioneer_input
  to: "Radio"
condition:
  condition: state
  entity_id: switch.av_vahvistin
  state: 'on'
###Change input to Radio      
action:
  service: broadlink.send_packet_192_168_8_xxx
  data:
    packet:
      - "JgAgAQABFowQNQ8VEDUSEhATETUQExE1EBMRNRATETUQNhATEDYQExE1ETQSNBATERIRExE1EBMQExATERMQNRE1ETUQExE1EAADbwABFYwRNRESETUSEhATEDURExE0EhIQNRISEDURNRESETUREhI0ETUQNhATEBQQEhE1ERIRExATERIRNRE1ETQSEhA1EgADbQABFowRNBETETQRExESEDYQExE1EBMQNhATEjQSMxETEDURExA1ETURNRESEBQQExA1ERMQExETEBMRNBE1ETUQExE1EAADbgABFY0QNRETETQQFBATETUQExI0EBMRNRESETUQNRISDzcREhA2ETQQNhESEBQQExA2EBMREhETEBMQNRE1ETUREhE1EAANBQAAAAAAAAAA"     

Thank you in advance!

What you’re looking for is what’s called “discrete” power on/off codes. Pioneer publishes an exhaustive list of such codes. This way you can send an “on” or “off” command instead of a power toggle which is essential to getting these sorts of automations to work well.