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”:
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?
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.
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
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.
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?
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.
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)