Check conditions function doesn't evaluate properly



Hello,

I have an flow that I try to setup. It should get 2 values from homeassistant called realtime_produced_power_kw and realtime_consumption_kw and if the produced is more then 1kw higher then the consumption, trigger start or airco. Else if the consumption is higher then produced with 0.5 kw then trigger a stop of airco.

Problem that I face is that realtime_produced_power_kw is updating more often then realtime_consumption_kw and when this happens then I get "Real-time Consumption: NaN".

I tried to store the last value of the realtime_consumption_kw to use it till it gets new values but it's not working for some reason.

My condition check is this

// Get the latest values for produced power and consumption power
let producedPower = flow.get("realtime_produced_power_kw");
let consumptionPower = flow.get("realtime_consumption_kw");

// Check if consumptionPower is valid (not NaN)
if (isNaN(consumptionPower)) {
    // If consumptionPower is NaN, try to use last valid consumption value from flow
    consumptionPower = flow.get("last_valid_consumption_kw");
    
    if (consumptionPower === undefined || isNaN(consumptionPower)) {
        node.warn("No valid consumption data available yet.");
        return null;  // Exit if no valid data available
    } else {
        node.warn("Using last valid consumption data: " + consumptionPower);
    }
} else {
    // Update the flow with the latest valid consumption power
    flow.set("last_valid_consumption_kw", consumptionPower);
}

// Ensure produced power is valid
if (isNaN(producedPower)) {
    node.warn("Produced power value is invalid.");
    return null;  // Exit if produced power is invalid
}

// Log the values for debugging
node.warn("Produced Power: " + producedPower);
node.warn("Real-time Consumption: " + consumptionPower);

// Check if the conditions are met for triggering actions
if (producedPower - consumptionPower >= 1.5 && producedPower > 1.9) {
    node.warn("Conditions met, taking action.");
    return [msg, null];  // Trigger HVAC actions
} else {
    node.warn("No action taken - conditions not met");
    return [null, msg];  // Do nothing if conditions are not met
}

and the debug output for this is

11/9/2024, 11:49:18 AMnode: Check conditions
function : (warn)
"Real-time Consumption: NaN"
11/9/2024, 11:49:18 AMnode: Check conditions
function : (warn)
"No action taken - conditions not met"
11/9/2024, 11:49:18 AMnode: Check conditions
function : (warn)
"No valid consumption data available yet."
11/9/2024, 11:49:27 AMnode: Check conditions
function : (warn)
"No valid consumption data available yet."
11/9/2024, 11:49:27 AMnode: Check conditions
function : (warn)
"Produced Power: 0.714"
11/9/2024, 11:49:27 AMnode: Check conditions
function : (warn)
"Real-time Consumption: 0.714"
11/9/2024, 11:49:27 AMnode: Check conditions
function : (warn)
"No action taken - conditions not met"
11/9/2024, 11:49:28 AMnode: Check conditions
function : (warn)
"Produced Power: 3.0332353"
11/9/2024, 11:49:28 AMnode: Check conditions
function : (warn)
"Real-time Consumption: NaN"
11/9/2024, 11:49:28 AMnode: Check conditions
function : (warn)
"No action taken - conditions not met"
11/9/2024, 11:49:29 AMnode: Check conditions
function : (warn)
"No valid consumption data available yet."
11/9/2024, 11:49:29 AMnode: Check conditions
function : (warn)
"No valid consumption data available yet."
11/9/2024, 11:49:29 AMnode: Check conditions
function : (warn)
"Produced Power: 0.638"
11/9/2024, 11:49:29 AMnode: Check conditions
function : (warn)
"Real-time Consumption: 0.638"
11/9/2024, 11:49:29 AMnode: Check conditions
function : (warn)
"No action taken - conditions not met"
11/9/2024, 11:49:39 AMnode: Check conditions
function : (warn)
"Produced Power: 3.0635878"
11/9/2024, 11:49:39 AMnode: Check conditions
function : (warn)
"Real-time Consumption: NaN"
11/9/2024, 11:49:39 AMnode: Check conditions
function : (warn)
"No action taken - conditions not met"

Is there anything I am overlooking and can fix to get this working please?
Thank you lots in advance

firstly note, i do not use HA so may be missing some under the covers magic, but from what i see here: you are not setting flow.realtime_consumption_kw and flow.realtime_produced_power_kw.

Assuming those 2 blue HA nodes on the left send out the values for realtime_consumption_kw and realtime_produced_power_kw, why not simply join the 2 values into a single payload:

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

Then you will always have the latest values.

Just be sure both values have unique topics and to set the join node to "After a number of message parts 2 - and every subsequent message."

Demo

chrome_dKVn66dnxY

