Cannot find sensors due to yaml code problems

I am not a YAML developer but I am trying to do a project to set up a dashboard with the Auto Entity card to see lights which are on and off.
To achieve this I set up a label Lights_Group and added it to the light entities.
Then I created a template with the code below but it does not work. I created the code in templates.yaml
I should be able to go to Settings >> Entities and search sensor.lights I should see the custom sensors, but I don’t see anything except Home Assistant Lights.

Also HA will restart, but I get a notification immediately after
• Invalid config for ‘template’ at templates.yaml, line 1: expected a dictionary ‘’, got ‘name:“lights_on_count” unique_id:82cac54b-acf0-427e-8f6c-44bbe105e23a icon:mdi:lightbulb state:> {{states.light| rejectattr('entity_id','in',label_entities('GROUP')) | rejectattr('entity_id','in',label_entities('LED')) | rejectattr('entity_id','in',label_entities('SECURITY')) | selectattr('state','eq','on') | list | count}}’

• Invalid config for ‘template’ at templates.yaml, line 10: expected a dictionary ‘’, got ‘name:“lights_off_count” unique_id:fc35325d-f934-4aa6-8978-9061b64399f1 icon:mdi:lightbulb-off state:> {{states.light| rejectattr('entity_id','in',label_entities('GROUP')) | rejectattr('entity_id','in',label_entities('LED')) | rejectattr('entity_id','in',label_entities('SECURITY')) | selectattr('state','eq','off') | list | count}}’

Can anyone assist me with what may be wrong with the code?

Templates.yaml

  • name:“lights_on_count”
    unique_id:82cac54b-acf0-427e-8f6c-44bbe105e23a
    icon:mdi:lightbulb
    state:>
    {{states.light| rejectattr(‘entity_id’,‘in’,label_entities(‘GROUP’))
    | rejectattr(‘entity_id’,‘in’,label_entities(‘LED’))
    | rejectattr(‘entity_id’,‘in’,label_entities(‘SECURITY’))
    | selectattr(‘state’,‘eq’,‘on’)
    | list | count}}
  • name:“lights_off_count”
    unique_id:fc35325d-f934-4aa6-8978-9061b64399f1
    icon:mdi:lightbulb-off
    state:>
    {{states.light| rejectattr(‘entity_id’,‘in’,label_entities(‘GROUP’))
    | rejectattr(‘entity_id’,‘in’,label_entities(‘LED’))
    | rejectattr(‘entity_id’,‘in’,label_entities(‘SECURITY’))
    | selectattr(‘state’,‘eq’,‘off’)
    | list | count}}

We can’t read those logs or that YAML formatted like that. Please format it properly with the </> button or by surrounding it with three backticks:

```
CODE (YAML, LOGS ETC) GOES HERE
```

You’ll need to re-paste the code, because the forum has converted all of the quotes and spaces. If it doesn’t look like my code blocks below, don’t hit Submit.

I assume your configuration.yaml has a line in that reads:

template: !include templates.yaml

That file should then contain YAML structured like this (one of mine):

- sensor:
    - name: outside_temperature
      unique_id: fd5837fd-6d40-4cd8-81a2-fc84d2411ff5
      unit_of_measurement: "°C"
      device_class: temperature

I think you’re missing the first - sensor: line; but without the properly-formatted code I can’t comment on any other problems as the indentation has been destroyed.

If you’re finding YAML hard, you could always create those sensors in the UI. Helpers / Create Helper / Template / Template a sensor, then:

Don’t use the code from your post above though, as all the quotes are now wrong.

1 Like

Good news it now works. I had copied the code but it just had erros so I removed spaces to get rid of the errrors. Don’t understand why it works but by retyping the lines in it seems to fix the erros so all is good. Thankyou for your help. Much appreciated.