Valetudo + MQTT + script = json errors?

Hi all. I setup Valetudo last night on my v1 Xiaomi rockrobo and I’m working on getting it integrated with my HA. I’m running into a problem I don’t understand, where publishing to MQTT works but using the same topic and payload in a script does not work. I know I’m doing something wrong but I can’t figure out what it is. Does anyone else know what I’m doing wrong by looking at the info below?

Here’s what I have in the MQTT publish page.

Topic:

valetudo/robot/ZoneCleaningCapability/start/set

Payload:

[
  "261b106a-0175-215b-85c0-2a7b254fd042"
]

When I hit “PUBLISH” on that it works and the vacuum goes along to the zone I’ve chosen and there are no issues.

Here’s what I have in the script:

alias: Vacuum All
sequence:
  - service: mqtt.publish
    data_template:
      topic: valetudo/robot/ZoneCleaningCapability/start/set
      payload: "261b106a-0175-215b-85c0-2a7b254fd042"

No errors in HA when I tell that to run, but here’s what I see inside Valetudo’s logs immediately after hitting run:

[2021-07-28T00:16:00.252Z] [ERROR] unhandledRejection SyntaxError: Unexpected token b in JSON at position 3
    at JSON.parse (<anonymous>)
    at PropertyMqttHandle.setter (/snapshot/Valetudo/backend/lib/mqtt/capabilities/ZoneCleaningCapabilityMqttHandle.js:50:38)
    at PropertyMqttHandle.set (/snapshot/Valetudo/backend/lib/mqtt/handles/PropertyMqttHandle.js:105:27)
    at PropertyMqttHandle.setHomie (/snapshot/Valetudo/backend/lib/mqtt/handles/MqttHandle.js:298:20)
    at Object.topics.<computed> [as valetudo/robot/ZoneCleaningCapability/start/set] (/snapshot/Valetudo/backend/lib/mqtt/handles/MqttHandle.js:82:28)
    at MqttClient.<anonymous> (/snapshot/Valetudo/backend/lib/mqtt/MqttController.js:304:42)
    at MqttClient.emit (node:events:394:28)
    at MqttClient._handlePublish (/snapshot/Valetudo/node_modules/mqtt/lib/client.js:1277:12)
    at MqttClient._handlePacket (/snapshot/Valetudo/node_modules/mqtt/lib/client.js:410:12)
    at work (/snapshot/Valetudo/node_modules/mqtt/lib/client.js:321:12)

I’ve tried adding the payload to the script in the following formats and all give the json error:

payload: "261b106a-0175-215b-85c0-2a7b254fd042"
payload: 261b106a-0175-215b-85c0-2a7b254fd042
payload: "[261b106a-0175-215b-85c0-2a7b254fd042]"
payload: "['261b106a-0175-215b-85c0-2a7b254fd042']"
payload: '261b106a-0175-215b-85c0-2a7b254fd042'

Since the script’s mqtt.publish is getting to the vacuum in order for an error to show up in its logs, I’m assuming the topic is correct and it’s the payload that’s the issue.

Using the publish option on the MQTT page works but attempting to use this script does not and all I get is the above error.

Any idea what I’m doing wrong? First time I’ve tried creating a script in HA so I’m 99% sure it’s my error. Thank you for any help or suggestions!

Have you tried this?

payload: ["261b106a-0175-215b-85c0-2a7b254fd042"]

BTW, data_template was deprecated in favor of data several versions ago. It still works but the preference now is to use data.

I did, and I get an error in HA of:

“Failed to call service script/vac_all_script. value should be a string for dictionary value @ data[‘payload’]”

That’s the only version that outright fails in HA to run the script, the rest of them run but error in Valetudo.

Thanks for the tip on data/data_template! I was just following previous posts online since it’s my first script, so I wasn’t aware of the change.

If it matters, I’m doing my testing on the script inside URL/config/script/edit/script.vac_all_script and not in the scripts.yaml because it’s quicker to make a change and hit run here. I figured if I could get it working here then I’d copy it to the yaml. If for some reason that’s the cause of my problem I’m all ears, but I had tried these variations in the yaml before finding this script/edit page and they also didn’t work there.

Thanks again!

What’s interesting is that the payload I suggested is exactly what you said you used in the MQTT Publish page.

I know, that was actually the second format I tried. The first was copy/pasting it in with the line breaks in there and then I just deleted the line breaks and tried it with the brackets around the quotes.

I don’t understand how any of this works well enough to try to determine why the brackets are acceptable in the MQTT Publish page but fail in the script page, but I get that error every time I try.

Do you also think it’s likely the formatting of the payload that’s the issue? There’s nothing about the formatting of the rest of the script that looks off?

Thanks again!

It’s working! Found the right format for it here:

This is the format I had to use:

payload: '["261b106a-0175-215b-85c0-2a7b254fd042"]'

It needed the single quotes on the outside, square brackets inside, and then double quotes inside that.

Thanks again for all of your help, it looks like I’m all set now!

So it looks like when you published this using the MQTT Publish page:

[
  "261b106a-0175-215b-85c0-2a7b254fd042"
]

it was published as a string (outer quotes added automatically):

'["261b106a-0175-215b-85c0-2a7b254fd042"]'

It also appears that the robot vacuum is particular about the way a string within a list is defined. It requires it to be delimited by double-quotes, as opposed to single quotes, because you had stated this failed (single inner quotes, double outer quotes):

"['261b106a-0175-215b-85c0-2a7b254fd042']"

I don’t get it to work.

This is my script.

vacuum_office_deskarea:
  alias: "vacuum office desk area"
  sequence:
    - service: mqtt.publish
      data:
        topic: valetudo/rambo/ZoneCleaningCapability/start/set
        payload: '["8ef8c724-9038-41d9-929d-845e62995204"]'

This is the automation, that calls the script.

alias: 'vacuum office deskarea'
trigger:
- platform: device
  domain: mqtt
  device_id: 254361051a21c6e614cd50f170f4f82c
  type: action
  subtype: off-press
  discovery_id: 0x0017880104f18637 action_off-press
action:
  service: script.vacuum_office_deskarea

I have another script that uses the segment - ids. It works flawlessly.


vacuum_dining:
  alias: vacuum dining areas
  sequence:
    - service: mqtt.publish
      data:
        topic: valetudo/rocky/MapSegmentationCapability/clean/set
        payload: '{"segment_ids": ["18","22"], "iterations": 1, "customOrder": true}'

So something seems to be faulty in the first script syntax.

I solved it now:
Here is my working script for cleaning a single zone:

 vacuum_office_deskarea:
   alias: "vacuum office desk area"
   sequence:
     - service: mqtt.publish
       data:
         topic: valetudo/rambo/ZoneCleaningCapability/start/set
         payload: 8ef8c724-9038-41d9-929d-845e62995204