Split MQTT payload and send values to entities

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