Parse parameters from MQTT

Hello to all!
I have a problem with parsing payload from MQTT. I have a device, which send it’s parameters to MQTT topic in this way:

$18 CE:0|CC:0|CO:0|CK:0|NC:0|SC:105|C1:255,255,255|DC:1|DD:3|DI:240|NP:1|NT:60|NZ:2|NS:[]|DW:1|OF:0|BR:100|PW:2200;

here is NAME:VALUE format. I’ve created some input_booleans, input_numbers and input_texts in HA to store this parameters. The length of this string depends from how many parameters were changed. Sometimes in can contain only 1 parameter, sometimes more.
I’m trying to create automation to parse this payload and put them into their input_***s
Here is my automation

  - alias: Get Parameters
    initial_state: true
      platform: mqtt
      topic: matrix/WiFiPanel-0/dta
      - service: input_number.set_value
        data_template: >
            {% for i in range(0, 20) %}
              {% if trigger.payload.split(" ")[1].split(";")[0].split("|")[i].split(":")[0] == "BR" %}
              entity_id: input_number.matrix_brightness
              value: '{{ trigger.payload.split(" ")[1].split(";")[0].split("|")[i].split(":")[1] }}'
              {% elif trigger.payload.split(" ")[1].split(";")[0].split("|")[i].split(":")[0] == "PW" %}
              entity_id: input_number.matrix_current_limit
              value: '{{ trigger.payload.split(" ")[1].split(";")[0].split("|")[i].split(":")[1] }}'
              {% else %}
              {% endif %}
            {% endfor %}

But it don’t work. The problem is, that it need also run different actions for boolean, number and text parameters. Is it even possible to get this working?

Input it a JSON since each component is labeled.

1 Like

This device can only publish parameters in that format

You can import with a value template. That will eliminate any need for this automation.

1 Like

Oh, really, thanks! Forgot about that component…