Split MQTT payload and send values to entities

Hi,

I’m new to home assistant (I want to switch from openhab to home assistant) and I have a first question.

What is the best way to split a MQTT payload in pieces and use/send this “pieces” as a state to different entities?

At this moment I configured a (MQTT) sensor that receives a string like this: “*STR,3,30,50,50,50,2,1;” . Now I want to use/send these different values as a state to different entities.

I’m able to split the string into pieces by using the “template” functionality of home assistant but I don’t know how to get this values to the entities. Is this the correct way to do this? or is there a better way (like scripts)?

thanks.

Can you explain what that data represents and how you want to use it in Home Assistant? It will help us determine the best way to handle it.

An automation can subscribe to the topic, parse the received payload, and re-publish each part to separate topics that are used by various MQTT Sensors. Alternately, each MQTT Sensor can subscribe to the same topic and extract the part of the received payload it needs.

These values represents parameters from an audio system.
*STR, zone, volume, balance, high tones, low tones, source, mute;
I want to “update” certain entities with this values and I also want to use these entities to sent different MQTT commands to the audio system (for example to change the volume).

The following automation takes this payload:

*STR,3,30,50,50,50,2,1;

and re-publishes each one of the seven values to a separate MQTT topic.

- alias: example 111
  trigger:
    platform: mqtt
    topic: test/audio
  action:
    - variables:
        data: "{{ trigger.payload[5:-1].split(',') }}"
        item: ['zone', 'volume', 'balance', 'high_tones', 'low_tones', 'source', 'mute']
    - repeat:
        count: '{{ item | length }}'
        sequence:
        - service: mqtt.publish
          data:
            topic: 'test/audio/{{item[repeat.index-1]}}'
            payload: '{{data[repeat.index-1]}}'

Here’s a screenshot from MQTT Explorer:

Screenshot from 2020-12-19 11-34-06

What remains is to define seven MQTT Sensors, one for each of the seven audio parameters, and subscribe each sensor to its own MQTT topic. For example:

sensors:
- platform: mqtt
  name: Audio Zone
  state_topic: test/audio/zone
- platform: mqtt
  name: Audio Volume
  state_topic: test/audio/volume
- platform: mqtt
  name: Audio Balance
  state_topic: test/audio/balance

... etc ...

If you don’t want to use the technique of an automation generating separate MQTT topics, then each MQTT Sensor will need to subscribe to the same topic and extract what it requires from the received payload.

sensors:
- platform: mqtt
  name: Audio Zone
  state_topic: test/audio
  value_template: "{{ trigger.payload[5:-1].split(',')[0] }}"
- platform: mqtt
  name: Audio Volume
  state_topic: test/audio
  value_template: "{{ trigger.payload[5:-1].split(',')[1] }}"
- platform: mqtt
  name: Audio Balance
  state_topic: test/audio
  value_template: "{{ trigger.payload[5:-1].split(',')[2] }}"

... etc ...
2 Likes

I noticed that the use of sensors was not the right way to go for me.
I now created MQTT switches with templates on it and MQTT sliders with automations on it to split and use the correct values.

for example I created this switch to mute a zone:

switch:
  - platform: mqtt
    unique_id: allegretto_zone1_mute
    name: "Music zone 1"
    icon: mdi:power-standby
    #send commands
    command_topic: "Audio/Commando"
    payload_on: "*MUT,99,99,1,0,00;"
    payload_off: "*MUT,99,99,1,1,00;"    
    #receive updates
    state_topic: "Audio/Status"
    value_template: >-
      {% if value[5:-1].split(',') [0] | int  == 1 and value[5:-1].split(',') [6] | int  == 1 %}
        mute_on     
      {% elif value[5:-1].split(',') [0] | int  == 1 and value[5:-1].split(',') [6] | int  == 0 %}
        mute_off
      {% endif %}
    state_on: "mute_off"
    state_off: "mute_on"
    optimistic: false

It’s working fine now.

Hi,
I want to send the values of selected sensors from HA to Arduino.

Automation:

alias: MQTT Send to Tasmota - Arduino.
description: ‘’
trigger:

  • platform: time_pattern
    seconds: /20
    condition: []
    action:
  • variables:
    item: [‘data00’, ‘data01’]
    data: string( value_json.sensor.elektrokotel_localtuya_total_energy | int),
    value_json.sensor.elektrokotel_localtuya_prikon | float
  • repeat:
    count: ‘{{ item | length }}’
    sequence:
    - service: mqtt.publish
    data:
    topic: TasmotaPLC/SerialSend
    payload: ‘{{item[repeat.index-1],data[repeat.index-1]}}’
    mode: single

I’ve been worried about this for a long time and the MQTT explorer shows me it’s gone. But these are not the expected data “A” and “r” without values.
What am I doing wrong? Thank you :slight_smile: