Lights on Template error

I am trying to write a sensors.yaml file that tells me how many lights are on. I am using a template provided on Smarthomejunkies site, but it is not working:

##### Count number of lights on ##################################
- platform: template
  sensors:
  number_lights_on:
    friendly_name: Number Lights On
    value_template: >-
    {{ states.light 
          | rejectattr('attributes.entity_id', 'defined') 
          | selectattr('state', 'eq', 'on')
          | list | count }}
    icon_template: mdi:lightbulb-group

I am running HASSOS on a RPI4 version 2022.10.4.

Because the indentation is incorrect. Look at the example in the documentation.

Here’s the corrected version (without the initial comment).

- platform: template
  sensors:
    number_lights_on:
      friendly_name: Number Lights On
      value_template: >-
        {{ states.light 
          | rejectattr('attributes.entity_id', 'defined') 
          | selectattr('state', 'eq', 'on')
          | list | count }}
      icon_template: mdi:lightbulb-group