Simple Zone Cleaning Script for Valetudo Vacuum

Hi Guys,

this is my first post in the forum so I hope I’m doing everything right. Please tell me if not.

Last Week my Xiaomi Robot Vacuum Gen1 finally arrived after 3 month of waiting. After I flashed it with Valetudo I was looking for a way to automate the zone cleaning so the vacuum does not build a new map every time. I could not find a suitable solution for the way I wanted it to be.

I’m using Input_booleans to activate or deactivate each cleaning zone. Than I’m using a rest command to be able to make a correct service call with templating. It is not working to make the service call directly because the templating does not result in correct json. I read the trick with the rest command in another thread to solve a similar problem.

My config is pretty simple and still a work in progress to automate the zone cleaning. For now I have a simple button to call the script.

I hope this has some use for you.

vacuum_zone_cleaning:
  alias: Staubsaugen nach Raum
  sequence:
    - condition: state
      entity_id: group.vacuum_zones
      state: "on"
    - service: rest_command.vacuum_zone_cleaning
	
rest_command:
  vacuum_zone_cleaning:
    url: "http://192.168.2.60:8123/api/services/vacuum/send_command"
    method: POST
    headers:
      content-type: application/json
      authorization: !secret rest_api_token
    content_type: application/json
    payload: >-
      {
        "entity_id": "vacuum.rockrobo",
        "command": "zoned_cleanup",
        "params": 
          {"zone_ids": {{ expand('group.vacuum_zones') | selectattr('state', 'eq', 'on') | map(attribute='name')|list|to_json }}
        }
      }
input_booleans:	 
  vacuum_livingroom:
    name: Wohnzimmer
    icon: mdi:robot-vacuum-variant

  vacuum_bedroom:
    name: Schlafzimmer
    icon: mdi:robot-vacuum-variant

  vacuum_studyroom:
    name: Wohnzimmer
    icon: mdi:robot-vacuum-variant

  vacuum_guestbedroom:
    name: Wohnzimmer
    icon: mdi:robot-vacuum-variant

  vacuum_hallway:
    name: Flur
    icon: mdi:robot-vacuum-variant
      
group:
  Vacuum_Zones:
    name: Staubsaugerzonen
    entities:
    - input_boolean.vacuum_livingroom
    - input_boolean.vacuum_bedroom
    - input_boolean.vacuum_studyroom
    - input_boolean.vacuum_guestbedroom
    - input_boolean.vacuum_hallway
2 Likes

The example is great, but there is a problem here, it’s encoding. If you write in English, everything will work well, but if you write not in English, there will be problems with encoding, so you need to use UTF-8 encoding.

There is a solution for this. Integration custom_filters. This integration of custom_filters will allow you to encode the text of your language into readable text.

  1. Install the integration via HACS
  2. Add the custom_filters line to configuration.yaml
    image
  3. Restart the Home Assistant
  4. In the template, we use to_ascii_dcon and check whether it should encode characters into text in a language that we understand
"zone_ids": {{ expand('group.vacuum_zones') | selectattr('state', 'eq', 'on') | map(attribute ='name') |list| to_ascii_json }}

"zone_ids": {{ expand('group.vacuum_zones') | selectattr('state', 'eq', 'on') | map(attribute ='name') |list| to_json }}

image

Текст на русском языке

Пример отличный, но тут есть проблема, это кодировка. Если писать на английском языке, то все будет работать хорошо, но если писать не на английском языке, то будут проблемы с кодировкой, поэтому нужно использовать кодировку UTF-8.

Для этого есть решение. Интеграция custom_filters. Данная интеграция custom_filters позволит кодировать текст своего языка в читаемый текст.

  1. Устанавливаем интеграцию через HACS
  2. Добавляем в configuration.yaml строчку custom_filters:
    image
  3. Перезагружаем Home Assistant
  4. В шаблоне используем to_ascii_json и проверяем, должно кодировать символы в текст на понятном нам языке
"zone_ids": {{ expand('group.vacuum_zones') | selectattr('state', 'eq', 'on') | map(attribute ='name') |list| to_ascii_json }}

"zone_ids": {{ expand('group.vacuum_zones') | selectattr('state', 'eq', 'on') | map(attribute ='name') |list| to_json }}

image