Calculate (sum) two entities to one sensor

Hello,

I’m brand new to HA and learning a lot in a short time. A lot of issues I could find on the forum, but there is one thing I couldn’t figure out.

The situation:

I have a smart energy meter for power and gas usage, which I can read with a P1 cable to my raspberry pi 4 where HA is running on. I got the readings working from the meter which is really nice. The meter works with two tariffs, low and high. Unfortunately sensor “total energy consumption” is coming in without a value. So in the front-end of HA I see now “total energy consumption tarif 1” and “total energy consumption tarif 2”. The additional I want is this two added up so I have the total energy consumption.

Now I started searching how to do this. I know I have to make a “sensor” in the configuration.yaml file. But the question is the coding, still a noob in this. So I need to sum two entities, the outcome will be the new sensor.

This is what I have so far:

sensor:
  - platform: template
    sensors:
        energie_verbruik_totaal:
            friendly_name: "Energie Verbruik (totaal)"
            unit_of_measurement: 'kWh'
            value_template: "{{ entity:sensor.energy_consumption_tarif_1 +
            entity:sensor.energy_consumption_tarif_2 }}"

I’ve tried different variants but I keep getting an error, propably because the code is wrong:

  • Error loading /config/configuration.yaml: mapping values are not allowed here in “/config/configuration.yaml”, line 43, column 26
  • Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token ‘end of print statement’, got ‘:’) for dictionary value @ data[‘sensors’][‘energie_verbruik_totaal’][‘value_template’]. Got ‘{{ entity:sensor.energy_consumption_tarif_1 + entity:sensor.energy_consumption_tarif_2 }}’. (See ?, line ?).

Could someone help me pointing the correct direction, that would be great!

Cheers, Darryl.

3 Likes

Something like:

{{ states("sensor.energy_consumption_tarif_1") |float + states("sensor.energy_consumption_tarif_2") | float }}
2 Likes

Try

{{ 
float(states('sensor.energy_consumption_tarif_1')) + float(states('sensor.energy_consumption_tarif_2')) 
}}

Thanks for helping, but both ways don’t work.

Am I doing it correctly this way:

sensor:
  - platform: template
    sensors:
        energie_verbruik_totaal:
            friendly_name: "Energie Verbruik (totaal)"
            unit_of_measurement: 'kWh'
            value_template: {{ float(states('sensor.energy_consumption_tarif_1)) + float(states('sensor.energy_consumption_tarif_2)) 
}}

I missed closing quotes in my initial response but added them afterwards. Please the-check

I am still getting errors:

  • Error loading /config/configuration.yaml: while parsing a block mapping in “/config/configuration.yaml”, line 38, column 13 expected , but found ‘’ in “/config/configuration.yaml”, line 40, column 41
  • Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token ‘,’, got ‘sensor’) for dictionary value @ data[‘sensors’][‘energie_verbruik_totaal’][‘value_template’]. Got “{{ float(states('sensor.energy_consumption_tarif_1)) + float(states('sensor.energy_consumption_tarif_2)) }}”. (See ?, line ?).
  • Error loading /config/configuration.yaml: invalid key: “OrderedDict([(“float(states('sensor.energy_consumption_tarif_1)) + float(states('sensor.energy_consumption_tarif_2))”, None)])” in “/config/configuration.yaml”, line 40, column 0
  • Error loading /config/configuration.yaml: invalid key: “OrderedDict([(“float(states(‘sensor.energy_consumption_tarif_1’)) + float(states(‘sensor.energy_consumption_tarif_2’))”, None)])” in “/config/configuration.yaml”, line 40, column 0

Hi @darryl12123 - what I do to debug these type of issues is to use the template editor under developer tools (You should see Developer Tools on the left hand side of Home Assistant UI). Then select Template from the tabs. Home Assistant uses Jinja2 templating. Then copy the value_template into the template editor window. On the right hand side you see the result of the templating code. If there is an error it will show up in red, explaining where you went wrong.

This worked for me:

{{ states("sensor.energy_consumption_tarif_1") |float + states("sensor.energy_consumption_tarif_2") | float }}

…it displayed 0.0 - as obviously I dont have those sensors.

sensor:
  - platform: template
    sensors:
        energie_verbruik_totaal:
            friendly_name: "Energie Verbruik (totaal)"
            unit_of_measurement: 'kWh'
            value_template: '{{ states("sensor.energy_consumption_tarif_1") |float + states("sensor.energy_consumption_tarif_2") | float }}'

HTH

5 Likes

Can you please show your full configuration.yaml file?

Hi @ozczecho & @Burningstone ,

That tool is really nice, thanks for pointing it out! I I copied your code and it seems to work in the template editor. When I put the code in config.yaml file I loose all the readings on the meter.

See below my full config.yaml file:


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

# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
#   base_url: example.duckdns.org:8123

# Text to speech
tts:
  - platform: google_translate
group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

tado:
    username: *********
    password: *********
    
