Yaml code to send ir code to mqtt

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