Where to place template code and how to add the template integration?

Hi all,
I need a little help with my first template which is a simple calculation.

#
# Dewpoint Calculations
#
# Simplified formula: Td = T - ((100 - RH)/5.)
#
# where Td is dew point temperature (in degrees Celsius), 
#       T  is observed temperature (in degrees Celsius), and 
#       RH is relative humidity (in percent). 
# This relationship is fairly accurate for relative humidity values above 50%.
#
# T  = sensor.t_buiten_2
#          unit of Measurement °C
#
# RH = sensor.humidity_buiten_bme280
#
template:
  - sensor:
    dew_point:
    friendly_name: “Dewpoint-Home”
    unit_of_measurement: “°C”
    value_template: >
      {% set T = states('sensor.t_buiten_2') | float %}
      {% set RH = states('sensor.humidity_buiten_bme280') | float %}

      {{ (T - ((100 - RH) / 5 ))|round(2) }}
#
#   End of Dewpoint calculation
#

This calculation works - it delivers a numerical result (-3.44) in the template editor under developer tools. Made me happy.

The problem starts when I simply drop this code into configuration.yaml or into automation.yaml.
I get errors with the Template integration not existing in the first case, and template not being a valid option for the latter.
“Logger: homeassistant.config
Source: config.py:508 First occurred: 16:32:10 (1 occurrences) Last logged: 16:32:10
Invalid config for [automation]: [template] is an invalid option for [automation].
Check: automation- >template. (See /config/configuration.yaml, line 8)”

So what am I not doing which I should be doing, what is missing ?

Bernard

You’re mixing the old template integration with the new one. Stick to the new format listed in the docs, not posts on the forums

template:
  - sensor:
      - name: Dewpoint Home
        unit_of_measurement: "°C"
        state: >
          {% set T = states('sensor.t_buiten_2') | float(0) %}
          {% set RH = states('sensor.humidity_buiten_bme280') | float(0) %}
          {{ (T - ((100 - RH) / 5 )) | round(2) }}

Thanks for the syntax correction. I am sure it was helpful.

However it did not solve the problem.

I did manage to get things working by creating a template.yaml file, using the include !template.yaml line in configuration.yaml and placing the code in there.

But still not sure what the issue was with “template is an invalid option”.

If you tried what I posted and it failed to work then you did something wrong.

The example I posted is syntactically correct and, if copy-pasted correctly into configuration.yaml, it will pass the configuration check and, after executing Reload Template Entities, will produce a new entity named sensor.dewpoint_home.

If you copy-pasted it into template.yaml then you must remove the first line from the example containing template: otherwise it will report an error. Why? Because you are effectively duplicating the template: key.

1 Like

you can’t put the template: line inside the included file.

Thanks - I get it.

Same is valid for sensor: inside 1sensors.yaml`.

Where is this general principle described ?

It’s yaml. It’s just how yaml works.

This here describes splitting your configuration and while it doesn’t explicitly say it, all the examples have it

Think about what this means:

template: !include templates.yaml

It’s saying include the contents of the file named templates.yaml with the key named template:

Let’s say this is what is inside of templates.yaml

  - sensor:
      - name: example
        state: "{{ now().timestamp() | timestamp_custom() }}"

Here’s the result of using template: !include templates.yaml

template:
  - sensor:
      - name: example
        state: "{{ now().timestamp() | timestamp_custom() }}"

Now let’s take the example where the file contains this:

template:
  - sensor:
      - name: example
        state: "{{ now().timestamp() | timestamp_custom() }}"

Here’s the result of using template: !include templates.yaml

template:
  template:
    - sensor:
        - name: example
          state: "{{ now().timestamp() | timestamp_custom() }}"

It contains a second instance of template: key. Why? Because we literally did specify it twice. The first time in template: !include templates.yaml and the second time inside the templates.yaml file.


EDIT

Correction. Included content should be indented.

3 Likes

sorry, minor correction here:

template:
  template:
    - sensor:
        - name: example
          state: "{{ now().timestamp() | timestamp_custom() }}"

It’s not a duplicate key, it includes the entire structure into the template category, so it’s essentially indented.

To get a duplicate key it would have to be included with an anchor like this

template:
<<: !include templates.yaml &templates

and TBH, I’m not sure if that even works

2 Likes

Oops! :man_facepalming: Fixed. Thanks!

1 Like