Using reject in 2 blueprint lists not working

Just wondering if someone could help.

In a blueprint I have 2 inputs that are targets. In the 2 variables, crossover_lights & crossover_night_lights am trying to reject the matching from the two lists. But it is just returning the list.

crossover_lights will return the light_switch list

and

crossover_night_lights will return the night_lights list

no reject just the full list, see below

  input:
    light_switch:
      name: Lights - Switches - Scenes *
      description: Lights
      selector:
        target:
          entity:
            domain: 
              - light
              - switch
              - scene
    night_lights:
      name: Night Lights
      description: Night Lights
      default: {}
      selector:
        target:
          entity:
            domain: 
              - light
              - switch
              - scene

variables:
  light_switch: !input light_switch
  night_lights: !input night_lights

  crossover_lights: >-
    {% set a = [light_switch] %}
    {% set b = [night_lights] %}
    {% if a and b %}
      {{ (a | reject('in', b) | list) | join(', ') }}
    {% else %}
      []
    {% endif %}

  crossover_night_lights: >-
    {% set a = [light_switch] %}
    {% set b = [night_lights] %}
    {% if a and b %}
      {{ (b | reject('in', a) | list) | join(', ') }}
    {% else %}
      []
    {% endif %}

If I use the developer tool / template it works perfectly how I would like it. See the test code below.

{% set a = [1, 2, 3, 5] %}
{% set b = [1, 3, 4, 6] %}
{% if a and b %}
  {{ (a | reject('in', b) | list) | join(', ') }}
{% else %}
  []
{% endif %}

am I missing something?

  1. Target selectors are complicated. They return a full target object. Depending on what the user selects, you’ll have to change your logic. This will complicate your code.
  2. You’re putting the target object inside a list.

Assuming you still want to use a target selector and you ONLY select entity_ids (Not devices or areas)

variables:
  light_switch: !input light_switch
  night_lights: !input night_lights

  crossover_lights: >-
    {% set a = light_switch.entity_id %}
    {% set b = night_lights.entity_id %}
    {% if a and b %}
      {{ a | reject('in', b) | list }}
    {% else %}
      []
    {% endif %}

  crossover_night_lights: >-
    {% set a = light_switch.entity_id %}
    {% set b = night_lights.entity_id %}
    {% if a and b %}
      {{ b | reject('in', a) | list }}
    {% else %}
      []
    {% endif %}

EDIT: Keep in mind, entity_id will be a list if more than 1 entity is provided or it’ll be a single string if only 1 is provided. Also something you’ll need to work through in your code.

Hi petro, thanks for your suggestions. I tried every thing below and get the same error.

Error: dictionary update sequence element #0 has length 1; 2 is required

  crossover_lights: >-
    {% set a = light_switch.entity_id %}
    {% set b = night_lights.entity_id %}
    {% if a and b %}
      {{ a | reject('in', b) | list }}
    {% else %}
      []
    {% endif %}

# or

  crossover_lights: >-
    {% set a = [light_switch.entity_id] %}
    {% set b = [night_lights.entity_id] %}
    {% if a and b %}
      {{ a | reject('in', b) | list }}
    {% else %}
      []
    {% endif %}

# or

  crossover_lights: >-
    {% set a = light_switch.entity_id %}
    {% set b = night_lights.entity_id %}
    {% if a and b %}
      {{ a | reject('in', b) | list | join(', ') }}
    {% else %}
      []
    {% endif %}

# or

  crossover_lights: >-
    {% set a = [light_switch.entity_id] %}
    {% set b = [night_lights.entity_id] %}
    {% if a and b %}
      {{ a | reject('in', b) | list | join(', ') }}
    {% else %}
      []
    {% endif %}


with this it returns a list but nothing rejected

  crossover_lights: >-
    {% set a = [light_switch] %}
    {% set b = [night_lights] %}
    {% if a and b %}
      {{ (a | reject('in', b) | list) | join(', ') }}
    {% else %}
      []
    {% endif %}