sensor:
  - platform: dsmr
    port: /dev/ttyUSB0
    dsmr_version: 5
    
group:
  meter_readings:
    name: Meter readings
    entities:
      - sensor.energy_consumption_tarif_1
      - sensor.energy_consumption_tarif_2
      - sensor.energy_production_tarif_1
      - sensor.energy_production_tarif_2
      - sensor.gas_consumption

homeassistant:
  customize: !include customize.yaml

sensor:
  - platform: template
    sensors:
        energie_verbruik_totaal:
            friendly_name: "Energie Verbruik (totaal)"
            unit_of_measurement: 'kWh'
            value_template: '{{ states("sensor.energy_consumption_tarif_1") |float + states("sensor.energy_consumption_tarif_2") | float }}'
1 Like

See below screenshot of the card when code is active and inactive:

Code active:

Code inactive:

You have two sensor: sections, that’s not allowed like this. Put all sensor configuration in the same sensor: section.

Like this:

sensor:
  - platform: dsmr
    port: /dev/ttyUSB0
    dsmr_version: 5
  - platform: template
    sensors:
      energie_verbruik_totaal:
        friendly_name: "Energie Verbruik (totaal)"
        unit_of_measurement: 'kWh'
        value_template: "{{ states('sensor.energy_consumption_tarif_1') |float + states('sensor.energy_consumption_tarif_2') | float }}"
4 Likes

Aaah that is the issue, great! Everything is working now, amazing! Thanks!

I just have an additional question regarding entities;

When I added the meter in the config.yaml file, a lot of entities were added in the list but as you can see in the config.yaml file there are only 5 entities listed. All the extra entities are also not working (no value showing). How and why are those extra entities in the list and are they not working because it is not supported?

Thanks anyway!

this works for me, thanks everyone!!!
Esto me sirvió al día de hoy. Gracias a todos!!

  - platform: template
    sensors:    
      energia_neta_solar_red:
        friendly_name: Consumo neto de Casa
        unit_of_measurement: 'W'
        value_template: >-
          {% if states('sensor.wibeee_001ec02c0df0_power_factor_l1')|float > 0 %}
            {{ states('sensor.wibeee_001ec02c0df0_active_power_l1')|float + states('sensor.microinversor_1_e_power')|float + states('sensor.microinversor_2_o_power')|float + states('sensor.available_channel_1_power')|float
            }}
          {% else %}
            {{ states('sensor.microinversor_1_e_power')|float + states('sensor.microinversor_2_o_power')|float + states('sensor.available_channel_1_power')|float - states('sensor.wibeee_001ec02c0df0_active_power_l1')|float}}
          {% endif %}
1 Like

apparently I am even more a novice with templating.
I have this code in the sensor: section of my Yaml file

  - platform: template
    sensors:
      P1_totaal:
        friendly_name: Vermogen (totaal)
        unit_of_measurement: "kWh"
        value_template: '{{ states("sensor.power_consumption") |float - states("sensor.power_production") | float }}'

I expect that I can use something like sensor.P1_totaal to be available for displaying the newly calculated value. But I can’t find it. where am I going wrong?

P1_totaal needs to be all lowercase.

1 Like

still no luck.
the Template test section of developer tools does not show any errors and the calculation is fine.
the configuration is ok according to the config page.
and all my MQTT sensors are working fine


  - platform: mqtt
    name: P1_Gas_day
    state_topic: "SmartConsult/DSMR"
    value_template: "{{ value_json.Gas_day}}"
    device_class: gas
    state_class: measurement
    unit_of_measurement: m³

  - platform: template
    sensors:
      p1_totaal:
        friendly_name: Vermogen (totaal)
        unit_of_measurement: "kWh"
        value_template: '{{ states("sensor.power_consumption") |float - states("sensor.power_production") | float }}'

If I look to the mqtt sensor, I can specify the name with lower and uppercase, but indeed as search the sensor the it is sensor.p1_gas_day all in lowercase.

and according to this post

https://community.home-assistant.io/t/difference-between-sensor-platform-template-and-template-sensor/389248

the used method is legacy and the new method shoud be

template:
  sensor:
    name: p1_total
    unit_of_measurement: 'kWh'
    state: '{{ states("sensor.power_consumption") |float - states("sensor.power_production") | float }}'

and after a reboot: Presto. it works

Hi everyone,

I tried combining two DC input values from my PV-Panels to one sensor like mentioned here: Template - Home Assistant
and in this thread.

template:
  sensor:
    name: "DC Power All"
    unit_of_measurement: W
    state: "{{ states('sensor.scb_dc1_power') | float + states('sensor.scb_dc2_power') | float }}"

But the sensor is not shown in HA and I can’t see any errors in the log.
So what am I doing wrong?

OK I found out, that when having more than one customized sensor, they all should be placed under “template:” like this

template:
  sensor:
    ....
  sensor:
    ....

Having multiple “template:” entries in configuration.yaml doesn’t seem to work.

1 Like