Hi I have a script that sends my vacuum cleaner to the kitchen by including a list of numbers in the data section. The numbers represents the coordinates of two opposite corners in the room and how many repetitions the vacuum cleaner should clean it. I want to be able to replace the number of repetitions in my script with a variable.
Hi, thanks for the suggestion. I tried to replace one of the entries with: - '{{30550|int}}'
Unfortunately it gave the same result. Still quotes in the log.
Jinja templates return strings. Not much you can do about it. Usually this isn’t an issue because the service schema will coerce certain parameters to int or float or whatever. However, I just looked at the vacuum service handler, and it doesn’t do this for send_command’s “params” parameter, probably because it can be various types.
Probably the only way to resolve this is to change the platform code. What vacuum platform are you using?
Based on the entity_id, I assume you’re using the xiaomi vacuum platform. I looked over that code, as well as the miio library it uses, and since you’re using a raw command, there’s no code specific to this app_zoned_clean command that can be enhanced to force the elements of the “params” list to be integers.
Having said that, you could modify the generic async_send_command method to do this if command == ‘app_zoned_clean’. This is the method:
@asyncio.coroutine
def async_send_command(self, command, params=None, **kwargs):
"""Send raw command."""
yield from self._try_command(
"Unable to send command to the vacuum: %s",
self._vacuum.raw_command, command, params)
You could do something like this:
@asyncio.coroutine
def async_send_command(self, command, params=None, **kwargs):
"""Send raw command."""
if command == 'app_zoned_clean':
params = [int(param) for param in params]
yield from self._try_command(
"Unable to send command to the vacuum: %s",
self._vacuum.raw_command, command, params)
One question, though. In your YAML code, you’re making params a list of list of numbers. Did you mean to do that? Or should it just be a list of numbers? If it does indeed need to be a list of list of numbers, then the code suggestion above would need a slight modification.
So, turns out, the miio library does indeed have a method specifically for app_zoned_clean. So, the best solution is to enhance the xiaomi vacuum platform code (ziaomi_miio.py) to add a corresponding method and service schema which would coerce the parameters into int’s. If this is really important to you, and you’re not comfortable doing that yourself, I could give it a shot, but it would probably have to wait until after the weekend.
Tried to call zoned_clean instead but it did not work. I think it needs to be a list of lists with numbers to work. The point of the second list is to be able to send the cleaner to multiple zones in one command.
I’m having the same issues.
When calling a service with data: is no problem, but when using data_template i’m getting errors the output is not an integer.
ERROR (MainThread) [homeassistant.core] Invalid service data for dreamscreen.set_brightness: expected int for dictionary value @ data[‘brightness’]. Got ‘13’
I’m sure it’s the same issue - the service schema doesn’t coerce the value to an int. Jinja templates output strings, you can’t avoid it as far as I know. So the code has to handle that situation. If it doesn’t, then the code has to be fixed. That’s my understanding.
Thanks @pnbruckner. That’s also my understanding. I’ve analysed my logs for Xiaomi vacuum service call, where I also use a template and it turns out the whole value is passed as ['[1,2,3,4,5]']
rather than [[1,2,3,4,5]]
The reason is that the whole expression is output as string with quotes. I’ve tried playing with the |tojson jinja filter, but it doesn’t help much
Has anyone filed a bug in guthub for this?? I’m asking as I’m willing to do so, so some dev could check this out.
Today I managed to solve the problem. The bug I filed wasn’t even touched by anyone and this fact forced me to dig in HA’s code, the Xiaomi vacuum component and this thread again.
I missed it when I saw this thread first, but what TENFIRE implemented in Aug '18 solves the problem. Now this piece of code is changed a bit, but it basically solves it!
To parametrize zone cleaning it’s enough to switch from the send_command service (which sends a raw command to the vacuum) to the xiaomi_clean_zone service which supports templating correctly!!
This is the key thing here. Most of the sources in the web are still referring to the “old” send_command service, which doesn’t support templating correctly.
So for all of you seeking the solution in the future - an example:
FWIW, the issue was not templating. Templating happens before the service is even called, and is the same for both services.
The issue was the service schema, or in this case, the lack of an appropriate one. The schema is a data structure (in the implementation) that describes what a service’s data should look like, and can force values to be converted to different types, etc. In this case, the fix was adding a new service, that included an appropriate service schema (that converts the string representations of numbers that come out of the template to ints), and which calls the appropriate library method.
But, basically, yes, @TENFIRE’s fix solved the problem.