I just can’t seam to work this one out :pensive:

what does the trace show for light_switch and night_lights in the variables section?

@petro

Hi Petro, sorry I had to get some Zzzz… as it was 5am here.

I will try and provide all the info just encase I am doing something wrong. Test 1 will be using my code and Test 2 will be using your code.

# The 2 lists

  light_switch:
    entity_id:
      - switch.family_room_light_2
      - switch.entry_light
      - light.water_tank_1_light

  night_lights:
    entity_id:
      - switch.family_room_light_2
      - switch.outside_front_door_lights

Test 1

# the variables
  crossover_lights: >-
    {% set a = [light_switch] %}
    {% set b = [night_lights] %}
    {% if a and b %}
      {{ (a | reject('in', b) | list) | join(', ') }}
    {% else %}
      []
    {% endif %}

# we have also tried this variables

  crossover_test:  "{{ [light_switch] | reject('in', [night_lights] | list) | join('n\  - ') }}"

#the traces in variables section

  crossover_lights: |-
    {% set a = [light_switch] %} {% set b = [night_lights] %} {% if a and b %}
      {{ (a | reject('in', b) | list) | join(', ') }}
    {% else %}
      []
    {% endif %}

# and

  crossover_test: '{{ [light_switch] | reject(''in'', [night_lights] | list) | join(''n  - '') }}'

# action

                sequence:
                  - alias: Turn off the lights
                    service: light.turn_off
                    target: '{{ crossover_lights }}'
                    data:
                      transition: '{{ transition_off_value }}'
# and
                  - alias: Turn off the switches
                    service: switch.turn_off
                    target: '{{ crossover_test }}'

# step details

params:
  domain: light
  service: turn_off
  service_data:
    transition: 1
    entity_id: 
      - switch.family_room_light_2
      - switch.entry_light
      - light.water_tank_1_light

# and 

params:
  domain: switch
  service: turn_off
  service_data: {}
  target:
    entity_id:
      - switch.family_room_light_2
      - switch.entry_light
      - light.water_tank_1_light

Test 2

# the variables

  crossover_night_lights: >-
    {% set a = light_switch.entity_id %}
    {% set b = night_lights.entity_id %}
    {% if a and b %}
      {{ b | reject('in', a) | list }}
    {% else %}
      []
    {% endif %}

#the traces in variables section

  crossover_night_lights: >-
    {% set a = light_switch.entity_id %} {% set b = night_lights.entity_id %} {%
    if a and b %}
      {{ b | reject('in', a) | list }}
    {% else %}
      []
    {% endif %}

# action

            sequence:
              - alias: Turn off the lights
                service: light.turn_off
                target: '{{ crossover_night_lights }}'
                data:
                  transition: '{{ night_transition_on_value }}'

# step details
# See image below.

8

:thinking:

Is this what your trace shows for those variables?

No they are the lists

this is the trace for your variables

  crossover_night_lights: >-
    {% set a = light_switch.entity_id %} {% set b = night_lights.entity_id %} {%
    if a and b %}
      {{ b | reject('in', a) | list }}
    {% else %}
      []
    {% endif %}

this is the trace for my variables


  crossover_lights: |-
    {% set a = [light_switch] %} {% set b = [night_lights] %} {% if a and b %}
      {{ (a | reject('in', b) | list) | join(', ') }}
    {% else %}
      []
    {% endif %}

You’re not answering my question.

I want you to post a screenshot of the output of light_switch and night_lights variables in a trace. I do not care about crossover_night_lights or crossover_lights. We can’t care about them until we know what light_switch and night_lights look like in the trace.

Oh yea sorry

9
10

Ok, then the only code that will work is:

    {% set a = light_switch.entity_id %}
    {% set b = night_lights.entity_id %}
    {% if a and b %}
      {{ b | reject('in', a) | list }}
    {% else %}
      []
    {% endif %}

which works perfectly fine in developer tools.

