Need remote transmitter lambda example for sending raw code

I’m trying to make a remote transmitter lambda call using a raw code. There is a good example in the docs using the Pioneer protocol as show below:

// Example - transmit using the Pioneer protocol
auto call = id(my_transmitter).transmit();
esphome::remote_base::PioneerData data = { rc_code_1, rc_code_2 };
esphome::remote_base::PioneerProtocol().encode(call.get_data(), data);
call.set_send_times(2);
call.perform();

Looking for a similar example for raw code. This is what I have tried to put together so far but I’m at my limit of my coding knowledge and ability to make connection between RemoteTransmitData and the TransmitCall.

script:
  - id: thermostat_script
    then:
    - lambda: |-               
        std::vector<int32_t> hvac_code;
        hvac_code = {+4500,-2700...+4450,-50100};
        // Call remote transmitter using selected code
        auto call = id(my_remote_transmitter).transmit();
        esphome::remote_base::RemoteTransmitData * RTD;
        RTD.set_data(hvac_code);
        RTD.set_carrier_frequency(38);
        esphome::remote_base::RawAction<esphome::remote_base::RemoteTransmitData>().encode(RTD);
        call.get_data(RTD);
        call.perform();

Not sure if this would work for you but ESPHome has a method to send raw data: Remote Transmitter - raw.

Yes, I have used that before but in this case I had a bunch of lambda code in order to pre-process so I wanted to call it from within the lambda. I also wasn’t aware I could template the code value, but have since learned that I can. So I altered my approach and now pre-process and determine the code in the lambda and then return the code. I originally was trying to use a string-vector dictionary in the lambda to store the various codes with a corresponding string key, but I struggled quite a bit and gave up and just went with an if statement.

I think it would still be useful to see how to make the call from within the lambda. I found it difficult to navigate the API and figure out how to connect the various classes that appeared to be needed.

script:
  - id: thermostat_script
    then:
    - remote_transmitter.transmit_raw:
        carrier_frequency: 38kHz
        code: !lambda |-
          if (id(houghton).mode == CLIMATE_MODE_OFF || id(houghton).action == CLIMATE_ACTION_IDLE || id(houghton).action == CLIMATE_ACTION_OFF) {
            ESP_LOGD("thermostat", "Transmitting IR code for OFF");
            return {+4550,-2700,..+4450,-50100};
          } else if (id(houghton).mode == CLIMATE_MODE_COOL && id(houghton).fan_mode == CLIMATE_FAN_LOW) {
            ESP_LOGD("thermostat", "Transmitting IR code for COOL-LOW");
            return {+4500,-2700,..4450,-50100};
......
          } else { // UNKNOWN therefore OFF
            ESP_LOGD("thermostat", "Transmitting IR code for OFF");
            return {+4550,-2700,..4450,-50100};
          }

1 Like

@ncarney - Any progress on this?

For anyone who comes across this now, what worked for me was creating buttons using the button component to transmit the raw ir code (remote_transmitter.transmit_raw) and then using lambda in whatever other component I wanted. You can use id(my_button).press(); inside lambda to trigger it. I also gave the buttons entity_category: config so that they are not forward facing.

button:
  - platform: template
    name: "Phillips Speaker Power"
    id: phillips_power
    entity_category: config
    on_press:
      - remote_transmitter.transmit_raw:
          code: [2706, -859, 481, -866, 479, -418, 484, -415, 481, -864, 925, -421, 481, -417, 933, -861, 480, -418, 482, -415, 481, -417, 480, -418, 480, -420, 479, -416, 457, -443, 479, -417, 936, -411, 456, -890, 480, -418, 481]
          carrier_frequency: 38kHz
  - platform: template
    name: "Phillips Speaker Bluetooth"
    id: phillips_bluetooth
    entity_category: config
    on_press:
      - remote_transmitter.transmit_raw:
          code: [2677, -891, 452, -893, 454, -445, 451, -446, 1346, -1320, 454, -445, 902, -892, 453, -447, 452, -445, 452, -446, 453, -445, 901, -894, 453, -445, 453, -445, 453, -444, 901, -445, 454, -893, 455]
          carrier_frequency: 38kHz
  - platform: template
    name: "Phillips Speaker USB"
    id: phillips_usb
    entity_category: config
    on_press:
      - remote_transmitter.transmit_raw:
          code: [2704, -861, 482, -864, 482, -416, 480, -418, 1374, -1293, 479, -418, 930, -865, 481, -417, 480, -418, 482, -416, 480, -417, 929, -866, 482, -417, 482, -415, 480, -418, 930, -416, 481, -417, 481]
          carrier_frequency: 38kHz

# Select component for Phillips Speaker modes
select:
  - platform: template
    name: "Phillips Speaker"
    id: phillips_mode
    options:
      - "on"
      - "off"
      - "bluetooth"
      - "usb"
    optimistic: True
    set_action: 
      then:
        - lambda: |-
            if (x == "on") {
              id(phillips_power).press();
            } else if (x == "off") {
              id(phillips_power).press();
            } else if (x == "bluetooth") {
              id(phillips_bluetooth).press();
            } else if (x == "usb") {
              id(phillips_usb).press();
            }

I had the same problem. The solution was the code:

auto call = id(ir_transmitter).transmit();
auto data = call.get_data();
esphome::remote_base::RawTimings ir_code = {364, -625, 156, -286, 156, -468, 156, -390, 156, -546, 156};
data->set_data(ir_code);
data->set_carrier_frequency(38000);
call.perform();