Issue regarding deprecation of legacy templates

Hello,

I just updated my HA and got a message about legacy templates being deprecated and showed my my legacy template that is an issue. What I currently have is the following, which is in “includes > sensor.yaml”:

(it counts the number of lights on)

  • platform: template
    sensors:
    lights_on:
    friendly_name: ‘Number of lights on’
    value_template: >
    {% set lights = [
    states.light.bedroom_right,
    states.light.living_room_right,
    states.light.living_room_left,
    states.light.living_room_floor1,
    ] %}
    {{ lights | selectattr(‘state’,‘eq’,‘on’) | list | count }}

and gives me the following to replace it with:

template:

  • sensor:
    • default_entity_id: sensor.lights_on
      name: Number of lights on
      state: “{% set lights = [\n states.light.bedroom_right,\n states.light.living_room_right,\n
      \ states.light.living_room_left,\n states.light.living_room_floor1,\n] %} {{
      lights | selectattr(‘state’,‘eq’,‘on’) | list | count }}”

I put that in to my ‘sensor.yaml’ file but, but then it gives a “DisallowedExtraPropWarning” message on the line where ‘template:’ is.

I saw in the thread explaining about the deprecation that one should use the “template” file, but I don’t know where this is located. I am assuming this warning is coming up because it is not in the “correct” file?

Thanks, in advance, for your insight and advice.

it goes in configuration.yaml not sensor.yaml

Does it have to go in to ‘configuration.yaml’ or can I create a file in the same level as ‘configuration.yaml’ and create an !include to it?

If you have a templates.yaml then it should go there. But then you have to remove the top line with template: and match the indenting to anything that is already there. If you do not have a templates.yaml file then you could indeed create an include for it.

Ugh. OK, now I am getting "entity ‘sensor.lights_on’ does not exist in your Home Assistant instance’.

It was working fine as-is before; why did this have to change?!?

post your current config and the file you’re trying to include and any errors in the logs

You are asking while linking to the post that explains it?

I didn’t link to any post.

OK, so I was frustrated earlier but have “calmed” down. I posted the following into the Developer Tools Template editor:

template:
  - sensor:
    - default_entity_id: sensor.lights_on
      name: Number of lights on
      state: "{% set lights = [
        states.light.bedroom_right,
        states.light.living_room_right,
        states.light.living_room_left,
        states.light.living_room_floor1,
      ] %} {{
        lights | selectattr('state','eq','on') | list | count }}"

and the “Result” actually returns the correct result (“3” lights on at the time. I even confirmed this by turning on the “bedroom_right” and the result changed to “4”, as expected)

However, when I paste the above code into my ‘configuration.yaml’, the following errors show up (I am entering the line from above the errors in for reference, then the error that is on that line in bold; hopefully it makes sense. if not, let me know if there is a better way to do this. I am using Visual Code Editor):

  • default_entity_id: sensor.lights_on - Entity ‘sensor.lights_on’ does not exist in your Home Assistant instance
  • states.light.living_room_floor1, - Missing closing "quote
  • ] %} {{ - Unexpected flow-seq-end token in YAML stream: “]”

ignore what vscode tells you. It’s wrong.

You also edited the template and broke it by messing with the spaces. The repair wouldn’t have provided that, it would have had the \n’s in it. When you removed them, you broke the template.

OK, I have made some progress. I have been able to "resolve: all the errors except the one about the “Entity ‘sensor.lights_on’ does not exist in your Home Assistant instance”. I think my issue about the other errors was indentation. I updated my template code to the following:

template:
  - sensor:
    - default_entity_id: sensor.lights_on
      name: Number of lights on
      state: "{% set lights = [
        states.light.bedroom_right,
        states.light.living_room_right,
        states.light.living_room_left,
        states.light.living_room_floor1,
        ] %} {{
        lights | selectattr('state','eq','on') | list | count }}"

basically the only real change was indenting ‘] %} {{’ to the same level as the various ‘states.*’ entries.

Still can’t figure out anything about the “Entity ‘sensor.lights_on’ does not exist in your Home Assistant instance”; I am guessing I have to define it somewhere, but not sure where? I don’t think I defined it anywhere before with the legacy code.

Are you ignoring my responses? The code editor you are using is wrong, ignore that warning.

No, I didn’t see your response before I posted my update.

Yes, the suggested fix did include those \n, etc., but as I have now removed them and don’t have a copy of what was suggested, is there a way I can “correct” it to get that syntax back?

You can just fix the template by using multiline notation instead of single line notation.

template:
  - sensor:
    - default_entity_id: sensor.lights_on
      name: Number of lights on
      state: > 
        {% set lights = [
          states.light.bedroom_right,
          states.light.living_room_right,
          states.light.living_room_left,
          states.light.living_room_floor1,
          ] %}
        {{ lights | selectattr('state','eq','on') | list | count }}

OK, so I am assuming that the ‘>’ after “state:” denotes multi-line?

I just pasted that updated code into my ‘configuration.yaml’ (ignoring that it still gives that “Entity does not exist” message) and reloaded the YAML configuration.

I went into my dashboard to the Horizontal Stack card where I use the ‘sensor.lights_on’ entity to show the number of lights on, but it also says “Entity not found” and I am unable to find it in a search within the “Entity” field of the card.

The Template editor in Developer Tools still returns the correct number of lights on with that updated code, though.

Yes

Where did you put the code? Share your entire config.

Here is my complete configuration.yaml:

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

# Enables the frontend
frontend:
  themes: !include_dir_merge_named themes

template:
  - sensor:
    - default_entity_id: sensor.lights_on
      name: Number of lights on
      state: > 
        {% set lights = [
          states.light.bedroom_right,
          states.light.living_room_right,
          states.light.living_room_left,
          states.light.living_room_floor1,
          ] %}
        {{ lights | selectattr('state','eq','on') | list | count }}

Ok, did you restart or reload template entities after making the change?

I only did “Restart all YAML configuration” but I just did a full HA restart just now and it is now working (number of lights that are on is reporting as expected)

Thanks! :slight_smile: