Removing a list item from a list

Is there is a way to remove a list item from a list in a template?

for example:

I need to send a list to a target in a script.

depending on certain conditions I want the script to act on all list items. if those conditions aren’t met then I only want the script to act on certain list items. Specifically in this case I need to remove only 1 item from the list. the list won’t always contain the same items or necessarily be in the same order ([a,b,c] or [b,c,a] or [c,b] but I will always want to remove c).

Here is the automation service call:

action: script.turn_on
data:
  entity_id: script.notification_pushover_message_exclusions
  variables:
    target: ['me', 'work', 'wife'] # or it could be ['wife', 'me', 'work']
    message: 'Testing.'
    sound: gamelan

here is the action of the script I’m trying to use:

- if:
    - "{{ 'work' in target and states('person.me') == 'At Work' }}"
  then:
    - service: notify.pushover
      data:
        target: "{{ target }}"
        message: "{{ message }}"
        data:
          sound: "{{ sound }}"
          priority: 0
  else:
    - service: notify.pushover
      data:
        target: "{{ target }}" ## <-- remove 'work' from the list of targets
        message: "{{ message }}"
        data:
          sound: "{{ sound }}"
          priority: 0

I tried to use the python “remove” function but it gave me an error in the template editor and it looks like it’s not allowed to use it:

is there another way to accomplish what I’m trying to do?

Paste this in the developer tools template tester:

{{ ['a','b','c'] | select('!=','b') | list }}
1 Like

You might as well do the if/then in the template:

- service: notify.pushover
  data:
    target: |
      {% set at_work = 'work' in target and states('person.me') == 'At Work' %}
      {{ target if at_work else target | reject('eq', 'work') | list }}
    message: "{{ message }}"
    data:
      sound: "{{ sound }}"
      priority: 0
1 Like

thanks to both of you for the solutions. I think both will work for the intended use.

I would give Edwin the solution tag since he gave me the first and most direct solution to the problem but for some reason he doesn’t have the checkmark box option in his reply (or in drew’s either). Has that gone somewhere else?

But I think I’ll actually use Drew’s solution in practice since it simplifies the logic.

thanks again to both of you.

2 Likes

Because you forgot to categorise your topic. Uncategorised topics do not have that feature.

Moved and marked as solved.

1 Like

Ah, ok. Thanks for fixing it for me.

1 Like

Yet another way to do it is to use the recently added difference function.

Copy-paste the following into the Template Editor and experiment with it.

{{ difference(['me', 'work', 'wife'], ['work']) }}

For your application, the template is simply this:

{{ difference(target, ['work']) }}
1 Like

I forgot about that one.

I’ve used that function before in a different situation to compare lists. But I didn’t think about using “work” as a list of one item to compare.