MQTT Automation modify payload

I am trying to setup an automation that listens to, and sends MQTT messages.

A unit in my network (in this case unit50), sends servo position requests via mqtt.
I want to setup an automation that triggers when receiving mqtt messages from this unit,
(no matter what the contents of the payload is),
and then uses this payload in the action: - field of the automation,
where I want to send this received payload info (via mqtt) to another unit (in this case unit65) in a modified format:

Example:
Trigger:
I want to listen for mqtt messages from unit50 as follows:
topic: unit50
payload example : [1234]

where topic = the unit from where I get servo data
and payload = the servo data I want to use
(The number 1 in payload is the servonumber of the servo to set (0-9)
and the number 234 is the servo position value I want to send (000-100)).

Action :
When I receive a message from unit50, I want to send a mqtt message as follows:
topic: unit65
payload : [S1][234]

where digit 1 is the servonumber from the above trigger payload
and 234 is the servo position value from the above trigger payload.

So it would be great if someone could help me, or direct me where can I find information on how to read and modify this payload data…
(If possible please provide a simple automation example, as I am still learning from a low level)…

Hi,
I’d suggest three stages:

  • Get the values into HASS entities as you would for any other MQTT sensor
  • Create an automation triggered by one of those entiries changing
  • In the triggered automation, MQTT.publish new MQTT topics.

The MQTT documentation is your friend.

I use something similar to write HASS sensor values out to a legacy logging script using MQTT as a comms bus.

My concern is the speed/ latency of using HASS as control system over MQTT - there’s a lot of time passing across MQTT / HASS / trigger / automation / MQTT which may be an issue depending on what the servo is controlling. There’s lots of Python MQTT example code out there that might execute faster than HASS or even NodeRed.

If this helps, :heart: this post!

Hi James.
Thank you for a fast reply…
If possible please provide some more specific info as I am new to this…

  1. If I set up an automation, but only specify topic and nothing in the payload field in the mqtt trigger area, will the automation then trigger on all incoming mqtt messages that is adressed to the specified topic ??
  2. If so, how can I access and modify the payload info, so I can use it in the payload section of my outgoing mqtt message ??
  1. MQTT topics without a payload are likely to update a HASS entity, but I’ve not written a test to be sure.
  2. The same way as any other MQTT sensor which updates a HASS entity - try creating a manual MQTT item which updates an entity. The entity is just a variable within HASS which can trigger an automation, and be used in a MQTT.publish.

My suggestion is to try some experiments, and report back.

Ok, tested first issue & found that even an empty mqtt payload in the trigger field activates the automation…

I’m still working on how to read the trigger payload & modify the payload for the action part of the automation…

:slight_smile:

Despite hours of testing (& Googling) I still cannot figure out how to configure an automation to read subscribed mqtt payload data and publish this as a modified payload string…

This is the simple automation where I need help …

  • id: ‘685010470446’
    alias: test unit50 mqtt
    description: ‘Read incoming mqtt payload and publish a modified payload string’
    trigger:
    • platform: mqtt
      topic: unit50
      condition: []
      action:
    • service: mqtt.publish
      data:
      qos: 0
      retain: false
      topic: unit65
      payload: “???”
      mode: single

As described earlier :
I want the automation to trigger as soon as a message from unit50 arrives, no matter what information is in the payload part of the message. (That part of the automation is tested and works !!)

Then I want to grab the data in the payload part of the message…

Is there a variable that holds the payload part of the subscribed message, or do I have to specify a variable to hold that information, (and if so, how do i do this ??)

The typical format of the subscribed payload is : [1234]
where 1 represents a servo number and 234 represents the servo position (000-100)

I want to take the content of this payload, and put it in the action: mqtt.publish payload field, in a modified form:
[S1][234]
where 1 is the servo number and 234 is the servo position from the subscribe payload string info.

If I can get hold of the subscribed payload data, I guess the data modification part is rather simple…

So the main question seems to be:
How do I get the payload part of the subscribed message so I can modify it…??

trigger.payload

Example

alias: example 
trigger:
  - platform: mqtt
    topic: unit50
condition: []
action:
  - service: mqtt.publish
    data:
      topic: unit65
      payload_template: "[S{{ trigger.payload[1] }}][{{ trigger.payload[-4:] }}"
mode: single

The template employs python’s string slicing to select the desired characters from the received string.

Thank you Taras.

This is exactly what i am looking for and it works just perfect…

Thank you !!! for saving me a lot of further hopeless googling…

The Home Assistant community is really a great source of information & a problem solver…

:slight_smile: :slight_smile: :slight_smile:

1 Like