# How to Calculate Power (P) from Voltage and Current Values in Node-RED?

**URL:** https://discourse.nodered.org/t/how-to-calculate-power-p-from-voltage-and-current-values-in-node-red/86936
**Category:** General
**Tags:** function-node
**Created:** [2 April 2024 11:04 UTC](https://discourse.nodered.org/t/how-to-calculate-power-p-from-voltage-and-current-values-in-node-red/86936 "2024-04-02T11:04:13Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![noctis-61](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/noctis-61/32/88170_2.png) [@noctis-61](https://discourse.nodered.org/u/noctis-61)
#### Post date: [2 April 2024 11:04 UTC](https://discourse.nodered.org/t/how-to-calculate-power-p-from-voltage-and-current-values-in-node-red/86936/1 "2024-04-02T11:04:13Z")

</div>

Hello,

I'm working on a project in Node-RED where I receive voltage from a ADS1115 and current from a ina219. I want to calculate the power (P) using these values, but i dont know how.

However, when I try to add both inputs into a Function node and write the following script:

var voltage = msg.payload;  
var current = msg.payload;  
var power = voltage \* current;  
var newMsg = { payload: power };  
return newMsg;

I get some weird values, and If I rename the payloads to `msg.payload.voltage` and `msg.payload.current` as suggested in some examples, I encounter error messages "NaN".

Could someone please provide guidance on how to accomplish this? I need to perform the calculation within the Function node without changing the message structure. Any examples or suggestions would be greatly appreciated. Thank you!

---

<div class="post-metadata">

### Author: ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)
#### Post date: [2 April 2024 11:18 UTC](https://discourse.nodered.org/t/how-to-calculate-power-p-from-voltage-and-current-values-in-node-red/86936/2 "2024-04-02T11:18:25Z")

</div>

Doesn't the INA219 provide all the data you need without a ADS1115?

---

<div class="post-metadata">

### Author: ![noctis-61](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/noctis-61/32/88170_2.png) [@noctis-61](https://discourse.nodered.org/u/noctis-61)
#### Post date: [2 April 2024 11:26 UTC](https://discourse.nodered.org/t/how-to-calculate-power-p-from-voltage-and-current-values-in-node-red/86936/3 "2024-04-02T11:26:55Z")

</div>

Usually, an INA219 measures both voltage and current, that's correct. However, there's an issue (at least with the pre-built Node-RED flow) where values above approximately 16V are only displayed as mismatched negative values. It's a known problem, which is why I've opted to use an ADS1115 for voltage measurement instead.

---

<div class="post-metadata">

### Author: ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)
#### Post date: [2 April 2024 11:31 UTC](https://discourse.nodered.org/t/how-to-calculate-power-p-from-voltage-and-current-values-in-node-red/86936/4 "2024-04-02T11:31:19Z")

</div>

> [@noctis-61](#):
>
> I need to perform the calculation within the Function node without changing the message structure.

If you can post the flow and message structure you will probably get some help.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [2 April 2024 11:34 UTC](https://discourse.nodered.org/t/how-to-calculate-power-p-from-voltage-and-current-values-in-node-red/86936/5 "2024-04-02T11:34:12Z")

</div>

> [@noctis-61](#):
>
> var voltage = msg.payload;  
> var current = msg.payload;  
> var power = voltage \* current;  
> var newMsg = { payload: power };  
> return newMsg;

Voltage and Current cannot have the same property (msg.payload). So i assume the payloads are in separate messages. You will have to use a join node to join the 2 messages before the function node. Or save the values to context.

---

<div class="post-metadata">

### Author: ![noctis-61](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/noctis-61/32/88170_2.png) [@noctis-61](https://discourse.nodered.org/u/noctis-61)
#### Post date: [2 April 2024 12:26 UTC](https://discourse.nodered.org/t/how-to-calculate-power-p-from-voltage-and-current-values-in-node-red/86936/6 "2024-04-02T12:26:54Z")

</div>

Thanks for the quick answer.

Here is the structure:

 ![111](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/7/d7b32ed3e289fc2fbd400cfc21fe24ebca39d1f1.png)

````auto
[
    {
        "id": "d0b817e64cb5e8f3",
        "type": "tab",
        "label": "Flow 3",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "5a1a25108a44a4b7",
        "type": "ina-sensor",
        "z": "d0b817e64cb5e8f3",
        "name": "",
        "handle": "68a7603e9b0ab180",
        "x": 270,
        "y": 240,
        "wires": [
            [],
            [
                "6bdf32801ece1206"
            ]
        ]
    },
    {
        "id": "003214632c94b6b5",
        "type": "mqtt in",
        "z": "d0b817e64cb5e8f3",
        "name": "",
        "topic": "ina_219_1",
        "qos": "2",
        "datatype": "auto-detect",
        "broker": "4d29b73341f90358",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 100,
        "y": 240,
        "wires": [
            [
                "5a1a25108a44a4b7"
            ]
        ]
    },
    {
        "id": "ae5ce491679590df",
        "type": "inject",
        "z": "d0b817e64cb5e8f3",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "3",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 110,
        "y": 300,
        "wires": [
            [
                "8fe81bc608d61bec"
            ]
        ]
    },
    {
        "id": "8fe81bc608d61bec",
        "type": "ads1x15_i2c",
        "z": "d0b817e64cb5e8f3",
        "property": "payload",
        "name": "ADS1115",
        "i2c_device_number": 1,
        "chip": "0x01",
        "i2c_address": "0x48",
        "inputsForChannel": "singleEnded",
        "singleEndedChannel0": true,
        "singleEndedChannel1": false,
        "singleEndedChannel2": false,
        "singleEndedChannel3": false,
        "differentialChannel0_1": true,
        "differentialChannel0_3": false,
        "differentialChannel1_3": false,
        "differentialChannel2_3": false,
        "samplesPerSecond0": "920",
        "samplesPerSecond1": "128",
        "progGainAmp": "6144",
        "x": 260,
        "y": 300,
        "wires": [
            [
                "d3269ae4faf888f3"
            ]
        ]
    },
    {
        "id": "d3269ae4faf888f3",
        "type": "change",
        "z": "d0b817e64cb5e8f3",
        "name": "extract_value_from_Channel_A0",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload[\"/dev/i2c-1\"].a1115[\"0x4ds8\"].singleEnded.channel_1.Volts",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 480,
        "y": 300,
        "wires": [
            [
                "d33f98b9d8ab1426"
            ]
        ]
    },
    {
        "id": "d33f98b9d8ab1426",
        "type": "voltage_undivider",
        "z": "d0b817e64cb5e8f3",
        "name": "Voltagedivider",
        "property": "payload",
        "Voltage_Input": "V-Volts",
        "Resistor_1": "2200",
        "Resistor_1Types": "Ω-Ohm",
        "Resistor_2": "200",
        "Resistor_2Types": "Ω-Ohm",
        "Voltage_Output": "V-Volts",
        "Round_Output": "Thousandths",
        "x": 740,
        "y": 300,
        "wires": [
            []
        ]
    },
    {
        "id": "6bdf32801ece1206",
        "type": "smooth",
        "z": "d0b817e64cb5e8f3",
        "name": "",
        "property": "payload",
        "action": "mean",
        "count": "3",
        "round": "3",
        "mult": "single",
        "reduce": false,
        "x": 720,
        "y": 240,
        "wires": [
            []
        ]
    },
    {
        "id": "68a7603e9b0ab180",
        "type": "ina-sensor-manager",
        "address": "0x40",
        "delay": "250",
        "ohms": "0.01",
        "customResistor": false
    },
    {
        "id": "4d29b73341f90358",
        "type": "mqtt-broker",
        "name": "Mqtt INPUT",
        "broker": "127.0.0.1",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "autoUnsubscribe": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthRetain": "false",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closeRetain": "false",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willRetain": "false",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    }
]```
````

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [2 April 2024 13:09 UTC](https://discourse.nodered.org/t/how-to-calculate-power-p-from-voltage-and-current-values-in-node-red/86936/7 "2024-04-02T13:09:41Z")

</div>

See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [1 July 2024 13:10 UTC](https://discourse.nodered.org/t/how-to-calculate-power-p-from-voltage-and-current-values-in-node-red/86936/8 "2024-07-01T13:10:29Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
