Pass IR-Command to User Defined Service

I’m trying to pass infrared-commands to a user defined service on an esp32.
In general the service works, when I define static values for “address” and “command”.

api:
  services:
    - service: send_ir
      then:
        - remote_transmitter.transmit_nec:
            address: 0x4040
            command: 0x807F

But When I try to pass the values as parameters, there is always a problem with type conversion:
When I pass them as a string

api:
  services:
    - service: send_ir
      variables:
        address_str: string
        command_str: string
      then:
        - remote_transmitter.transmit_nec:
            address: !lambda 'return address_str;'
            command: !lambda 'return command_str;'

during compile-time it’s this error:

/config/esphome/_common/component_remote_transmitter.yaml: In lambda function:
/config/esphome/_common/component_remote_transmitter.yaml:20:14: error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'uint16_t {aka short unsigned int}' in return
             address: !lambda 'return address_str;'

When I pass them as int

        address_str: int
        command_str: int

and enter them as int in dev-tools (0x4040 → 16448; 0x807F → 32895) nothing happens.

I guess, I would have to define and enter them as string and do some kind of conversion in the lambda, but how?

Is this even possible?

This is how I convert a string to a vector for writing to the UART:

  services:
    - service: write
      variables:
        command: string
      then:
        - uart.write:
            id: projector
            data: !lambda |-
              std::string str = command;
              std::vector<uint8_t> vec(str.begin(), str.end());
              return vec;

I’d be lying if I said I fully understand it. The helpful folks in the ESPHome Discord channel did the conversion bit for me. If thiis does not help you with your conversion I suggest you ask over there. https://discord.gg/P5rTy49w

Thanx! I finally found the correct conversion:

api:
  services:
    - service: send_ir
      variables:
        address_str: string
        command_str: string
      then:
        - remote_transmitter.transmit_nec:
            address: !lambda 'return (int)strtol(address_str.c_str(), NULL, 16);'
            command: !lambda 'return (int)strtol(command_str.c_str(), NULL, 16);'

2 Likes