Multiple Template Sensors

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!!

Should your last line of code be

{{ number.charging_amps | float(0) }}
  1. It needs to be states('number.charging_amps') .
  2. 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.

  - 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) }}

    - name: "ChargeAmps"
      state: >
          {{ states('number.charging_amps') }}

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:

-template:
  - sensor:
    - name: Garage Double Door Temp
      state: "{{states.sensor.lumi_lumi_sensor_magnet_aq2_device_temperature_3.state |float + 25 |round(0) }}"

I then tried to add another sensor to calibrate. I tried:

- sensor:
  - name: "Garage Double Door Temp"
    state: {{states.sensor.lumi_lumi_sensor_magnet_aq2_device_temperature_3.state |float + 25 |round(0) }}
 
  - name: "Front Door Temp"     
    state: {{states.sensor.lumi_lumi_sensor_magnet_aq2_device_temperature_5.state |float - 16.0 |round(0) }}

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.

Would appreciate any help.

For a basic configuration it should look like:

template:
  - sensor:
    - name: "Garage Double Door Temp"
      state: "{{ (states('sensor.lumi_lumi_sensor_magnet_aq2_device_temperature_3') | float + 25) |round(0) }}"
 
    - name: "Front Door Temp"     
      state: "{{ (states('sensor.lumi_lumi_sensor_magnet_aq2_device_temperature_5') |float - 16) |round(0) }}"

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?

Thank you sooo much! This works great. I think I just learned an important formatting rule.

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.