Help with zone cleaning parameters - Xiaomi vacuum

Is anyone else having this issue - I have a card setup with pause and a start clean button.
service: vacuum.pause & service: vacuum.start

In zone cleaning - pause and then sending vacuum.start works to resume the cleaning where it left off

Now I am using Room/segmented Cleaning and although pause still works - vacuum.start starts a brand new clean house cycle - so current room clean is ignored ? Have tried double pause but it doesn’t work. Any suggestions.

Hi Piotr,

Usage of templates inside params for service vacuum.send_command is something i was looking for some time now. Your script seems to be an answer for my need. Point is, that it works only for zone cleaning while I’m using app_segment_clean where each specific room is defined as a number - see below. Would it be possible for you to adjust you script for such configuration? What I need as an input for params is a list of specific numbers. I would appreciate your support! Thanks!

image

Hi!

My script is already adjusted to support app_segment_clean, but links to GitHub are altered by this forum to point to a version that is latest at the time of posting, so you just have to go to the latest version of the file to find features you need :wink:

Hi,

Thanks for feedback! I have found it, but it seems to be too complicated for me as a newbie. I have no idea how my template should look like to generate proper input for params.
I was trying with below, but no success:

data_template:
  command: app_segment_clean
  entity_id: vacuum.xiaomi_vacuum_cleaner
  params: '    
  {% if is_state("input_boolean.kuchnia", "on")  -%} 20 {%- endif %}    
  {% if is_state("input_boolean.salon", "on")  -%} 16 {%- endif %}    
  {% if is_state("input_boolean.korytarz", "on")  -%} 19 {%- endif -%}    
  {% if is_state("input_boolean.pokoj_leny", "on")  -%} 17 {%- endif %}    
  {% if is_state("input_boolean.lazienka", "on")  -%} 21 {%- endif %}    
  {% if is_state("input_boolean.sypialnia", "on")  -%} 18 {%- endif %}'

Try something like this:

data_template:
  command: app_segment_clean
  entity_id: vacuum.xiaomi_vacuum_cleaner
  params: >
    {% if is_state("input_boolean.kuchnia", "on")  -%} 20 {%- endif %}    
    {% if is_state("input_boolean.salon", "on")  -%} 16 {%- endif %}    
    {% if is_state("input_boolean.korytarz", "on")  -%} 19 {%- endif -%}    
    {% if is_state("input_boolean.pokoj_leny", "on")  -%} 17 {%- endif %}    
    {% if is_state("input_boolean.lazienka", "on")  -%} 21 {%- endif %}    
    {% if is_state("input_boolean.sypialnia", "on")  -%} 18 {%- endif %}

Nope, still nothing. When pasting it into HA templates seems to be OK, but there is an empty line created when some of the rooms are omitted - see below. Could it be the issue? Is your python script designed to work with such formatted data as an input?

Ok, I have the ultimate solution (in my opinion):

  1. Add attribute room_number for all your input_booleans (documentation):
    homeassistant:
      customize:
        input_boolean.kuchnia:
          room_number: 20
        input_boolean.salon:
          room_number: 16
        input_boolean.korytarz:
          room_number: 19
        input_boolean.pokoj_leny:
          room_number: 17
        input_boolean.lazienka:
          room_number: 21
        input_boolean.sypialnia:
          room_number: 18
    
  2. Create a group with all input_numbers:
    group:
      room_input_booleans:
        entities:
          - input_boolean.kuchnia
          - input_boolean.salon
          - input_boolean.korytarz
          - input_boolean.pokoj_leny
          - input_boolean.lazienka
          - input_boolean.sypialnia
    
  3. Use the following code in your script/automation:
    - service: python_script.vacuum_send_command
      data_template:
        command: app_segment_clean
        entity_id: vacuum.xiaomi_vacuum_cleaner
        params: "{{ 'group.room_input_booleans' | expand | selectattr('state', 'equalto', 'on') | map(attribute='attributes.room_number') | list }}"
    

If you don’t like this approach, you can do everything directly in a script:

- service: python_script.vacuum_send_command
  data_template:
    command: app_segment_clean
    entity_id: vacuum.xiaomi_vacuum_cleaner
    params: >
      {%- set room_numbers = {
        'input_boolean.kuchnia': 20,
        'input_boolean.salon': 16,
        'input_boolean.korytarz': 19,
        'input_boolean.pokoj_leny': 17,
        'input_boolean.lazienka': 21,
        'input_boolean.sypialnia': 18,
      } -%}
      [{%- for room_name in room_numbers | expand | selectattr('state', 'equalto', 'on') | map(attribute='entity_id') | list -%}
      {{ room_numbers[room_name] }}
      {%- if not loop.last %},{% endif -%}
      {%- endfor -%}]
1 Like

Currently, whats the easiest way to start a Zoned Cleanup with Valetudo?

I have my zones configurd on my robot, in the past this would work:

service: vacuum.send_command
entity_id: vacuum.pitbull
data:
  command: zoned_cleanup
  params:
    zone_ids:
      - kitchen

