Automation Config - Updating entity based on MQTT topic

Hey folks, I’m trying to set up an automation that is triggered by an MQTT topic and then sets the value of an input number slider based on data from the MQTT topic. The trigger seems to be working fine, but the action that is supposed to update the input number value is not doing anything. I tried using the template editor, but it doesn’t seem to work for automations.

I suspect that I’m not parsing the trigger payload correctly in the data_template. Any insight on what I may be doing wrong?

Here’s the yaml:

- alias: Kitchen_RGBW_Sub
  id: Kitchen_RGBW_Sub
  trigger:
    platform: mqtt
    topic: "stat/Lights/Kitchen_Cab/STATE"
  action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.red_slider
        value: "{{ trigger.payload.value_json.Color.split(',')[0] | int}}"
    - service: input_number.set_value
      data_template:
        entity_id: input_number.green_slider
        value: "{{ trigger.payload.value_json.Color.split(',')[1] | int }}"
    - service: input_number.set_value
      data_template:
        entity_id: input_number.blue_slider
        value: "{{ trigger.payload.value_json.Color.split(',')[2] | int }}"
    - service: input_number.set_value
      data_template:
        entity_id: input_number.white_slider
        value: "{{ trigger.payload.value_json.Channel[3] | int }}"

You could help if you posted the actual payload.

Yes, of course. The data is published in json, here is an example:

{
    "Channel": [
        0,
        0,
        0,
        255
    ],
    "Color": "0,0,0",
    "POWER": "OFF",
    "Dimmer": 1
}

In the template editor :

Imitate available variables:
{% set my_test_json = {
    "Channel": [
        0,
        0,
        0,
        255
    ],
    "Color": "0,0,0",
    "POWER": "OFF",
    "Dimmer": 1
} %}

value: "{{ my_test_json.Color.split(',')[0] | int}}"

returns “0”, did not expect that.

Try this :

Imitate available variables:
{% set my_test_json = {
    "Channel": [
        0,
        0,
        0,
        255
    ],
    "Color": "0,0,0",
    "POWER": "OFF",
    "Dimmer": 1
} %}

value: {{ my_test_json.Color.split(',')[0] | int }}

value is now 0

I need the quotations around {{ my_test_json.Color.split(’,’)[0] | int}} in order for the automations.yaml file to be valid. But it does appear that my parsing is correct from the .value_json onward.

So does that indicate that either “trigger.payload” is not the correct value to use or maybe the " - service: input_number.set_value" is not correctly used?

To be honest, I’m lost here too. Both seem correct.

Here they use the trigger.payload.value_json successfully

I used trigger.payload_json, not payload.value_json

Try

"{{ trigger.payload_json.Color.split(',')[0] | int}}"

Thanks for the input everybody! It looks like changing to using payload_json did the trick. Here’s what I ended up using:

- alias: Kitchen_RGBW_Sub
  id: Kitchen_RGBW_Sub
  trigger:
    platform: mqtt
    topic: "stat/Lights/Kitchen_Cab/STATE"
  action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.red_slider
        value: "{{ trigger.payload_json.Color.split(',')[0] | int }}"
    - service: input_number.set_value
      data_template:
        entity_id: input_number.green_slider
        value: "{{ trigger.payload_json.Color.split(',')[1] | int }}"
    - service: input_number.set_value
      data_template:
        entity_id: input_number.blue_slider
        value: "{{ trigger.payload_json.Color.split(',')[2] | int }}"
    - service: input_number.set_value
      data_template:
        entity_id: input_number.white_slider
        value: "{{ trigger.payload_json.Channel[3] | int }}"