Issue with rejectattr not working after upgrading to 2024.4.1

I have had a script built for the past 6 months that will tell me if any windows or doors are open set to run at a specific time. I had to use a rejectattr to exclude the integration of my Toyota, as it was catching my vehicles doors as open. Everything was working fine until I upgraded to the latest stable release, and now the script/template does not seem to ignore what I am telling it to. Not sure if this is a bug in the latest release or if something has changed and I need to update my script.

My current template is as follows:

message: >
    {%- set windowsanddoors = states.binary_sensor|
    selectattr('attributes.device_class', 'in',['door','window'])|
    selectattr('state','eq','on') | map(attribute='name')  |
    rejectattr('entity_id', 'in', integration_entities('Lexus NX450h+')) | list
    %} {% if windowsanddoors | length == 0 %} All windows and doors are closed.
    {%- elif windowsanddoors | length == 1 %} {{ windowsanddoors[0] }} is open.
    {%- else %} {{ windowsanddoors[:-1] | join(', ') }}{{',' if windowsanddoors
    | length > 2 else ''}} and {{ windowsanddoors[-1]}} are open. {%- endif %}

EDIT: I currently disabled the Toyota integration as a workaround

Are you sure the name of the integration is “Lexus NX450h+”?

Also, you should always select for the device class attribute being defined prior to selecting for its value… there will often be entities that don’t have a defined device class and it will cause your template to fail.

After you do | map(attribute='name') you don’t have the full state objects to work with anymore, only the names. So using selectattr('entity_id', 'in' whatever) won’t work anymore.

Move the map filter to after the selectattr() filter

1 Like

Thank you for the explanation.

Can you provide an example of what you mean? I’m not the greatest when it comes to the script stuff.

| map(attribute='name') | rejectattr('entity_id', 'in', integration_entities('Lexus NX450h+'))

These 2 filters have to be switched. After applying the first one (| map()) you will only have the names left, so you don’t have access to the entity_id anymore to use in the second one (| rejectattr())

1 Like

Excellent. That is what I thought, and seemed to work in testing, but just wanted to confirm!

Thank you very much!