I can trigger it without error but the vacuum does not move. Help please!

The entity_id has to be inside the data area.

Hm, I tried with:

service: vacuum.send_command
data:
  entity_id: vacuum.pitbull
  command: zoned_cleanup
  params:
    zone_ids:
      - kitchen

and (like HA suggests with sample data)


service: vacuum.send_command
data:
  entity_id: vacuum.pitbull
  command: zoned_cleanup
  params:
    zone_ids: kitchen

Does not work. What is wrong?

Something is no yes :wink: I have followed all steps:

  1. python script created:
entity_id = data.get('entity_id')
command = data.get('command')
params = str(data.get('params'))
parsedParams = []

for z in params.replace(' ', '').replace('],[', '|').replace('[', '').replace(']', '').split('|'):
    rect = []
    for c in z.split(','):
        rect.append(int(c))
    parsedParams.append(rect)

if command in  ["app_segment_clean"]:
    parsedParams = parsedParams[0]

hass.services.call('vacuum', 'send_command',
                   {'entity_id': entity_id, 'command': command, 'params': parsedParams}, True)
  1. groups created:
room_input_booleans:
   entities:
      - input_boolean.kuchnia
      - input_boolean.salon
      - input_boolean.korytarz
      - input_boolean.pokoj_leny
      - input_boolean.lazienka
      - input_boolean.sypialnia  
  1. New attribute for input_boolean created:
editable: true
friendly_name: Korytarz
icon: 'mdi:wall'
room_number: 19
  1. Automation created:
- id: '1600287204887'
  alias: odkurzanie_selektywne
  description: ''
  trigger:
  - entity_id: input_boolean.slij_mariusza
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - data_template:
       command: app_segment_clean
       entity_id: vacuum.xiaomi_vacuum_cleaner
       params: "{{ ''group.room_input_booleans'' | expand | selectattr(''state'', ''equalto'', ''on'') | map(attribute=''attributes.room_number'') | list }"
    service: python_script.vacuum_send_command
  - delay: 00:00:05
  - data: {}
    entity_id: input_boolean.slij_mariusza
    service: input_boolean.turn_off
  mode: single

And got an error when checking configuration:

  Invalid config for [automation]: invalid template (TemplateAssertionError: no filter named 'expand') for dictionary value @ data['action'][0]['data_template']['params']. Got None. (See /config/configuration.yaml, line 155). 

Any idea what went wrong?

I am not sue if quotes in params are correct…
Try something like this:

  - data_template:
       command: app_segment_clean
       entity_id: vacuum.xiaomi_vacuum_cleaner
       params: "{{ 'group.room_input_booleans' | expand | selectattr('state', 'equalto', 'on') | map(attribute='attributes.room_number') | list }"

Problem solved!

I have pasted an error into google and it guided me to:

https://community.home-assistant.io/t/how-to-use-expand-function-in-sensor-template-to-sum-all-state-of-a-group/136791/5

I’m not that good with coding but idea to use expand ('group.room_input_booleans') instead of 'group.room_input_booleans' | expand | is solving the issue. My automation is now working. Thanks for your effort @3_14( Piotr) :slight_smile: You are my HERO! :wink:

@EdwardEnglish where you able to solve this issue?
I am trying to corner the issue piece by piece. First I wanted to see if vacuum.send_command is working correctly. If I use Developer Tools to call a service.
vacuum.start for example works perfectly fine.
Shouldn’t I get the same result using

service vacuum.send_command
entity_id: vacuum.rockrobo
command: start

Happy for any input.

1 Like

Unfortunately not…never found the time to give it more tries. But if anybody ever finds a solution please post it!

Have a look here, I solved it using the new 0.117 template as list feature and can send it directly to the vacuum without using any python script at all.

https://community.home-assistant.io/t/a-list-generated-by-a-template-is-it-possible-help/111239/35?u=hagenuck1

And also in 0.118 with Native types support for templates

Do you mind sharing the final code for this please?

I have tried using the python script method but get the following error:

 File "vacuum_send_command.py", line 9, in <module>
ValueError: invalid literal for int() with base 10: "'19'"

Seems like an issue with the room_number attribute, which I can confirm exists for the room I am trying to clean.

Ignore, got it to work by mapping to integers first.

here is the code, seems to work well now!

    - service: python_script.vacuum_send_command
      data_template:
        command: app_segment_clean
        entity_id: vacuum.robomac
        params: "{{ expand('group.vc_rooms') | selectattr('state', 'equalto', 'on') | map(attribute='attributes.room_number') | map('int') | list }}"

With the latest valetudo update it broke my vacuum.send_command stuff but I was able to get it working using MQTT publish like so.

service: mqtt.publish
data:
    topic: valetudo/robovacDown/MapSegmentationCapability/clean/set
    payload: '20'

One thing I’ve not been able to figure out yet is how to provide a payload of multiple segments to be cleaned though. Say like payload: ['20', '21', '22'].

EDIT: Figured it out. 2021.06 - Home Assistant segment clean help · Discussion #989 · Hypfer/Valetudo · GitHub