Template works in dev tools but not in lovelace

I have a template which works fine in the dev tools and produces the expected output, but when trying to use it on a dashboard there is no output at all. Can anyone suggest why this might be?

{{ states.binary_sensor | selectattr('state', 'eq', 'on') | selectattr('attributes.device_class', 'eq', 'door') | selectattr('attributes.friendly_name', 'match', '.*window.*') | list | count }}

Weirdly I just got it to work by removing code :confused:

I dropped selectattr('attributes.device_class', 'eq', 'door') which left me with

{{ states.binary_sensor | selectattr('state', 'eq', 'on') | selectattr('attributes.friendly_name', 'match', '.*window.*') | list | count }}

I get the same expected result and it now works on a dashboard too