Help for a template sensor error

I have got this error on startup:

TemplateError(‘TypeError: argument of type ‘NoneType’ is not iterable’)

with the following template

{{states.light 
  | selectattr('entity_id', 'in', state_attr('light.cuisine','entity_id')) 
  | selectattr('state', 'eq', 'on')  
  | list 
  | count 
}}

What’s the way to not have this error please?
tx for answers

If you only want the entities in the light group, there’s no reason to start with the state objects of all light entities.

{{ state_attr('light.cuisine','entity_id') 
  | select('is_state', 'on')  
  | list 
  | count }}

If this is being used in a template sensor, you should assign an availability to avoid errors when the light group isn’t available at restart/reload.

good lesson
tx very much, all is ok now