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)?
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).
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:
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.
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
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