Array to let my Roomba clean

Hi, is there a way to define an array?

I am trying to clean my rooms with my Roomba, The roomba wants all rooms that need to be cleaned in a single command.
roomba.sendCommand(<MAPID>;<roomID>;<roomID>;<roomID>;<roomID>)

If i only want two rooms to be cleaned, the array only should have 2 “slots”. If i want 6 rooms to be cleaned, it should contain 6 “slots”.

I want to automate this with my sitemap. So if i click on one room in my sitemap and choose “CLEAN ROOM”, it should be always the last room to be cleaned.

Is this somehow possible?

What the array needs to be able to do:

Add new roomIDs always at the end
being able to delete a roomID in “slot 2” when “slot 1” and “slot 3” contains a roomID too.
“Slot 3” should drop to “Slot 2” when “Slot 2” has been deleted.

I never worked with arrays before, so i have no idea how to use them and work with them properly.

Would love to get some help <3

Ultimately, this is an XY Problem. You’ve jumped to a solution (“arrays”) without considering alternatives.

You don’t provide enough details to actually answer this question though. What version of OH? What language for Rules? What type of Item is roomba? What does the binding say it accepts as commands?

I’d guess the Item needs to be a String Item which means your “array” actually needs to be a single long String. That significantly changes the overall approach, no arrays required.

Because you are using sitemaps your options are limited. I do something like the following:

  1. Create a Group Item to hold all the room Items
  2. Create a Switch Item for each room and make them members of the Group created in 1
  3. Create a String Item to hold the command that you’d send to roomba with the list of rooms
  4. Create a rule triggered by Member of RoomsGroup changed where RoomsGroup is the Group created in 1. This rule will loop through the members of the Group created in 1 and build up a String listing the rooms whose Switch Item created in step 2 is ON. Update the String Item created in step 3 with this String.
  5. Some other event (Switch?) triggers a rule when it’s time to clean. This rule sends the state of the String Item created in step 3 to roomba.

Wow, thank you very much. I will try my best to get this to work. Otherwise i will come back and beg for some more help. Thank you :slight_smile:

Isn’t it easier to check the members of the group for those that are switched on and with that information build the string to send as command in the rule mentioned in step 5? Something like

var roomList = items.getItem('RoomGroup').members
    .filter(item => item.state == 'ON')
    .sort((a, b) => {
        var lastUpdateA = a.history.lastUpdate().toLocalDateTime();
        var lastUpdateB = b.history.lastUpdate().toLocalDateTime();
        return lastUpdateA.compareTo(lastUpdateB);
    })
    .map(item => item.label)
    .join(';');
items.getItem('RoombaCommand').sendCommand('mapId;' + roomList);

(substitute label with whatever item property the value for the string is stored in)

Edit: I missed the requirement of the order to be kept. For that, if persistence is enabled, sorting the member list by item.history.lastUpdate() would probably work, I think. I updated the (untested) code suggestion accordingly.

EDIT: Ignore me. I misread the paragraph about deleting slots, and now realize that there are more than six options.

If I understand this correctly, you have a limited set of options:

  • If you click on “Room 1”, the vacuum only cleans Room 1
  • If you click on “Room 3”, the vacuum cleans Room 1, Room 2, and Room 3
  • If you click on “Room 6”, the vacuum cleans Room 1, Room 2, Room 3, Room 4, Room 6, and Room 6.

If that’s the case, I think you just need a single rule that sends the correct string, based on whichever room you’ve selected in your sitemap. You’re never going to deviate from the six options, so they might as well be hardcoded into the rule.

I figured you wouldn’t want to command the roomba to start cleaning every time you change the list of rooms to clean. I figured you’d want to have those be separate events so you can choose the rooms and command the roomba independently.

But checking the members to build the String is exactly what is done in step 4. It just stores it in a String Item instead of immediately commanding the robot to run.

Yeah, but why? He said he wants to have a button to start cleaning (thus, a separate switch item or something), which is what’ll trigger step 5, which in turn seems like the perfect time to build the string? Otherwise one needs 2 rules - one to build the string and one to send the command.

Oh, now I understand what you are saying. Yes, I agree, we could save the String Item and build the rooms string in the rule triggered on step 5.

I thought you were saying that we would trigger the cleaning every time a room was selected or deselected.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.