How to rename todo list items that have an empty space at the end?

I have a todo list, and sometimes people in the family accidentally add a trailing empty space on an item, like this:

This is causing issues with finding and removing duplicates in the list, so I want to remove this empty space with an automation, using the todo.update_item to rename it.

This is my attempt:

alias: Remove duplicate items
description: ""
triggers: []
conditions: []
actions:
  - action: todo.get_items
    target:
      entity_id: todo.test_list
    data:
      status:
        - completed
    response_variable: completed_items
  - variables:
      item_to_rename: "Pears "
  - action: todo.update_item
    target:
      entity_id: todo.test_list
    data:
      item: "{{ item_to_rename }}"
      rename: "Pears"
mode: single

Unfortunately it seems that when using "{{ item_to_rename }}" then the trailing empty space is stripped, and the update_item action does not find the item in the list. How can I remove the empty space in the list item?

I have tried using the following:

"{{ item_to_rename | to_json }}"
"{{ item_to_rename | regex_replace('\s+$', ' ') }}"
"{{ item_to_rename + '\u00A0' }}"
"{{ item_to_rename | replace(' ', '\u00A0')}}"
"{{ item_to_rename | escape }}"

But cannot get any of them to work.

The template engine is trying to be a bit overly helpful. If you want it to honor your variables exactly as they are without any modifications or type changes, you can template the entire data object instead of templating each individual item.

Try this:

  - action: todo.update_item
    target:
      entity_id: todo.test_list
    data: "{{ { 'item': item_to_rename, 'rename':'Pears'} }}"
1 Like

I see, thanks for this - it solves the problem.