Newbie fighting with templates

Hi there!

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:

`#Medidor consumo electrico

utility_meter:
daily_energy:
source: sensor.today_load
cycle: daily
tariffs:
- Pico
- Valle
monthly_energy:
source: sensor.today_load
cycle: monthly
tariffs:
- Pico
- Valle

#Automatizacion cambio de tarifa

automation:

  • alias: “Cambio de tarifas electricas”
    initial_state: true
    hide_entity: true
    trigger:
  • platform: time
    at:10:00:00
  • platform: time
    at:11:00:00
  • platform: time
    at:12:00:00
  • platform: time
    at:13:00:00
  • platform: time
    at:14:00:00
  • platform: time
    at:15:00:00
  • platform: time
    at:17:00:00
  • platform: time
    at:18:00:00
    action:
    • service: utility_meter.next.tariff
      entity_id: utility_meter.daily_energy
    • service: utility_meter.next.tariff
      entity_id: utility_meter.monthly_energy

#Sensores de calculo de consumo

template:
sensors:
gastovalle:
friendly_name: “Gasto Valle”
unit_of_measurement: CZK
device_class: monetary
value_template: {{ ( (6.44)| float * states(‘sensor.monthly_energy_pico’) | float ) | round(2) }}

gastopico:
  friendly_name: "Gasto Pico"
  unit_of_measurement: CZK
  device_class: monetary
  value_template: {{ ( (6.59)| float * states('sensor.monthly_energy_pico') | float ) | round(2) }}`

But I´m getting the error :

Error loading /config/configuration.yaml: invalid key: “{”( (6.44)| float * states(‘sensor.monthly_energy_pico’) | float ) | round(2)": None}" in “/config/configuration.yaml”, line 209, column 0

How can I solve it? And even more important, why I´m getting such error?

Definitely, there are a lot of things to explore and learn about the HA universe

Oooops, it seems the code was not formatted, only the last part… Sorry, another thing to learn. Anyway, the original code is in the page Calcula tu gasto eléctrico con Home Assitant - Domology 2.0

Single line templates need quotation marks around.

value_template: "{{ ( (6.59) | float * states('sensor.monthly_energy_pico') | float ) | round(2) }}"

Thank you Kimotu, but I tried as well and I’m still getting the same error.

The File Editor says that the configuration is OK, but when I check with Developers Tools, it says that I can’t restart HA due to this error.

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.

template:
  - sensor:
    - name: "Gasto Pico"
      unit_of_measurement: CZK
      device_class: monetary
      state: "{{ ( (6.59) | float * states('sensor.monthly_energy_pico') | float ) | round(2) }}"

Thank you Kimotu,

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:

sensor: !include_dir_merge_list sensors
template: !include_dir_merge_list templates

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.

Dear Taras,

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.

Thank you very much again.

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 :blush:

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.

No, because you already have that in configuration.yaml:

template: !include templates.yaml

Then templates.yaml should look like:

- sensor:
    - name: "Modern sensor"
      state: "I am a modern sensor"
    - name: "Meaning of life"
      state: 42
- binary_sensor:
    - name: "Universe makes sense"
      state: "{{ states('sensor.meaning_of_life')|int(42) == 6*9 }}"

…exactly as if it were pasted in at the !include but at the correct indentation level.

Thank you again Troon and Taras, much clear now

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.