How can I compare a value to a result from another node?

I don't know if the heading is the correct terminology, but what I want to do is this:

Every 20 minutes my core system (a combination of Girder and Event Ghost) sends a command to the Sensibo pod with the Sensibo node, based on the temperature in the room. But the heat pump beeps every time it gets a command, and that is a bit annoying. My system does not know for sure if the current state of the heat pump is the same as the signal it sends (the state may have been changed during a restart somewhere in the system or from the Sensibo app), so I would like Node-RED to check the current state and compare it to the state that's sent. If they are the same, the message to Sensibo should not be sent.

The message from the core system is simple, a topic to trigger and then either 18 or 25 as the payload, which is sent to Sensibo. When I do a check with "GetConfig", a Sensibo in node, the result is this:

{"status":"success","result":{"acState":{"timestamp":{"time":"2022-10-11T06:28:45.053423Z","secondsAgo":-1},"on":true,"mode":"heat","targetTemperature":18,"temperatureUnit":"C","fanLevel":"auto","swing":"stopped"}},"_msgid":"40d3a11ff6fdf3c1"}

So targetTemperature is here 18. If the command from the core system is 18 as well, the command should not be sent. So how can I check the current state when the command comes, compare it to the command that comes and then stop if they are the same?

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

Thanks! I'll try that. And then I'll do a script compare of the two values.

And I'm stuck in the starting block... I know I have taken out a value from an object before, but I can't remember how, and I can't find it in my flows either. :flushed:

The path to the value I need is

result.acState.targetTemperature

But if I try to use that in a function node, like this:

msg.payload = result.acState.targetTemperature;
return msg;

I get an error message:

ReferenceError: result is not defined (line 1, col 15)

In the function node you need to append the msg. to the path
e.g.
msg.result.acState.targetTemperature

Thank you! That got me a step further! :+1: Only, I dont get the number 21, I get another object with 21 as the payload.

Temperatur fra varmepumpe : msg : Object
object
status: "success"
result: object
acState: object
timestamp: object
on: true
mode: "heat"
targetTemperature: 21
temperatureUnit: "C"
fanLevel: "auto"
swing: "stopped"
_msgid: "ae504b43b3d89825"
topic: "Temperatur fra varmepumpe"
payload: 21

How can I stip the uneccessary stuff away and only get the payload?

do you need to?

Where you use the value just access it via msg.targetTemperature

Or if you must, use a change mode to move or copy the value to msg.payload.


2 tips...

tip 1

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

tip 2

When copying data from the debug sidebar to paste into the forum - use the "copy value" button as described in Tip 1 (that way we get actual useable JSON!)

Thanks! I didn't need to change it, it seemed, I just used the payload. And actually I knew how to get a single JSON value out of it, but I figured it would be easier to solve if I had the full JSON result. Anyway, I'm almost there! I get the two values into the function node via a join node, but for some reason I can't split it. I thought this would give me one value:

> var temp = msg.payload;
var values = temp.split(';');
msg.payload = values[1];
return msg;

But it errors out:

> TypeError: temp.split is not a function

I'm used to Python (well, not used to, but I can do simple things there...) and LUA (that I'm quite used to!), but this has me stumped.

Edit: Never mind, I found out I had to convert the JSOn to a string first. So I'm getting slowly there. I have this that I need to split:

{"Fra system":21,"Fra varmepumpe":21}

But it's not working out the way I thought, this only gives me "Fra varmepumpe" and nothing more:

var payload =msg.payload;
temp = JSON.stringify(payload);
var system = temp.split(':')[1].split(',')[1];
var varmepumpe = temp.split(':')[2].split(',')[2];
msg.payload = system,varmepumpe;
return msg;

Are you trying to turn your {"Fra system":21,"Fra varmepumpe":21} into an array [21,21] ?
Why?

You have this object: {"Fra system":21,"Fra varmepumpe":21}

In my opinion having a space in the element names makes things more difficult.
Try changing it to {"FraSystem":21,"FraVarmepumpe":21}

Now you can refer to each element as msg.payload.FraSystem or msg.payload.FraVarmepumpe
For example you can test if they are equal with a Switch node
Untitled 1

No, to numbers, so I can compare them, as I said in the first post. I had gotten further, I just dropped the Fra system and Fra varmepumpe, so the combine node gave me the numbers. But the switch node was a smart thing, thanks! That should work better!

And that's weird..again. I have set both to 21 to test, but the switch says they're not equal. I did a function node to check them, and they are both listed as numbers. How can 21 not be equal to 21?

Feed that into a debug node and show us what is there, also show us how you have configured the Switch node.

Actually, please use SteveMcL's tip #2, a debug mode set to show the complete msg object, to copy each of your input messages and past them here:
This one

Temperatur fra varmepumpe : msg : Object
object
status: "success"
result: object
acState: object
timestamp: object
on: true
mode: "heat"
targetTemperature: 21
temperatureUnit: "C"
fanLevel: "auto"
swing: "stopped"
_msgid: "ae504b43b3d89825"
topic: "Temperatur fra varmepumpe"
payload: 21

And this one
{"status":"success","result":{"acState":{"timestamp":{"time":"2022-10-11T06:28:45.053423Z","secondsAgo":-1},"on":true,"mode":"heat","targetTemperature":18,"temperatureUnit":"C","fanLevel":"auto","swing":"stopped"}},"_msgid":"40d3a11ff6fdf3c1"}

(I'm not clear if those are actually the messages you are working with)
I suspect the comparison you seek is way simpler than all your splits and stringifies suggest.

Never mind! I suddenly saw "the error of my ways":

It should be "msg.", not "string" on the compare... Sorry, idiot wetware mistake between the screen and the keyboard. It works now. Thanks for the help!