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 %}
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.
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 %}
# 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.
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 %}
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.
{% 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.
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 %}
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 %}'
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 %}