Sensor Template help please!

Hi all,

I’m trying to make my first template sensor and I keep getting errors, not sure what my issue is after reading through the template documentation. Trying out the code in the template section of the developer tools it seems to work, but every time I put it in my config file I get an error (error text below the code)

template:
  - sensor:
    - name: "Minutes until next alarm on Pixel 6"
      state: "{{((as_timestamp(states('sensor.pixel_6_next_alarm')) - as_timestamp(now()))/60)|int}}"

  - sensor:
    - name: "Average upstairs temp"
      state: >
      {% set bedroom = states('sensor.lumi_lumi_weather_temperature') | float %}
      {% set office = states('sensor.office_temperature') | float %}
      
      {{((bedroom + office) / 2) | round(1, default=0) }}

And this is the error that I keep getting from HA:

  • Invalid config for [template]: extra keys not allowed @ data[‘state’]. Got “{{((as_timestamp(states(‘sensor.pixel_6_next_alarm’)) - as_timestamp(now()))/60)|int}}” required key not provided @ data[‘sensor’][0][‘state’]. Got None. (See /config/configuration.yaml, line 24).
  • Invalid config for [template]: extra keys not allowed @ data[‘state’]. Got “{% set bedroom = states(‘sensor.lumi_lumi_weather_temperature’) | float %} {% set office = states(‘sensor.office_temperature’) | float %}\n{{((bedroom + office) / 2) | round(1, default=0) }}\n” required key not provided @ data[‘sensor’][0][‘state’]. Got None. (See /config/configuration.yaml, line 29).

Any help would be greatly appreciated!!

That second instance of sensor is valid yaml and it should work.

1 Like

You need to indent these lines:

Your template can’t be inline with the word state. Keep state at the indentation it’s currently at. Indent the actual template lines by 2 spaces.

@petro Thanks for the quick reply…
But it’s still giving me the same error

image

you removed the indent from state… I said add indents to the template lines.

Use min_max like this instead.

sensor:
  - platform: min_max
    name: Avg Upstairs Temperature
    type: mean
    entity_ids:
      - sensor.1_temperature
      - sensor.2_temperature
      - sensor.3_temperature

image

1 Like