Yaml code to send ir code to mqtt

hi all,
I’m struggling with sending ir codes to an zigbee IR-remote control. Using the visual editor I can send ir codes using an pre-defined action. However, I’m not able to send the same code via a variable.

I found some code to send the ir code via MQTT but it doesn’t work either :pensive:

Please could anyone see quickly what is wrong in the next script. I’m getting desperate.

alias: Denon Remote Control
description: Denon IR Remote Control 
triggers:
  - trigger: state
    entity_id:
      - input_boolean.denon_button_switchonoff
conditions: []
actions:
  - variables:
       test_ir_code_to_send: >-
        "CwYBJwMGATcH2gAnA0ADAQYBgAOACwI3BzVgAwInAwYgA8ATQAuAAwnrsAYBJwPaADcHQCNAA0APATUBQA8BNwdAC4ADATcHQA+AA0ATwAsD67A1AYAPAAZgB0ALBScD2gAnA0AHBAYBNwc1YAMBJwPgAQMLNwfaACcD2gAnAwYB"
   - data:
      topic: zigbee2mqtt/IrRemoteControl_Livingroom/set
      payload: >-
        {"ir_code_to_send":
        "CwYBJwMGATcH2gAnA0ADAQYBgAOACwI3BzVgAwInAwYgA8ATQAuAAwnrsAYBJwPaADcHQCNAA0APATUBQA8BNwdAC4ADATcHQA+AA0ATwAsD67A1AYAPAAZgB0ALBScD2gAnA0AHBAYBNwc1YAMBJwPgAQMLNwfaACcD2gAnAwYB"} 
    action: mqtt.publish
   - data:
      topic: zigbee2mqtt/IrRemoteControl_Livingroom/set
      payload: >-
        {"ir_code_to_send": test_ir_code_to_send} 
    action: mqtt.publish
mode: single

many thanks in advance!
wolly wonka

Hi WollyWonka,

Are you sure that is the right code? Generally base64 codes are required, but that doesn’t look like one. They look more like this…

JgBgAAABJ5MUERQRFDYUERQRFBEUEhMSEzYUNhQRFDYUNhQ2FDYTNhQ2FBEUEhMSFBEUERQRFBEUERQ2FDYUNhQ2EzYUNhQ2FAAFHAABKEgUAAxUAAEpSBQADFQAAShJFAANBQAAAAAAAAAA

Thanks for response.

Yes it is the right code (I copied it from the listening-mode), but it doesn’t show up in the UI as well. Normally, if it works, you will see in the UI of HA or Zigbee2MQTT the last code sent, even if it is wrong.

I won’t get any further :unamused:

If I changed the part to send to zigbee2mqtt to the following:

 - device_id: eaf3fdxxxxxxxxxxxxxxxxxxxxxxxx
   domain: text
   entity_id: text.irremotecontrol_livingroom_ir_code_to_send
   type: set_value
   value: test_ir_code_to_send

the output value of ir_code_to_send is “test_ir_code_to_send” and not the content of the variable.

I don’t understand why. I tried {{ test_ir_code_to_send }} but that didn’t work either…

Please what am I doing wrong?

Thanks in advance!!

Your first attempt has two problems.

  1. You’re using a YAML line continuation character >- to indicate the string begins on the next line but you’re still delimiting the string with double-quotes. Those double-quotes become part of the transmitted IR code. They should be removed.

  2. The following line is sending the literal string test_ir_code_to_send (not the value of the variable with that name).

payload: >-
  {"ir_code_to_send": test_ir_code_to_send}

Why? Because you need to use a template to indicate that the string test_ir_code_to_send shouldn’t be taken literally but ought to be interpreted as the name of a variable.

payload: >-
  {"ir_code_to_send": {{test_ir_code_to_send}} }

Your second attempt failed because a Device Action doesn’t support the use of variables or templates.


Try the following version. This is how I define the variable containing an IR code and transmit it.

alias: Denon Remote Control
description: Denon IR Remote Control 
triggers:
  - trigger: state
    entity_id:
      - input_boolean.denon_button_switchonoff
conditions: []
actions:
  - variables:
      ir_code:
        ir_code_to_send: >- 
          CwYBJwMGATcH2gAnA0ADAQYBgAOACwI3BzVgAwInAwYgA8ATQAuAAwnrsAYBJwPaADcHQCNAA0APATUBQA8BNwdAC4ADATcHQA+AA0ATwAsD67A1AYAPAAZgB0ALBScD2gAnA0AHBAYBNwc1YAMBJwPgAQMLNwfaACcD2gAnAwYB
   - action: mqtt.publish
     data:
      topic: zigbee2mqtt/IrRemoteControl_Livingroom/set
      payload: "{{ ir_code | to_json }}"
mode: single