MQTT Cover - how to split position payload?

Hi,
I like to set up my system so that there is some kind of “subsystem”, that manages the complete control for 8 window blinds/covers. It controls the switches, calculates positions and so on.

This subsystem shall communicate to my HA instance.
It will report the position of all 8 covers in one single MQTT message as a string where the individual positions are separated by a separator character (’;’ for example).
This means the position message sent by the subsystem to my HA instance will look like this:

home/windows/covers/positions/[12;87;100;69;0;47;78;55]

Now my python skills are less than limited. I think I can extract the position for the individual covers by a template. But I just don’t know how.
This is my current config for 2 of the 8 covers:

- platform: mqtt
  name: "first"
  command_topic: "first/window/cover/position/set/"
  position_topic: "home/windows/covers/positions/"
  set_position_topic: "first/window/cover/position/set/"
  payload_open: "100"
  payload_close: "0"
  payload_stop: "-1"
  state_open: "100"
  state_closed: "0"
  position_template: |-
  { here is where I don't know how to extract the FIRST position (12) from the payload }

- platform: mqtt
  name: "second"
  command_topic: "second/window/cover/position/set/"
  position_topic: "home/windows/covers/positions/"
  set_position_topic: "second/window/cover/position/set/"
  payload_open: "100"
  payload_close: "0"
  payload_stop: "-1"
  state_open: "100"
  state_closed: "0"
  position_template: |-
  { here is where I don't know how to extract the SECOND position (87) from the payload }

Might be just a single python line - but currently still beyond my knowledge… :confused:

Thanks and greetings!

Use comma as seperator ( [12,87,…]) and then try

position_template: {{ value_json[0] }}

and

position_template: {{ value_json[1] }}

I’ll think that will work, as “value_json” contains the JSON interpreted value of the topic (an array, if you use comma instead of semi-colon).

1 Like

Awesome, thanks for the fast response!
I think I’ve got your point regarding json. That sounds reasonable. I think I will have to remove the “[ ]” from my payload, right?

And secondly: how would it look like if I want to split it up by any other character or even a word? Maybe

position_template: "{{ value.split(',')[1] }}"

or so?

Okay, I was too excited and installed mosquitto_pub and gave it a try. Both solutions work for my case. Thank you again!

@m0wlheld what would you prefer to use now that I can confirm that both solutions do their job? I just ask because I just don’t know the advantages or disadvantages of both ways…

value.split is flexible, but requires more effort (e.g. if the values are strings or contain the separator character by themself).

value_json covers all of this out of the box if you use comma as separator.

Perfect, thank you! Solved :slight_smile: :+1: