Noob issue with configuration.yaml and template sensors

I’ve been racking my brain about creating a simple sensor which adds up the current consumption of my smart lights. However the Home Assistant keeps informing me that the configuration is wrong and throws several error messages.

Been looking through forums and posts but can’t seem to resolve it

Below is my config, what am I doing wrong?

sensor:
- platform: template
  sensors:
    energieverbruik_totaal:
      friendly_name: 'Energie Verbruik (Totaal)'
      entity_id:
        - sensor.eetkamer_1_current_consumption
        - sensor.eetkamer_2_current_consumption
        - sensor.eetkamer_3_current_consumption
        - sensor.eetkamer_4_current_consumption
        - sensor.eetkamer_5_current_consumption
        - sensor.eetkamer_6_current_consumption
      value_template: "{{ states("sensor.eetkamer_1_current_consumption") |float + states("sensor.eetkamer_2_current_consumption") | float + states("sensor.eetkamer_3_current_consumption") | float + states("sensor.eetkamer_4_current_consumption") | float+ states("sensor.eetkamer_5_current_consumption") | float+ states("sensor.eetkamer_6_current_consumption") | float}}"
      unit_of_measurement: "kWh"

On the code at the top, I get this error message:
can not read a block mapping entry; a multiline key may not be an implicit key at line 47, column 26:
unit_of_measurement: “kWh”
^

If I remove the unit_of_measurement I get the following error message:
can not read an implicit mapping pair; a colon is missed at line 46, column 372:
… current_consumption") | float}}"
^

You must be reading old posts. entity_id is no longer valid or needed, you’re also using the legacy format.

Place this directly into configuration.yaml

template:
- sensor:
  - name: Energie Verbruik (Totaal)
    state: >
      {% set entities = expand('sensor.eetkamer_1_current_consumption', 'sensor.eetkamer_2_current_consumption', 'sensor.eetkamer_3_current_consumption', 'sensor.eetkamer_4_current_consumption', 'sensor.eetkamer_5_current_consumption', 'sensor.eetkamer_6_current_consumption') %}
      {{ entities | map(attribute='state') | map('float') | sum / 1000 }}
    availability: >
      {% set entities = expand('sensor.eetkamer_1_current_consumption', 'sensor.eetkamer_2_current_consumption', 'sensor.eetkamer_3_current_consumption', 'sensor.eetkamer_4_current_consumption', 'sensor.eetkamer_5_current_consumption', 'sensor.eetkamer_6_current_consumption') %}
      {{ entities | selectattr('state', 'is_number') | list | length == entities | length }}             
    unit_of_measurement: kWh

You might consider using powercalc from HACS, which does exactly this.

I’ll look into it, thanks

This resolved my issue.

Thank you Petro,
I did however needed to add a “>” after “availability:” and noticed that I needed to “sum / 1000” to get my kWh reading.

1 Like