[{"id":"4924d89b2e1e9719","type":"join","z":"dc3aa5374a70eb0a","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","useparts":false,"accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":970,"y":240,"wires":[["5c4202ee8c059e38"]]},{"id":"6da76b9a48cf9027","type":"change","z":"dc3aa5374a70eb0a","name":"set topic to realtime_consumption_kw","rules":[{"t":"set","p":"topic","pt":"msg","to":"realtime_consumption_kw","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":200,"wires":[["4924d89b2e1e9719"]]},{"id":"1aa242ed1340626a","type":"change","z":"dc3aa5374a70eb0a","name":"set topic to realtime_produced_power_kw","rules":[{"t":"set","p":"topic","pt":"msg","to":"realtime_produced_power_kw","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":300,"wires":[["4924d89b2e1e9719"]]},{"id":"05a23a2fb6775b33","type":"inject","z":"dc3aa5374a70eb0a","name":"faking realtime_produced_power_kw","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"12","payloadType":"num","x":620,"y":260,"wires":[["1aa242ed1340626a"]]},{"id":"bf526e763078ebce","type":"inject","z":"dc3aa5374a70eb0a","name":"faking realtime_consumption_kw","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"11.2","payloadType":"num","x":630,"y":160,"wires":[["6da76b9a48cf9027"]]},{"id":"5c4202ee8c059e38","type":"debug","z":"dc3aa5374a70eb0a","name":"debug 11","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1020,"y":300,"wires":[]}]

Then you can simply access the values in your function using msg.payload.realtime_consumption_kw and msg.payload.realtime_produced_power_kw

I tried this code based on your example:

[
    {
        "id": "5b18c39baec77c7b",
        "type": "tab",
        "label": "Flow 4",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "3e1ee0dafce6593a",
        "type": "server-state-changed",
        "z": "5b18c39baec77c7b",
        "name": "realtime_produced_power_kw",
        "server": "e088f9a18be9efaf",
        "version": 6,
        "outputs": 1,
        "exposeAsEntityConfig": "",
        "entities": {
            "entity": [
                "sensor.realtime_produced_power_kw"
            ],
            "substring": [],
            "regex": []
        },
        "outputInitially": false,
        "stateType": "num",
        "ifState": "",
        "ifStateType": "str",
        "ifStateOperator": "is",
        "outputOnlyOnStateChange": true,
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "ignorePrevStateNull": false,
        "ignorePrevStateUnknown": false,
        "ignorePrevStateUnavailable": false,
        "ignoreCurrentStateUnknown": false,
        "ignoreCurrentStateUnavailable": false,
        "outputProperties": [
            {
                "property": "realtime_produced",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            }
        ],
        "x": 380,
        "y": 200,
        "wires": [
            [
                "0f838cacaf888d91"
            ]
        ]
    },
    {
        "id": "f03f55139e883fc4",
        "type": "debug",
        "z": "5b18c39baec77c7b",
        "name": "debug 12",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1360,
        "y": 220,
        "wires": []
    },
    {
        "id": "0f838cacaf888d91",
        "type": "change",
        "z": "5b18c39baec77c7b",
        "name": "set topic to realtime_produced_power_kw",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "realtime_produced_power_kw",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 460,
        "y": 280,
        "wires": [
            [
                "d880514ac0a78e85",
                "f03f55139e883fc4"
            ]
        ]
    },
    {
        "id": "f3af83e8ff22dc05",
        "type": "change",
        "z": "5b18c39baec77c7b",
        "name": "set topic to realtime_consumption_kw ",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "realtime_consumption_kw",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1010,
        "y": 280,
        "wires": [
            [
                "d880514ac0a78e85",
                "f03f55139e883fc4"
            ]
        ]
    },
    {
        "id": "0fb2c559c8d0ed8a",
        "type": "server-state-changed",
        "z": "5b18c39baec77c7b",
        "name": "realtime_consumption_kw",
        "server": "e088f9a18be9efaf",
        "version": 6,
        "outputs": 1,
        "exposeAsEntityConfig": "",
        "entities": {
            "entity": [
                "sensor.realtime_power_kw"
            ],
            "substring": [],
            "regex": []
        },
        "outputInitially": false,
        "stateType": "num",
        "ifState": "",
        "ifStateType": "str",
        "ifStateOperator": "is",
        "outputOnlyOnStateChange": true,
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "ignorePrevStateNull": false,
        "ignorePrevStateUnknown": false,
        "ignorePrevStateUnavailable": false,
        "ignoreCurrentStateUnknown": false,
        "ignoreCurrentStateUnavailable": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            }
        ],
        "x": 710,
        "y": 140,
        "wires": [
            [
                "f3af83e8ff22dc05"
            ]
        ]
    },
    {
        "id": "d880514ac0a78e85",
        "type": "join",
        "z": "5b18c39baec77c7b",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "useparts": false,
        "accumulate": true,
        "timeout": "",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 1210,
        "y": 420,
        "wires": [
            [
                "f03f55139e883fc4"
            ]
        ]
    },
    {
        "id": "e088f9a18be9efaf",
        "type": "server",
        "name": "Home Assistant",
        "version": 5,
        "addon": false,
        "rejectUnauthorizedCerts": true,
        "ha_boolean": "y|yes|true|on|home|open",
        "connectionDelay": true,
        "cacheJson": true,
        "heartbeat": false,
        "heartbeatInterval": "30",
        "areaSelector": "friendlyName",
        "deviceSelector": "friendlyName",
        "entitySelector": "friendlyName",
        "statusSeparator": ": ",
        "statusYear": "hidden",
        "statusMonth": "short",
        "statusDay": "numeric",
        "statusHourCycle": "default",
        "statusTimeFormat": "h:m",
        "enableGlobalContextStore": false
    }
]

And the output is

11/9/2024, 12:58:50 PMnode: Check conditions
function : (warn)
"Produced Power: 2.876947"
11/9/2024, 12:58:50 PMnode: Check conditions
function : (warn)
"Real-time Consumption: NaN"
11/9/2024, 12:58:50 PMnode: Check conditions
function : (warn)
"No action taken - conditions not met"
11/9/2024, 12:58:50 PMnode: 618207cb3d9d48e4
msg : string[63]
"Message missing key property 'msg.topic' - cannot add to object"
11/9/2024, 12:58:50 PMnode: d880514ac0a78e85
msg : string[63]
"Message missing key property 'msg.topic' - cannot add to object"
11/9/2024, 12:58:50 PMnode: debug 12
msg.payload : string[26]
"realtime_produced_power_kw"
11/9/2024, 12:59:00 PMnode: Check conditions
function : (warn)
"Produced Power: 2.873873"
11/9/2024, 12:59:00 PMnode: Check conditions
function : (warn)
"Real-time Consumption: NaN"
11/9/2024, 12:59:00 PMnode: Check conditions
function : (warn)
"No action taken - conditions not met"
11/9/2024, 12:59:00 PMnode: 618207cb3d9d48e4
msg : string[63]
"Message missing key property 'msg.topic' - cannot add to object"
11/9/2024, 12:59:00 PMnode: d880514ac0a78e85
msg : string[63]
"Message missing key property 'msg.topic' - cannot add to object"
11/9/2024, 12:59:00 PMnode: debug 12
msg.payload : string[26]
"realtime_produced_power_kw"
11/9/2024, 12:59:10 PMnode: Check conditions
function : (warn)
"Produced Power: 2.871841"
11/9/2024, 12:59:10 PMnode: Check conditions
function : (warn)
"Real-time Consumption: NaN"
11/9/2024, 12:59:10 PMnode: Check conditions
function : (warn)
"No action taken - conditions not met"
11/9/2024, 12:59:10 PMnode: 618207cb3d9d48e4
msg : string[63]
"Message missing key property 'msg.topic' - cannot add to object"
11/9/2024, 12:59:10 PMnode: d880514ac0a78e85
msg : string[63]
"Message missing key property 'msg.topic' - cannot add to object"
11/9/2024, 12:59:10 PMnode: debug 12
msg.payload : string[26]
"realtime_produced_power_kw"
11/9/2024, 12:59:20 PMnode: Check conditions
function : (warn)
"Produced Power: 2.8725623"
11/9/2024, 12:59:20 PMnode: Check conditions
function : (warn)
"Real-time Consumption: NaN"
11/9/2024, 12:59:20 PMnode: Check conditions
function : (warn)
"No action taken - conditions not met"
11/9/2024, 12:59:20 PMnode: 618207cb3d9d48e4
msg : string[63]
"Message missing key property 'msg.topic' - cannot add to object"
11/9/2024, 12:59:20 PMnode: d880514ac0a78e85
msg : string[63]
"Message missing key property 'msg.topic' - cannot add to object"
11/9/2024, 12:59:20 PMnode: debug 12
msg.payload : string[26]
"realtime_produced_power_kw"
11/9/2024, 12:59:27 PMnode: Check conditions
function : (warn)
"Produced Power: 2.781"
11/9/2024, 12:59:27 PMnode: Check conditions
function : (warn)
"Real-time Consumption: 2.781"
11/9/2024, 12:59:27 PMnode: Check conditions
function : (warn)
"No action taken - conditions not met"

As you can see I still get "Produced Power: 2.781" and "Real-time Consumption: 2.781". This is not right since the values that come in are different. Can't understand what I do wrong here

Set that to topic

NOTE:

It is really difficult to decipher output when you use a generic debug for all things.

Please in future, use separate named debugs so it is easier to see where the issue is happening





I have this in my flow.


I even try it like this but without any luck :frowning:

You are setting payload to a string! I already said ...

↑ Set that to topic


did you not read the link I sent - it explains how to use the join node.

Thank you for your patience. Really appreciated.
I did change that to topic and now I have this

where realtime_produced_power_kw is undefined and it is set to topic

  1. Please use separate named debugs like I showed you.
  1. Show me what comes out of all 3 debug nodes.

Thanks to your patience and help I think I saw something that was wrong

this seems to not be set to payload. Corrected it and thanks to your separated debugs I managed to find what was going on and now it looks like it's producing the proper output

Now back to the drawing board to change the conditional function to read this values.

Many many thanks for your help and suggestions. Really appreciated

2 Likes