MQTT Extracting the value y from payload "x, y, z"

I’m trying to set up automation that changes the saturation of a Tasmota controlled RGB LED. I need to know what the current Saturation is - ie have it as a numeric (0-100) value in an Entity. Every 5 minutes, Tasmota reports the State and the JSON payload contains an HSBColor element structured like this with 25 being the saturation in this example

{"first element1:"something", ... ,"HSBColor":"177,25,52", ... "last element":"something"}

I can create an MQTT template entity to pick out the “177,25,52” bit, but how can i just isolate the 25?

Hello Graham Richards,

Does HSBColor[1] find it?
Check in the dev tools - template
Open your Home Assistant instance and show your template developer tools.

Thanks Sir_Goodenough
No. That gives me the second character ie 7 from the example above.
Interestingly, another element in the MQTT message is Channel which has a pair of square brackets around another list of 3 numbers.

{... "HSBColor":"177,25,52","Channel":[49,53,78], ...}

I’m no expert but I think that defines a list and so your idea would work in that case.

Edit - I just checked and your method does work for Channel with its square brackets in the json string. Still haven’t cracked the HSBColor Saturation though!

You can apply .split()[1] to the string value to get the saturation. It will by default split on a comma.

Thanks parautenbach
I tried your suggestion but it’s not working. Maybe I got the syntax wrong.
Here’s what I had in my yaml file to create the entity originally with just the [1] based on Sir_Goodenough’s suggestion. It returned the second character of the string as the entity value

  sensor:
    - name: "Glass Ball Saturation"
      unique_id: sunny_mqtt_60
      state_topic: tele/GlassBall/STATE
      unit_of_measurement: "%"
      value_template: "{{value_json.HSBColor[1]}}"

So, on your suggestion, I changed the value template to

    value_template: "{{value_json.HSBColor.split()[1]}}"

Following the next MQTT message the value remained Unknown.

For context, this is the Glass Ball thing in question. It has 3 RGB LED driven by Tasmota using scheme 2 (which cycles through the colours). I want to change the Saturation at a much slower rate than the colour change.

Edit: I just change it to this and it now works. I needed the comma as the split parameter - perhaps there is no default! Thanks.

    value_template: "{{value_json.HSBColor.split(',')[1]}}"

It seems I had my default wrong. At least I stated my assumption. Still, the core of this was knowing to use split.

1 Like