Modify mqtt payload in automation?

Hi,

Im just put together a custom milight hub (https://github.com/sidoh/esp8266_milight_hub)
Now i want to use one of my milight remotes to control more than just the milights directly but also other stuff arround my flat.

This is possible using a little automation as the hub picks up every signal from the remote and publishes it to mqtt. HA will pick this up and forward it to the right light.
I can now control my rgbw light this way even tho it is not paired with this remote.
Only one Problem, as this is RGBW and not RGB_CCT the color_temp command wont trigger my bulb into white mode, this only works by using another command.

So my plan is if the trigger payload looks like {“color_temp”:123} it should replace the payload and if not just forward the original payload.

automation:
  - alias: MiLight Remote - G2
    initial_state: True
    trigger:
      platform: mqtt
      topic: milight/updates/0x34D3/rgb_cct/2
    action:
      - service: mqtt.publish
        data_template:
          topic: "milight/commands/0x1/rgbw/2"
          payload_template: >
            {% if trigger.payload.color_temp -%}
              {{ {"command":"set_white"} }}
            {%- else -%}
              {{ trigger.payload }}
            {%- endif %}

But this won’t work and i can’t understand why, it will never detect the color_temp key. And HA always
publishes the original payload.

I tested this with the templating tool and there it works fine:

Setup test trigger with color_temp as payload:
{% set trigger = {
  "payload": {"color_temp":248}
} %}
Template result:
{% if (trigger.payload.color_temp) -%}
  {{ {"command":"set_white"} }}
{%- else -%}
  {{ trigger.payload }}
{%- endif %}


Setup test trigger with hue as payload:
{% set trigger = {
  "payload": {"hue":248}
} %}
Template result:
{% if (trigger.payload.color_temp) -%}
  {{ {"command":"set_white"} }}
{%- else -%}
  {{ trigger.payload }}
{%- endif %}

Maybe someone else has an idea why it wont work in my automation.

It won’t work because jinja cannot output complex objects like dictionaries and lists. It’s outputting them as a string and the interpreter is keeping it a string.

Thanks, can you point me in the right direction to get this working? If it is possible?
I have spend way to much time on this allready :slight_smile:

I just realized that you aren’t even at that point, your code is failing at the if statement before.

if you want to make that if statement, check to see if the payload contains the key:

    {% if 'color_temp' in trigger.payload %}

This line still wont work and you’ll need to do something different:

    {{ {"command":"set_white"} }}

You should always pull the information out of the payload or populate it with default. I don’t know what’s in your payload so this code will probably not work for you:

      payload_template:
        command: >
          {% if 'color_temp' in trigger.payload %}
            {{ trigger.payload.color_temp }}
          {%- else -%}
            set_white
          {%- endif %}

The payload is a simple json string:
{“color_temp”:123} or {“hue”:223} or {“brightness”:145}

the color temp wont work on my bulb so thats why i want to replace it.

And yes your advice got it working for me:

automation:
  - alias: MiLight Remote - G2
    initial_state: True
    trigger:
      platform: mqtt
      topic: milight/updates/0x34D3/rgb_cct/2
    action:
      - service: mqtt.publish
        data_template:
          topic: "milight/commands/0x1/rgbw/2"
          payload_template: >
            {% if 'color_temp' in trigger.payload -%}
              {{ {"command":"set_white"} }}
            {%- else -%}
              {{ trigger.payload }}
            {%- endif %}

Thank you very much!

I’m surprised that works to be honest. Usually that syntax would return an error. I’m gonna have to investigate this. It doesn’t make sense that you can output a json dict using templating.

Ok thats strange but good for me i guess.

Just for your information, im still running 0.65.6 (Docker).

I’m not sure that matters. The templating has been pretty solid for a while. I’m wondering if I’ve been steering people wrong because I made a mistake in the past or if payload_templates are unique. Either way, I need to investigate.

I ran some tests and talked with some people, this only works because MQTT is parsing the string properly when reading the payload. This will not work for other templates. Just keep that in mind when you create other automations.