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

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!

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

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.

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

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.

Thanks for the quick answer.

Here is the structure:

[
    {
        "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": ""
    }
]```

See this article in the cookbook for an example of how to join messages into one object.

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