String variable for script execution

I have what I thought was a pretty basic problem but I can’t find a relevant topic that seems to make sense to the use-case.

Basically I have some very long RF codes and for various reasons I’d like to store these as shorthand variables as they will be present multiple times across my scripts (as the way the RM pro pushes pulses out some need to be in sequence some don’t).

blind_1_up:
  sequence:
    - service: remote.send_command
      target:
        entity_id: remote.rm_pro_remote
      data:
        command:
          - b64:very_very_long_code
        num_repeats: 20

I’d like to be able to store a reference to the b64 RF code somewhere and call it something like “blind_1_up_code” as a variable and then refer to it in the script e.g.:

        command:
          - b64:blind_1_up_code

I’ve seen the data templates etc but these appear to be done via automations and I’ll be invoking different ones so I don’t want to encapsulate them there as that would seemingly defeat the purpose.

You could use your secrets.yaml file.

blind_1_up:
  sequence:
    - service: remote.send_command
      target:
        entity_id: remote.rm_pro_remote
      data:
        command:
          - !secret blind_1_up_code
        num_repeats: 20

Then in your secrets.yaml file (example):

blind_1_up_code: 01e44ad8-6891-42d5-8b77-e01e20af6fa384262cbb-13b8-4f2c-b096-bead7ffa78fcc454e168-6dee-45e8-aaeb-748659c9f095

Thanks I saw a suggestion on this but I think I’m getting stumped because of the required “b64:” prefix. If I don’t specify it I get an error and I’ve tried with / without this in the secret etc. Any insight on how to get around this?

Error: ValueError: You need to specify a device (where I specify b64 in secrets file)
Error: Invalid code: ‘!secret blind_1_up_key’ (where I specify it as below)

blind_1_up:
  sequence:
    - service: remote.send_command
      target:
        entity_id: remote.rm_pro_remote
      data:
        command:
          - b64:!secret blind_1_up_key
        num_repeats: 20

in secrets:

blind_1_up_key: sgGEAw4JDQkNCQ4JDQkNCQ0JDgkNCQ0JDgkNCQ0JDgkNCQ0JDgkNCQ0JDQkOCQ0JDQkOCQ0JDQkOCA4JDQkOCQ0JDQkOCA4JDQkNCQ4IDgkNCQ0JDgkNCQ0JDggOCQ0JDgkNCQ0JDgkNCQ0JDgkNCQ0JDgkNCQ0JDgkNCQ0JDgkNCQ0JDQkNCQwLDAoMCgwKDAsMCgwKDAoMCgwLDAoMCgwLCwsMCgwK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AAAAAA=

Why don’t you just store them in the RM config itself? I started on the route of sending the base64 codes in YAML and realized, as you do, that the repetitive nature of copy-pasting those codes all the time was not only a chore but would lead to a lot of manual fixing of my code if something changed, so now I just use the learn feature of the RM and HA stashes it into a file and then I just call the device + remote code name to execute it at will.

office_tv_vol_up:
  alias: Office TV Volume Up
  sequence:
  - service: remote.send_command
    data:
      entity_id: remote.office_remote
      device: sharp aquos
      command: volume up  

Ahhh yep there’s definitely a lot of copy paste! I used to use switches instead and that worked OK but it had no repeat function so was clunky also. I no longer have the remote to learn the codes although that does bring a question - do you know where the RM config is stored/saved and whether it’s plaintext / I can simply create a file myself?

Edit: looks like it’s stored in a JSON file albeit it says explicitly not to alter:

Learned codes storage location

The learned codes are stored in /configuration/.storage/ in a JSON file called broadlink_remote_MACADDRESS_codes. You can open this file with a text editor and copy the codes to set up custom IR/RF switches or to send them as base64 codes, but beware: the files in the .storage folder should never be edited manually.

The Broadlink integration has num_repeats as a command, so you can do that.

But, yes, I’ve never modified the JSON because of that message in the file and I seem to remember reading somewhere that the file gets rebuilt and any changes made directly to the file get lost, so I never tried to edit it by hand.

If you don’t have the remote but you have two RM’s, you could always use one to send the command to the other. Personally I have a half dozen of these things around, but I also have the remotes so I’ve never had to try this work-around.

Try modifying the file, there’s no harm in trying it (after getting a snapshot of course) and see if it persists through a reboot and learning new codes.

Yep that’s what I’m using but it doesn’t seem to work with the switch component. In any respect I much prefer the coded solution as that keeps my config clean! I’ll def try with the two RM pros thanks for the idea it should work crossed fingers!

For anyone that comes across this from google: I tried recording the codes through the native home assistant script syntax. This successfully created the entries in the JSON file but the codes were incorrect. I was able to successfully override these codes manually editing the JSON file. I have rebooted HA a few times etc and it seems to be persistent and all working 100% now. Thanks to those that replied :slight_smile: