Can't figure out the correct YML formatting

Hi,
I can’t figure out the correct YML formatting as I get various errors.

Invalid config for ‘homeassistant’ at packages/template.yaml, line 1: expected a dictionary for dictionary value ‘packages->template’, got [{‘sensor’:

I’m trying to create a template sensor which counts all my switched on lights.
I have a “template.yml” file in the folder " packages" with the following data (I reduced it only to some lights).

- sensor:
    numberOfLightsOn:
      friendlyname: LightsOn
        state: >
        {% set lights = [
          states.light.eg_kochinsel,
          states.light.eg_kuche,
          states.light.eg_esstisch,
          ] %}
        {{ lights | selectattr('state','eq','on') | list | count }}

In the yml config file I include it as following:

homeassistant:
  packages: !include_dir_named packages 

Would highly appreciate it, if someone could point me in the direction of the error.
Thanks a lot!