It’s impossible to complete a task in todo list using a jinja template, when the name of the task ends with a space.
For example let’s say that I have a todo list, and its tasks are stored in a list (I have an entity that has that list as an attribute), such as
[take your pill ,brush your teeth,training]
then updating the task to complete it by using this as the item
{{ list[1] }}
will work. But this will not work:
{{ list[0] }}
as the jinja removes the space at the end and then there is no matching with any tasks, so there’s an error.
I have been unsuccessful at trying to find a nice workaround.
I know I could go and:
Retrieve all the tasks in the todo entity with todo.get_items
Perform a loop to find the task that contains the result of list[0] (reasonably there would be just one task fitting the criteria but it’s not logically guaranteed)
Complete the found task
But how horrible and unefficient is that?
I’d prefer to have the jinja template doing the workaround without having to loop over the todo.
If you make it so that your final output includes quotes around it, whitespace should not be folded. As you haven’t posted any code I can’t give you a direct solution but one option would to simply use the to_json filter.
So can you update the list item at all, if you hardcode the string? And if so does that work without quotes (just a trailing space), with quotes, both, neither…?
item: take you pill ← with a space at the end item: "take you pill " ← with a space within quotes
action: todo.update_item
data:
item: take your pill ← with a space at the end
status: completed
target:
entity_id: todo.mytasks
This works:
action: todo.update_item
data:
item: "take your pill " ← with a space within quotes
status: completed
target:
entity_id: todo.mytasks
The issue comes when its within a template.
Very obviously I can just make sure no task ends with a space. But still I would like to know how to get a jinja template working the way I would expect: whatever string is in the list, if I retrieve the element using an index number, I should get the element exactly as it was, no more no less.
I suspect you’re out of luck. If you put this into the Template tab of Dev Tools, you get no leading or trailing spaces. However, if you put this same thing into J2Live and select “Show whitespaces” you can see they’re all preserved - so it’s not default Jinja behaviour.
Therefore I would assume HA is trimming the result of the template.
Seems like a catch 22. You need quotes to preserve the trailing whitespace, but quotes apparently aren’t stripped from template output.
Here is a thread about a similar problem, preserving leading whitespace. Seems like the consensus is that this is not possible.
As far as I can tell from the documentation, there is no other way to refer to to-do items than by their name/label, no id or index or anything. So I don’t see how the above would be possible either?
At least not through normal HASS automation actions. Maybe you can do it by executing the action some different way, like say through the REST API, command line or Python.
Edit: Seems the todo.get_items action does return a UID for each todo item. Even though the documentation makes zero mention of it, the UID can be fed into todo.update_item as its item parameter. Seems like this is the only possible workaround.
The HA template engine automatically trims strings. However, if you template the entire data field, it won’t do that as your result is a mapping/dictionary and not a string.
If not mistaken, the issue here is that there is already an item on the todo list with a trailing space. and the error occurs when they try to mark it as completed using a template (as the template engine will remove the trailing space when the template is rendered)
So if the trim filter should be used, it is when adding the items.
@Dablidou how do you add the list items? Are you using voice? In that case you could create an issue as well, as I would expect the input to be trimmed before they are added to the list.
The item is added automatically by the Habitica integration. The space is simply added by the user from the Habitica app, probably because of the autocorrect. Thank you for your solution. I didn’t want to rely on “just don’t put spaces at the end of the tasks”.
This being said this is a very very strange behavior of the template implementation. But my problem’s solved. Thank you!