Template.yaml sensors

I have a template.yaml that has a list of sensors. I have - sensor: before every single sensor. Shouldn’t I be able to have sensors: at the top so I don’t have to repeat each sensor? I get an error when I use Check Configuration that tells me to check the log but I don’t see anything in the log. What am I doing wrong? How can I make the yaml file more concise?

This is what is in the original template.yaml:

  - sensor:
      - name: Deebot N8 Pro+ Battery
        unit_of_measurement: "%"
        state: "{{ state_attr('vacuum.deebot_n8_pro', 'battery_level') }}"

  - sensor:
      - name: Deebot N8 Pro+ Status
        state: "{{ state_attr('vacuum.deebot_n8_pro', 'last_error') }}"

  - sensor:
      - name: HVAC Fan Mode
        state: "{{ state_attr('climate.thermostat', 'fan_mode') }}"

  - sensor:
      - name: HVAC Action
        state: "{{ state_attr('climate.thermostat', 'hvac_action') }}"

  - sensor:
      - name: Todays High Temp
        state: "{{states('sensor.openweathermap_forecast_temperature') }}"

  - sensor:
      - name: Todays Low Temp
        state: "{{states('sensor.openweathermap_forecast_temperature_low') }}"

Format should be like this:

platform: template
sensors:

And then, for each sensor you start with name of the sensors, and sensor definition:

  media_player_title:
    value_template: '{{ state_attr("media_player.lg_tv", "media_title") }}'
    friendly_name: Media Player Title
  media_content_type:
    value_template: '{{ state_attr("media_player.lg_tv", "media_content_type") }}'
    friendly_name: Media Content Type
  media_source:
    value_template: '{{ state_attr("media_player.lg_tv", "source") }}'
    friendly_name: Media Source

No need to have sensor: for each of them

That’s the Legacy Sensor configuration format Coincidentally as I was looking for that link I found an example of what I needed on that same page with the example to Change the Unit of Measurement which has sensor just once but two entries below it.

template:
  - sensor:
      - name: "Transmission Down Speed"
        unit_of_measurement: "kB/s"
        state: "{{ states('sensor.transmission_down_speed')|float * 1024 }}"

      - name: "Transmission Up Speed"
        unit_of_measurement: "kB/s"
        state: "{{ states('sensor.transmission_up_speed')|float * 1024 }}"

So now my template.yaml has the following and no errors with Check Configuration

  - sensor:
      - name: Deebot N8 Pro+ Battery
        unit_of_measurement: "%"
        state: "{{ state_attr('vacuum.deebot_n8_pro', 'battery_level') }}"

      - name: Deebot N8 Pro+ Status
        state: "{{ state_attr('vacuum.deebot_n8_pro', 'last_error') }}"

      - name: HVAC Fan Mode
        state: "{{ state_attr('climate.thermostat', 'fan_mode') }}"

      - name: HVAC Action
        state: "{{ state_attr('climate.thermostat', 'hvac_action') }}"

      - name: Todays High Temp
        state: "{{states('sensor.openweathermap_forecast_temperature') }}"

      - name: Todays Low Temp
        state: "{{states('sensor.openweathermap_forecast_temperature_low') }}"
1 Like

Yeah you can have one - sensor: and one - binary_sensor: under template: with the individual sensors listed under each. You have to have a separate - trigger: under template: for each triggered sensor or binary sensor though.

1 Like

What is a triggered sensor?

https://www.home-assistant.io/integrations/template/#trigger-based-template-sensors

I’m glad you mentioned the binary_sensor. I used that info to include a few in my template.yaml

My solution is:

in configuration.yaml:

template:
  - sensor: !include template_sensors.yaml

and in template_sensors.yaml

- name: Sonnenwinkel
  unit_of_measurement: "°"
  icon: mdi:sun-angle-outline
  state: "{{ '%+.1f'|format(state_attr('sun.sun', 'elevation')) }}"
- name: Sonnenazimuth
  unit_of_measurement: "°"
  icon: mdi:sun-angle-outline
  state: "{{ '%+.1f'|format(state_attr('sun.sun', 'azimuth')) }}"

3 Likes