Control AC using IR blaster

I have an IR blaster and I’m trying to control my air conditioner with it. I want to enable the selection of mode (cooling, auto, heating), fan strength (1-5, auto), and temperature (from 18 to 30). Using input select for mode and fan strength makes sense to me, as well as a number input for temperature. Since I don’t have a smart plug and don’t know if the AC is on or off, I would use automation triggered by a button, and that automation would then run a script.

Now, the problem is with the script and how to make it simple to maintain. The issue is that there is a separate code for each of these options, and I need to somehow retrieve it.

Mode: Cooling
(TEMP - FAN_SPEED = IR_CODE)

18 - auto = CUgM+SUrAgoG/gHgAwHgBw/gHwFAN0ADQAHgAwfACwcrAv4B/gH+AQ==
18 - 5 = CdoL9SX4ARgG+AHgAwHgBw8F+AEoAvgB4AcB4AkTQDdAA0AZQAdAAUALQAMLGAb4ARgG+AEYBvgB
18 - 4 = CSYMDib/AfsF/wHgAwHgBw8F/wEzAv8B4AUB4AkRAf8BQDfAA0ABQAtAAUAHC/sF/wH/Af8B+wX/AQ==
18 - 3 = CSEMAiYMAjkGDALgAwHgAQ8DxQEMAuADAUAPwAPgAwGAEwE5BoAD4AMPAFsgG0ADCzkGxQEMAsUBOQbFAQ==
18 - 2 = CSsM9iUBAv8FAQLgAwHgBw/gHwFAN8ADwAHADwv/BQECAQIBAgECAQI=
18 - 1 = CEoM9CUCAvgFAuAEAOAHD+AfAEA3QAPgDwAH+AUCAvgFAgI=

etc. Does anyone have an idea of how to best create the script without it being a bunch of if-then statements?

I believe I read a thread where someone read the codes as binary and found how to change the code to get the settings they wanted.

I don’t know about that, though I wonder if it would work. Here, the remote for the AC gives a different code for the same command each time, although the code I capture consistently works.

This is what im trying to make it work:

alias: AC Control Script
description: Control AC via IR blaster with improved error handling and flexibility
variables:
  mode: "{{ states('input_select.ac_mode') | lower }}"
  fan_speed: "{{ states('input_select.ac_fan_speed') | lower }}"
  temperature: "{{ states('input_number.ac_set_temperature') | int }}"
  ir_codes:
    cold:
      "18":
        "1": CEoM9CUCAvgFAuAEAOAHD+AfAEA3QAPgDwAH+AUCAvgFAgI=
        "2": CSsM9iUBAv8FAQLgAwHgBw/gHwFAN8ADwAHADwv/BQECAQIBAgECAQI=
        "3": >-
          CSEMAiYMAjkGDALgAwHgAQ8DxQEMAuADAUAPwAPgAwGAEwE5BoAD4AMPAFsgG0ADCzkGxQEMasUBOQbFAQ==
        "4": >-
          CSYMDib/AfsF/wHgAwHgBw8F/wEzAv8B4AUB4AkRAf8BQDfAA0ABQAtAAUAHC/sF/wH/Af8B+wX/AQ==
        "5": >-
          CdoL9SX4ARgG+AHgAwHgBw8F+AEoAvgB4AcB4AkTQDdAA0AZQAdAAUALQAMLGAb4ARgG+AEYBvgB
        auto: CUgM+SUrAgoG/gHgAwHgBw/gHwFAN0ADQAHgAwfACwcrAv4B/gH+AQ==
sequence:
  - service: persistent_notification.create
    data:
      title: AC Control Debug - Initial Variables
      message: >-
        Mode: {{ mode }}, Temperature: {{ temperature }}, Fan Speed: {{
        fan_speed }}
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ mode == 'cold' }}"
          - condition: template
            value_template: "{{ temperature | int == 18 }}"
          - condition: template
            value_template: "{{ fan_speed in ['1', '2', '3', '4', '5', 'auto'] }}"
        sequence:
          - service: mqtt.publish
            data:
              topic: zigbee2mqtt/remote.living/set
              payload: |
                {"ir_code_to_send": "{{ ir_codes['cold'][18][fan_speed] }}"}
          - service: persistent_notification.create
            data:
              title: AC Control - IR Code Sent
              message: >-
                Sent IR code for {{ mode }} mode, {{ temperature }}°C, Fan
                Speed: {{ fan_speed }}
    default:
      - service: persistent_notification.create
        data:
          title: AC Control Error
          message: >-
            No matching IR code found for: Mode: {{ mode }}, Temperature: {{
            temperature }}, Fan Speed: {{ fan_speed }}
mode: queued
max: 5

If your IR blaster is one of a select few (including Broadlink), there is a smartir HACS integration that can do all the heavy lifting for you.