Calculate (sum) two entities to one sensor

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:

  - platform: dsmr
    port: /dev/ttyUSB0
    dsmr_version: 5
  - platform: template
        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 }}"

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
        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
        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
        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

the used method is legacy and the new method shoud be

    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.

    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


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

1 Like

I made a template.yaml which is included in configuration.yaml with this template:

- sensor:
  - name: Differenzverbrauch
    unique_id: Differenzverbrauch
    unit_of_measurement: 'W'
    state: '{{ states("sensor.shellyem3_c45bbe6be51f_channel_a_power") |float(0) |round(2)
             + states("sensor.shellyem3_c45bbe6be51f_channel_b_power") |float(0) |round(2) 
             + states("sensor.shellyem3_c45bbe6be51f_channel_c_power") |float(0) |round(2) }}'
    device_class: energy


Hi @dirksenrdh

Can I conclude that you are using Iungo?

grtz chauf

Helper: Subtract/Add/devide/multiply two sensor values please vote


can someone explain to me how to create the sensor.energy_consumption_tarif_1?

does this have anything to do with mqtt?

Hi all,

Can some help me to calculate only the energy consumption if it is >0?
My template now is following:

- platform: template
       friendly_name: Vermogen (totaal)
       unit_of_measurement: "kWh"
       value_template: '{{ states("sensor.youless_consumption_high" )| float + states("sensor.youless_consumption_low") | float }}'

1 Like

Maybe this will guide you.

value_template: >
  {% if is_state('switch.sonoff_1000bee815', 'on') %}
    {{ ((states('sensor.sonoff_1000bee0f9_temperature')|float
    |float | round(1) }}
  {% else %} 0.0 {% endif %}

I have the following template that appears to work:

But I’m getting errors in the yaml config check.

I’m new to yaml but after a lot of research can’t figure out what I’m doing wrong! Can someone help please?

I believe there’s an error in the indentation and " vs ’

  - sensor:
      - name: "house_cons_in_w"
        unique_id: "sensor.house_cons_in_W"
        unit_of_measurement: "W"
        device_class: power
        state: "{{ states('sensor.p1_meter_grid_power_W') | float + states('sensor.pv_total_instant_solar_power_w') | float }}"

Hi all,
I am trying to make a subtraction from an energy value. I do the operations in Jinja2 templating and the result is ok.

But I write the lines in the condifuraion.yaml and give me errors:
Error loading /config/configuration.yaml: invalid key: “{‘states(sensor.es0021000013482086rn_accumulated_consumption) |float - (24991)|float’: None}”
in “/config/configuration.yaml”, line 198, column 0

Somebody can help me, please? Thanks a lot.