Template for total power consumption not working

Hi all,

I have multiple power monitoring smart plugs alla round the house that I use to monitor real-time power consumption (in Watt) of multiple devices.

I would like to use the “Template” platform to create a “Total Power Consumption” sensor which is the sum of every smart plug power consumption.

I am using the following code:

- platform: template
  sensors:
    total_power_consumption:
      friendly_name: 'Total Power Consumption'
      entity_id:
        - sensor.dishwasher_energy_power
        - sensor.oven_energy_power
        - sensor.coffee_machine_energy_power
	    - sensor.fridge_energy_power
	    - sensor.microwave_energy_power
      value_template: "{{ (states('sensor.dishwasher_energy_power')|float + states('sensor.oven_energy_power')|float + states('sensor.coffee_machine_energy_power')|float) + states('sensor.fridge_energy_power)|float + states('sensor.microwave_energy_power')|float)|round(3) }}"
      unit_of_measurement: "W"

I get a green check mark in the upper right corner meaning that text formatting should be ok.
Unfortunately I get the following error when I check the configuration.yaml:

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token ‘,’, got ‘sensor’) for dictionary value @ data[‘sensors’][‘total_power_consumption’][‘value_template’]. Got “{{ (states(‘sensor.dishwasher_energy_power’)|float + states(‘sensor.oven_energy_power’)|float + states(‘sensor.coffee_machine_energy_power’)|float) + states('sensor.fridge_energy_power)|float + states(‘sensor.microwave_energy_power’)|float)|round(3) }}”. (See ?, line ?).

And I can’t figure out why.

This is everything that’s in my configuration.yaml at the moment:

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml


homeassistant:
  customize: !include customize.yaml
  

frontend:
  themes: !include_dir_merge_named themes


# Wake on LAN enable
wake_on_lan:


# DuckDNS Home Assistant
http:
  ssl_certificate: /ssl/fullchain.pem
  ssl_key: /ssl/privkey.pem
  
  
# Alexa Home Assistant Integration
api:
alexa:
  smart_home:
    
    
# System Monitor
sensor:
  - platform: systemmonitor
    resources:
      - type: disk_use_percent
        arg: "/"
      - type: disk_use
        arg: "/"
      - type: disk_free
        arg: "/"
      - type: memory_use_percent
      - type: memory_use
      - type: memory_free
      - type: throughput_network_in
        arg: eth0
      - type: throughput_network_out
        arg: eth0
      - type: ipv4_address
        arg: eth0
      - type: processor_use
      - type: processor_temperature
      
      
# Date & Time
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'date_time_utc'
      - 'date_time_iso'
      - 'time_date'
      - 'time_utc'
      - 'beat'
      
      
# Custom Time & Date Template
  - platform: template
    sensors:
      pretty_date:
        friendly_name: Data di oggi
        value_template: >-
          {% set today = states("sensor.date") %}
          {% set arr_week_days = ["Domenica", "Lunedì", "Martedì", "Mercoledì", "Giovedì", "Venerdì", "Sabato"] %}
          {% set arr_months = ["Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"] %}
          {% set week_day = as_timestamp(today) | timestamp_custom('%w') | int %}
          {% set day = as_timestamp(today) | timestamp_custom('%d') %}
          {% set month = as_timestamp(today) | timestamp_custom('%m') | int %}
          {% set year = as_timestamp(today) | timestamp_custom('%Y') | int %}
          {{ arr_week_days[week_day] }}, {{ day }} {{ arr_months[month-1] }} {{ year }}
          
          
# Total Power Consumption
  - platform: template
    sensors:
      total_power_consumption:
        friendly_name: Total Power Consumption
        entity_id:
          - sensor.dishwasher_energy_power
          - sensor.oven_energy_power
          - sensor.coffee_machine_energy_power
	      - sensor.fridge_energy_power
	      - sensor.microwave_energy_power
        value_template: "{{ (states('sensor.dishwasher_energy_power')|float + states('sensor.oven_energy_power')|float + states('sensor.coffee_machine_energy_power')|float) + states('sensor.fridge_energy_power)|float + states('sensor.microwave_energy_power')|float)|round(3) }}"
        unit_of_measurement: "W"

What am I doing wrong? Any help is greatly appreciated.
Thanks!

Try below instead …
I don’t think you need the list of entity_id: (maybe even wrong to add them)

sensor:
  - platform: template
    sensors:
      total_power_consumption:
        friendly_name: 'Total Power Consumption'
        value_template: "{{ (states('sensor.dishwasher_energy_power')|float + states('sensor.oven_energy_power')|float + states('sensor.coffee_machine_energy_power')|float) + states('sensor.fridge_energy_power)|float + states('sensor.microwave_energy_power')|float)|round(3) }}"
        unit_of_measurement: "W"
1 Like

There is an even better solution. I found that when I was solving my own problem just now.
My problem was that the Energy dashboard did not allow me to add a template sensor, which sums up 3 real sensors.
See also Template - Home Assistant and Templating - Home Assistant

Here my entries for my template sensor in configuration.yaml.
FYI: sensor.wp1_energy etc. are Shelly 3EM energy sensors.

template:
  - sensor:
    - name: "WP All energy"
      unique_id: C45BBE785AB501
      state: >
        {% set state1 = states('sensor.wp1_energy') %}
        {% set state2 = states('sensor.wp2_energy') %}
        {% set state3 = states('sensor.wp3_energy') %}
        {% if is_number(state1) and is_number(state2) and is_number(state3) %}
          {{ ( state1|float + state2|float + state3|float ) | float }}
        {% endif %}
      state_class: total_increasing
      unit_of_measurement: kWh      
      device_class: energy
1 Like

Hi @zoulou , I’ve used your code but I am still getting the following error message whenever I check the yaml code:

This is how I am writing it down in my yaml:

(sorry for posting screenshots instead of code).

I’ve also tried to adapt your template “WP All energy” to my use case and I am writing it down like this:

# Total Power Consumption
template:
  - sensor:
    - name: "Total Power Consumption"
      unique_id: C45BBE785AB501
      state: >
        {% set state1 = states('sensor.dishwasher_energy_power') %}
        {% set state2 = states('sensor.oven_energy_power') %}
        {% set state3 = states('sensor.coffee_machine_energy_power') %}
        {% set state4 = states('sensor.fridge_energy_power') %}
        {% set state5 = states('sensor.microwave_energy_power') %}
        {% if is_number(state1) and is_number(state2) and is_number(state3) and is_number(state4) and is_number(state5) %}
          {{ ( state1|float + state2|float + state3|float + state4|float state5|float ) | float }}
        {% endif %}
      state_class: total
      unit_of_measurement: W      
      device_class: power

I am now getting a valid configuration:

But I am also getting this error message in my logs:
image

It tells me to check line 92 in my config.yaml and line 92 is the one that starts with “template:”

What am I missing?

There’s a ‘+’ missing between ‘float’ and ‘state5’

1 Like

holy crap I’m dumb :man_facepalming:

Thanks @bertreb for the correction, everything is finally working.

@zoulou I’ve marked your post as solution!

Cheers :beers:

1 Like