Help with my first template sensor

Hi, I’m trying to create a template sensor showing the average temperature in my house but I’m doing something wrong. I want say firstly that I haven’t put the following code in configuration.yaml under the template: key but I wanted to put every template entity in a sidecar file, writing template: !include template_entities.yaml.
My code for the sensor is that below (it should calculate the average temperature of my thermostatic valves)

- sensor:
    - name: "Temperatura Media Casa"
      unit_of_measurement: "C°"
      state: >
        {% set bagno = {{state_attr("climate.netatmo_bagno", "current_temperature")}} | float %}
        {% set letto = {{state_attr("climate.netatmo_camera_da_letto", "current_temperature")}} | float %}
        {% set sofia = {{state_attr("climate.netatmo_camera_di_sofia", "current_temperature")}} | float %}
        {% set salotto = {{state_attr("climate.netatmo_salotto", "current_temperature")}} | float %}

        {{ ((bagno + letto + sofia + salotto) / 4) | round(1, default=0) }}

I’ve adapted my code from the very first example in the template documentation page here.
Could anyone help me please? thanks!

Tip 1… try this out in the dev section under ‘Templates’ before configuration.yaml and unnecessary restarts
Then … an example from my side as food for thought

home_feels_like:
        friendly_name: 'Feels Like Outside'
        value_template: >
                        {% set temperature = state_attr('weather.home', 'temperature') %}
                        {% set humidity = state_attr('weather.home', 'humidity') %}
                        {% set windspeedmps = state_attr('weather.home', 'wind_speed') / 3.6 %}
                        {% set wvp = (humidity/100) * 6.105 * e**((17.27*temperature) / (237.7 + temperature))%}
                        {{(temperature + 0.33 * wvp - 0.70 * windspeedmps - 4.00)|round(2)}}
        unit_of_measurement: "°C"

Vingerha is right, testing codes in Developer Tools —> Templates saves tons of lifetime. :slight_smile:

Your code is almost correct; compare with the referenced example in the documentation


template:
  - sensor:
      - name: "Average temperature"
        unit_of_measurement: "°C"
        state: >
          {% set bedroom = states('sensor.bedroom_temperature') | float %}
          {% set kitchen = states('sensor.kitchen_temperature') | float %}

          {{ ((bedroom + kitchen) / 2) | round(1, default=0) }}

and drop the curly brackets.

Thank you very much now it works :slight_smile: