Combining two sensors

I have two sensors for reading out my DMSR P1 port. It sends separate data for production as for consumption.

I would like to combine them in to one sensor with a positive value in case of consumption and a negative value in case of Production.

These are my two sensors currently:

- platform: mqtt
  name: power_current_consumption
  state_topic: "p1monitor/smartmeter/consumption_kw"
  unit_of_measurement: "Kwh"
  value_template: "{{ value | float }}"

- platform: mqtt
  name: power_current_production
  state_topic: "p1monitor/smartmeter/production_kw"
  unit_of_measurement: "Kwh"
  value_template: "{{ value | float }}"

Is there a way of combining them?

Thanks

Something like:

- platform: template
  name: "net usage"
  unit_of_measurement: "Kwh"
  value_template: "{{ states('sensor.power_current_production') | float - states('sensor.power_current_consumption') | float }}"

Missing a sensor. in that template, and layout as per legacy format. Corrected version, and note that units are “kWh” not “Kwh”:

- platform: template
  sensors:
    net_usage:
      unit_of_measurement: "kWh"
      value_template: "{{ states('sensor.power_current_production')|float - states('sensor.power_current_consumption')|float }}"

That’s what you get when copy pasting on phone…
Everything doesn’t fit the screen.

2 Likes

Thanks it works perfect. I only switch Consumption and production because I wanted to have the production as the negative value