Roborock (xiaomi vacuum robot) move away from charger when reaching 80% charge

820 views
Skip to first unread message

red_gooogle

unread,
Mar 1, 2021, 4:08:10 PM3/1/21
to Routix
Hi everyone,
I am trying to preserve the battery of my new robot. It should move away from the charger when the charge has reached 80% and return to the charger when the battery drops below 30%.

The topic: valetudo/rockrobo/state
returns: {"state":"idle","battery_level":93,"fan_speed":"mop"}

I subscribed to this topic on mqtt dash.

The topic: valetudo/rockrobo/custom_command
{ "command": "go_to", "spot_id": "Somewhere" }
Sends the device to the specified pre-saved spot location

The topic: valetudo/robot/command
payload: return_to_base

Makes the device returning to charging station.

I set up the script like this. on receive:

if (event.payload < 31) {
app.publish('valetudo/robot/custom_command',{"command": "go_to",
"spot_id": "No charging"}, false, 0);
} else if (event.payload > 80) {
    app.publish('valetudo/robot/command', 'return_to_base', false, 0);
} else {
    app.publish('messages/alert', 'NORMAL', false, 0);


The else could maybe completely left out?
The script does not seem to work. The payload returns more than the number as I showed above. Maybe that is the problem here? I am very new to this kind of scripting so any help would be appreciated.

Routix.net

unread,
Mar 1, 2021, 4:17:44 PM3/1/21
to Routix
Looks like you need to send JSON string instead of plain JS object:
app.publish('valetudo/robot/custom_command',JSON.stringify({"command": "go_to", "spot_id": "No charging"}), false, 0);

Also, why you are sending to "messages/alert" here? I suppose this was copied from examples?

app.publish('messages/alert', 'NORMAL', false, 0);

Seems like a bit of mess in your JS code... It's hard to say without knowing what commands to which topics can be accepted by your device.

red_gooogle

unread,
Mar 2, 2021, 12:07:37 AM3/2/21
to Routix
Thanks for the quick reply.
Well, here is the list of the possible commands.

I did not know if the else line containing
app.publish('messages/alert', 'NORMAL', false, 0);
was needed for the script to work, so I left it in for the time being as it does not hurt to publish anything, does it?

Also, the received payload from the robot is this line: {"state":"idle","battery_level":90,"fan_speed":"mop"}
Will it work to use this line of script:
if (event.payload < 30 {  
to read out the number?

Routix.net

unread,
Mar 2, 2021, 12:14:03 AM3/2/21
to Routix
Received payload is JSON string. You can't treat it as number.
Do something like this:
var data = JSON.parse(event.payload);
if (data.battery_level < 30) {
    ...

red_gooogle

unread,
Mar 2, 2021, 7:09:57 AM3/2/21
to Routix
Cheers, it seems to work. I just need to integrate a cooldown timer so that the robot does not move around every couple of minutes. 

This is my code so far:

var data = JSON.parse(event.payload);
if (data.battery_level > 80) {
app.publish('valetudo/robot/custom_command',JSON.stringify({"command": "go_to", "spot_id": "No charging"}), false, 0);
} else if (data.battery_level < 31) {
    app.publish('valetudo/robot/command', 'return_to_base', false, 0);
} else {
    app.publish('valetudo/robot/state', 'all_systems_nominal', false, 0);
}  

red_gooogle

unread,
Mar 2, 2021, 12:29:45 PM3/2/21
to Routix
I introduced new conditions so the robot does not try to go back to the charger when it's already docked. I think the code is complete for now. Many thanks again, for helping me out @
Routix.net!

var data = JSON.parse(event.payload);
if ((data.battery_level > 80) && (data.state == "docked")) {
app.publish('valetudo/robot/custom_command',JSON.stringify({"command": "go_to", "spot_id": "No charging"}), false, 0);
} else if ((data.battery_level < 31) && (data.state == "idle")) {
    app.publish('valetudo/robot/command', 'return_to_base', false, 0);
} else {
    app.publish('valetudo/robot/mate', 'all_systems_nominal', false, 0);
}  

Is Yeri

unread,
Sep 13, 2022, 12:16:56 PM9/13/22
to Routix
hi, i want to use this code too, but i really don't know from which application 
I should run it. I would be grateful if you could give some tips on how to do it.

2 Mart 2021 Salı tarihinde saat 20:29:45 UTC+3 itibarıyla red_gooogle şunları yazdı:
Reply all
Reply to author
Forward
0 new messages