I am trying to add more than one template sensor in Configuration.yaml and cannot get the syntax right after couple of hours of trying every option i can think of. this is what i have currently.
- sensor:
- name: "Indoors"
state: >
{% set downstairs = states('sensor.downstairs_temperature') | float(0) %}
{% set upstairs = states('sensor.upstairs_temperature') | float(0) %}
{{ (((downstairs + upstairs) / 2) | round(1, default=0.0)) }}
- sensor:
- name: "ChargeAmps"
state: >
{% number.charging_amps | float(0) %}```
This results in the following error
Logger: homeassistant.config
Source: config.py:868
First occurred: 4:48:06 PM (3 occurrences)
Last logged: 4:49:31 PM
* Invalid config for [template]: invalid template (TemplateSyntaxError: tag name expected) for dictionary value @ data['sensor'][0]['state']. Got '{% (number.charging_amps) | float(0) %}\n'. (See /config/configuration.yaml, line 81).
* Invalid config for [template]: invalid template (TemplateSyntaxError: Encountered unknown tag 'number'.) for dictionary value @ data['sensor'][0]['state']. Got '{% number.charging_amps | float(0) %}\n'. (See /config/configuration.yaml, line 81).
Line 81 being
{{ (((downstairs + upstairs) / 2) | round(1, default=0.0)) }}
I hope someone can help as this is driving me mad!!
You need to use the correct delimiters {{ }} for the sensor to have an output. The statement delimiters {% %} are used for setting variable values, defining macros, and control structures like if/then and for loops. On their own they do not output anything.
FWIW, there’s no point in converting the value to a float since all states return strings anyway. You only need to convert it if you were going to do further mathematical functions within the template. Also, you don’t need to declare sensor every time.
I’m trying to do something similar. I want to create a new sensor from a correction to an existing sensor (calibrating temperatures). I can get 1 sensor to work, but having problems for the second sensor. This works for 1 sensor:
Got configuration errors. Tried preceding entire block with “- template:”, indenting the rest. Still configuration errors. I’m new at this so probably made a dumb error.
Note that your non-working example was missing the required quote marks around the templates.
If that doesn’t fix the issue, you will need to provide a bit more detail about how you have set up the configuration of these sensors. Are they directly in the configuration.yaml file or somewhere else? If somewhere else, which method are you using to have that file included into configuration.yaml?
Thanks for the help, thats got it working now. I was using the float(0) to try to avoid a “undefined” problem when the sensor was not yet initialised. Is there another way to provide a default value?
scratch that, stupid question obviously “default = 0.0” as in the previous sensor.