Building templates with one or more data incoming

Hi.
I am trying to make a lovelace with a vacuum, when I touch the room I would like that the Home assistant to add room’s number to list, and finally press button clean and the vacuum clean the select rooms in the list.

I don’t know to add numbers to the list and I don’t know to take this list for to use in the clean’s script.

can someone help me, please?

Thanks

You can’t make temporary variables. And to top it off, you’ll run into a type issue when trying to call your vacuum service. Typically, something like this is handled with templating but because templates only return a string, you get yourself into murky waters. Your service call expects a list. So, the only option you have is a python script.

input_boolean:
  room1:
  room2:
  room3:
  room4:

vacuum_rooms.py

# Map input_boolean to room number
room_entities = {
  'input_boolean.room1': 16,
  'input_boolean.room2': 19, 
  'input_boolean.room3': 20,
  'input_boolean.room4': 21,
  }
ret = [] # make a list that we will add numbers to
for entity_id, number in room_entities.items():  # iterate through entity_id & number pairs
  state = hass.states.get(entity_id) # get the current entity_id's state object
  if state != None and state.state == 'on': # if we have a state object, and it's state is 'on'...
    ret.append(number) # add the number to a list

# make data that will be sent to the service.
data = {'command': 'app_segment_clean', 'entity_id': 'vacuum.roborock', 'params': ret }
hass.services.call('vacuum', 'send_command', data) # call the service

Then just make sure each item in your picture-elements toggle one of the input booleans. You’ll have to tailor the script to your room numbers and input_boolean names.

To call the python script on your Lovelace button, the tap action should be:

tap_action:
  action: call-service
  service: python_script.vacuum_rooms

You will need to check out the python_script integration to properly add it to your HA configuration.

1 Like

Thanks for you answer, I don’t have enough knowledge but I going to study this solution.

I have studied this code and could never have done it.
I’m afraid my knowledge is very, very poor and I need your help to finish this project.

I write this in configuration.yaml and I add “vacuum:”, it is more correct to write “vacuum:” to separate from other possible projects?

image

Have I to leave empty or write in the vacuum_rooms.py all the rooms too?

image

The file vacuum_rooms.py, in which directory do I have to put it?

And finally, how do I do than the picture-elements toggle one of the input booleans

Thanks for your time and patient

Your input boolean configuration is incorrect. Compare your input_boolean configuration to mine. You’ll notice that you have extra fields and you’re mapping fields to numbers. That’s only done in the python_script.

Go to the integrations page and search python_script. Find the documentation and read how to integrate it.

I don’t know what you mean here.

Sorry I try to learn the most possible quick

I have erased the numbers in my input boolean, should the input boolean is into the configuration.yaml?

image

I have installed pyscript from hass and I have read where I have to make the folder and put the file.

And the last question, the name of the Lovelace button should be the same than describe in the python script?, because I don’t understand how the scrypt take de correct number and put into de “ret

After the last botton “clean” call de python script with all the numer select. ¿no?

tap_action:
action: call-service
service: python_script.vacuum_rooms

thank you very much

Are you simply abbreviating the word python_script or did you install a completely different integration called pyscript?

There are two integrations, one is called python_script and the other pyscript. Petro’s example requires python_script.

I have resolved my mistake, and I think than I have installed python-script correctly

I have erased the numbers in my input boolean, should the input boolean is into the configuration.yaml or in another file?

image

And the last question, the name of the Lovelace button should be the same than describe in the python script?, because I don’t understand how the scrypt take de correct number and put into de “ret” After than that the last step “botton clean” call the python script with all the numer select. ¿Is correctly?

tap_action:
action: call-service
service: python_script.vacuum_rooms

Well, you can just put input_boolean: in your configuration.yaml, then in the UI you can configure each one in the helpers section. Might be easier.

I’m not sure what you’re saying here. All you need to do is add the proper tap action to whatever control you have for your “Clean” button.

So if it’s a service-button on a picture elements card…

- type: service-button
  title: Clean
  service: python_script.vacuum_rooms

If it’s an image:

- type: image
  image: /path/to/image.png
  tap_action:
    action: call-service
    service: python_script.vacuum_rooms

I’m very happy. I have finally understood all your code and how it works. I have implemented it correctly in HA and it works very well.

Thank you very much for you time and I sorry than you teached to very dificult student.

1 Like

Hi.

I’m sorry to refloat this topic, the solution was perfect but the situation has changed.

Blockquote# Map input_boolean to room number
room_entities = {
‘input_boolean.despacho’: 16,
‘input_boolean.pasillo’: 17,
‘input_boolean.entrada’: 18,
‘input_boolean.cocina’: 19,
‘input_boolean.habitacion_principal’: 22,
‘input_boolean.bano_habitacion_principal’: 21,
‘input_boolean.salon’: 20,
‘input_boolean.habitacion_sara’: 23,
‘input_boolean.habitacion_julia’: 24,
‘input_boolean.bano_pasillo’: 25,
}
ret = # make a list that we will add numbers to
for entity_id, number in room_entities.items(): # iterate through entity_id & number pairs
state = hass.states.get(entity_id) # get the current entity_id’s state object
if state != None and state.state == ‘on’: # if we have a state object, and it’s state is ‘on’…
ret.append(number) # add the number to a list
'# make data that will be sent to the service.
data = {‘command’: ‘app_segment_clean’, ‘entity_id’: ‘vacuum.roborock_s5_max’, ‘params’: ret }
hass.services.call(‘vacuum’, ‘send_command’, data) # call the service

When the map change in the app of Vacuum the number of room change too and in HA lost the configuration. Now I need use zone in this format

Blockquote
‘input_boolean.habitacion_principal’: [30437,26778,34987,30478],

and the command is

Blockquoteservice: xiaomi_miio.vacuum_clean_zone
target:
entity_id: vacuum.roborock_s5_max
data:
repeats: 1
zone:
- 30437
- 26778
- 34987
- 30478

I have tried to adapt the python script but I couldn’t to do.

Thanks for the help.