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