Multiplying voltage and current sensors

Hello,

I have been trying to figure this out but I think I am missing a key piece of knowlege here. This question has been asked a number of time and ppl seem to get it to work but I keep ending up in a dead end. I have to assume this may have to do with different versions of HA or different ways of configureing sensors. So here is the configuration of the 2 sensors I like to multiply the values of. These two sensors are working in HA and I get a nice read out as a graphs or number. As MQTT server I am using the HA Mosquitto broker add-on.

My voltage sensor as configured in “config/configuration.yaml”

  - platform: mqtt
    name: "ESP32 Clock"
    unit_of_measurement: 'V'
    state_topic: "mfi115/port3/voltage"

And my current sensor configured also in the same file:

  - platform: mqtt
    name: "Main Ampere"
    unit_of_measurement: 'A'
    state_topic: "mport/amp"
    value_template: "{{ value | round(2) }}"

Both are among other sensors under the “sensor:” heading in configuration.yaml

My configuration.yaml is the default that came with HA, I have added only a bunch of MQTT and SNMP sensors as well as MQTT and SNMP switches. All of that if functional. Here the output of the 2 sensors I like to multiply as shown on “Developer Tools → States”


I would appreciate if someone could show me how this could be done.
Thank You!
Jan P.

Also your mqtt sensors are using the old format

mqtt:
    sensor:
         - name: sensor1
            Etc etc

         - name: sensor2
            Etc etc

Is the new format

Thank you HasQT, that loks rather complex, I have to have a look when I have more time later also regarding the Jinja2 template.

Regarding the formatting, does the same then go for my SNMP sensors ?

snmp:
sensor:

  • name: snmpsensor1
    etc.

It would be non-logic otherwise, does that mean withing the same technology I can mix sensors and switches ? under the heading of mqtt: ?

Regards
Jan P

PS: I am running HA Core Version 2022.10.4

Not sure about snmp.

The sensor isn’t hard.

It’s just sensorsA x(* times symbol) sensorV

That other post is showing all 3 sensors in jinja template in dev tools.

HasQT, the math is not hard, thats true but the format and where to put in the configuration. Its obviously not a physical sensor like a mqtt or snmp data source but a “virtual” sensor derrived from the values of 2 actual physical sensors. So with my old config would there be something like:

  • platform: virtual ?

or with the new config instead of

mqtt:
sensor:

virtual:
sensor:

I seem to not get the basic logic I suspect, a working example would be great.
Thank you
Jan P.

udc is volts and idc is amps, just swap for your sensors.
You can probably add state and device if you want.

device_class: power

and

state_class: measurement or maybe total_increasing, although that may fail because it probably won't be be total increasing.
template:
  - sensor:
      - name: "energy_watts"
        unit_of_measurement: "W" 
        state: >-
          "{{ states('sensor.sma_array_udc') | float * states('sensor.sma_array_idc') | float }}"

Hmmm… that yields no results. What I noticed, for what ever reason vi on the HA system is configured to no wrap lines when they go over the end of the field of view. That cause me to miss some characters when copy and pasting, very odd way to have it as default. Never mind, thats another issue for another time. Once I had that found out I no longer had errors stating up but I still get no results. Here the config I am using now:

template:
  - sensor:
      - name: "energy_watts"
        device_class: power
        unit_of_measurement: "W"
        state: >-
          "{{ states('sensor.esp32_clock_2') | float * states('sensor.main_current') | float }}"

I did remove the rounding of the Ampere reading in that senstor setting cause I thoiugh that causes issues but still no good. It states to be unavailable and if I add the virtual sensor to the overview its greyed out.

Here screen shots of the sensors from the dev tools view.



What am I missing ?
Regards
Jan P.

Looks like it should work. Take that template expression and go into developer tools / templates and paste it in. See if you get a value.

main_current instead of main_ampere

you put the wrong sensor

{{ states('sensor.esp32_clock_2') | float * states('sensor.main_ampere') | float }}

also you don’t need the outer " " quotes, if you don’t want.

Thank you HasQT, that was it. I have to come up with a naming schema that is better. That template testing thing is pretty cool, thanks for pointing that out PeteRage. With that I was able to get it rounding the output to only 1 decimal.

Here is now what seems to work for my application:

template:
  - sensor:
      - name: "energy_watts"
        device_class: power
        unit_of_measurement: "W"
        state: "{{((states('sensor.esp32_clock_2')| float * states('sensor.main_ampere')| float)) | round(1) }}"

Thank you for your help, much appreciated !!

Kind regards
Jan P.

1 Like