{% set light_switch = {
  'entity_id': [
      'switch.family_room_light_2',
      'switch.entry_light',
      'light.water_tank_1_light',
  ]
} %}
{% set night_lights = {
  'entity_id': [
      'switch.family_room_light_2',
      'switch.outside_front_door_lights'
  ]
} %}

    {% set a = light_switch.entity_id %}
    {% set b = night_lights.entity_id %}
    {% if a and b %}
      {{ b | reject('in', a) | list }}
    {% else %}
      []
    {% endif %}

So, I’m not sure what you’re doing when testing your automation out.

1 Like

yep I agree. Just checking, is this set correctly?

variables:
  motion_trigger: !input motion_trigger
  light_switch: !input light_switch
  night_lights: !input night_lights
  crossover_lights: >-
    {% set a = light_switch.entity_id %}
    {% set b = night_lights.entity_id %}
    {% if a and b %}
      {{ a | reject('in', b) | list }}
    {% else %}
      []
    {% endif %}

  crossover_night_lights: >-
    {% set a = light_switch.entity_id %}
    {% set b = night_lights.entity_id %}
    {% if a and b %}
      {{ b | reject('in', a) | list }}
    {% else %}
      []
    {% endif %}

Yes. How are you testing the generated automation?

time trigger

and you’re 100% sure the error is coming from those 2 variables?

I can see in a screenshot above that you have something incorrect in your step calling night_whatever

image

You have an entity_id list, not a target object.

target:
  entity_d: "{{ crossover.... }}"
1 Like

:partying_face: :partying_face: :partying_face: :smiling_face_with_three_hearts: :smiley: :+1:

Nailed it, Thanks Petro… Legend

@petro

Just wondering if you could help us again. If we don’t have any entities in " night_lights" we get this error because obviously there is nothing in the list.

Is there a way to not get any errors if “night_lights” has no entities?

Template variable warning: 'str object' has no attribute 'entity_id' when rendering '{% set a = light_switch.entity_id %} {% set b = night_lights.entity_id %} {% if a and b %} {{ a | reject('in', b) | list }} {% else %} {{ a | list }} {% endif %}'
Template variable warning: 'str object' has no attribute 'entity_id' when rendering '{% set a = light_switch.entity_id %} {% set b = night_lights.entity_id %} {% if a and b %} {{ b | reject('in', a) | list }} {% else %} {{ b | list }} {% endif %}'

Thanks again

would anyone else know how to stop this template variable warning when night_lights has no entities selected in the input? Because there are no entities in “night_lights” the automation config has this.

  night_lights: {}

The warning

Template variable warning: 'str object' has no attribute 'entity_id' when rendering '{% set a = light_switch.entity_id %} {% set b = night_lights.entity_id %} {% if a and b %} {{ a | reject('in', b) | list }} {% else %} {{ a | list }} {% endif %}'
Template variable warning: 'str object' has no attribute 'entity_id' when rendering '{% set a = light_switch.entity_id %} {% set b = night_lights.entity_id %} {% if a and b %} {{ b | reject('in', a) | list }} {% else %} {{ b | list }} {% endif %}'
variables:
  motion_trigger: !input motion_trigger
  light_switch: !input light_switch
  night_lights: !input night_lights
  crossover_lights: >-
    {% set a = light_switch.entity_id %}
    {% set b = night_lights.entity_id %} # this is what is causing the warning  - because it has no entities
    {% if a and b %}
      {{ a | reject('in', b) | list }}
    {% else %}
      []
    {% endif %}

  crossover_night_lights: >-
    {% set a = light_switch.entity_id %}
    {% set b = night_lights.entity_id %} # this is what is causing the warning - because it has no entities
    {% if a and b %}
      {{ b | reject('in', a) | list }}
    {% else %}
      []
    {% endif %}

I tried explaining to you that using target selectors were complicated.

Here’s a post that grabs all entities from a target selector

Thanks Petro, yeah your not wrong about target selectors. I did manage to resolve the warning with a good night sleep.

Thanks for the link, nice to learn a thing or two and will have to remember that :wink: :+1:

1 Like