I got a solar system a month ago and I started with HA about a week ago, and I´m even more confused after reading a lot of pages of documentation. I copied the following template from a webpage adapting it to the sensors in my HA. I´m trying to have the cost of electricity calculated according to the tariff I have. This is the configuration in my configuration.yaml:
If it is a template sensor, try new syntax. And check your template. Whenever I copy your original code, there is a backtick (`) behind last curly brace and I would make a space before filter symbol.
After checking several times, I found that the issue was that all sensors and templates were in the configuration.yaml. A colleague suggested to create the sensors.yaml and templates.yaml and it solved the issue, it seems it was a kind of collision.
Glad you’ve solved it. It is possible to have all of your configuration in the configuration.yaml file, but you can only have one top-level sensor: and one top-level template: declaration in the file. Splitting into separate files is a good way to keep things cleaner.
You can go further still. The top of my configuration.yaml contains:
then in config/sensors/ I have files like heating.yaml that contain heating-related sensors; in config/templates/ contains various YAML files for the template sensors / binary sensors etc.
Because you defined a Template Sensor using an invalid combination of legacy and modern format.
If you define a Template Sensor using the key word template: then all options after it must be in modern format. However, you used legacy format which isn’t valid for use with the template: key.
A Template Sensor defined in legacy format should be placed under the sensor: key (not template: key).
Creating separate files isn’t what solved it. What solved it was that you corrected the root-cause by putting legacy format Template Sensors in the sensors.yaml file and not in the templates.yaml file. I assure you that even with separate files, you can reproduce the original error if you were to put a legacy format Template Sensor into the templates.yaml file.
Thank you for the explanation, but I’m still confused. I’m completely new to HA and I’m trying to check in post, ask different people, search the forum and that’s why I could mix legacy and modern syntaxis. Could you please point me out to one place to differentiated both? When checking the official documentation, I assume that this is the modern one, then I mix things when adapting templates found in the web.
You can use both modern and legacy format at the same time. This is legal (assuming you don’t have any other sensor: or template: blocks):
sensor:
- platform: template
sensors:
legacy_sensor:
friendly_name: "Legacy sensor"
value_template: "I am a legacy sensor"
template:
- sensor:
- name: "Modern sensor"
state: "I am a modern sensor"
What you can’t do is mix up the allowable keys between them — you cannot use friendly_name and value_template in the modern configuration. The document linked above shows all of this, with the modern configuration first and a link to the legacy configuration at the bottom.
Thank you, Troon, somehow, I missed the bottom part. In your example, it’s quite easy to identify both syntaxis.
Now the last question (I promise): when using the modern syntaxis in templates.yaml, should I also start with template:? When correcting my configuration, I was not sure if I should place it in the templates.yaml or sensors.yaml because was I’m defining is a sensor in fact…
As you see, I’m quite confused and I need to read a lot more trying not to mix things
I hope my questions helps other newbies in the forum
It’s in the documentation for the Template integration. In my previous post, I included a link to the section explaining legacy format (which appears after the documentation’s section explaining modern format).
I don’t understand how you were led to that assumption. Refer to the highlighted screenshot in my previous post. The first line highlighted in green is the start of a Template Sensor in modern format yet everything highlighted in yellow is legacy format. You created an invalid blend of the two formats and 95% of it was legacy format.
FWIW, your questions have already been asked by others and they have been answered. Your
're not the first person to make invalid combinations of modern and legacy formats.