[SOLVED] Node-Red and previous.state

Hello,

after reading the great tutorials on node-red i am playing around with it now.

do you know, if there is a possibility to get the previous.state in node-red also? i am trying to convert this rule:

// Luftfeuchtigkeit im Wohnzimmer steigt von unter 60% auf >= 60% an oder sinkt von >= 60% auf unter 60% ab --> iOS-Benachrichtigung (Broadcast)
rule "Luftfeuchtigkeit_Wohnzimmer_zu_hoch"
when
	Item Tado_zone_Wohnzimmer_Humidity changed
then
	if ((Tado_zone_Wohnzimmer_Humidity.state >= 60 && previousState <  60) && (Rule_Luftfeuchtigkeit.state == ON)) {
		sendBroadcastNotification(
		"Im Wohnzimmer ist die Luftfeuchtigkeit zu hoch ("+Tado_zone_Wohnzimmer_Humidity.state+"%).")
	}
end
rule "Luftfeuchtigkeit_Wohnzimmer_wieder_ok"
when
	Item Tado_zone_Wohnzimmer_Humidity changed
then
	if ((Tado_zone_Wohnzimmer_Humidity.state < 60 && previousState >=  60) && (Rule_Luftfeuchtigkeit.state == ON))  {
		sendBroadcastNotification(
		"Im Wohnzimmer ist die Luftfeuchtigkeit wieder ok ("+Tado_zone_Wohnzimmer_Humidity.state+"%).")
	}
end

thanks for help!

yours, alex

Not in front of pc so can’t answer exactly but every oh node has 2 outputs, the lower one has all detail, including previous state if I’m not mistaking.
So put a debug node next to the lower output and then collapse the full output and you should find it back,

thank you, i tried out but i just get these informations into the debug log:

12.8.2018, 19:20:54node: 2794de80.8ed80a
msg.payload : Object
object
topic: "smarthome/items/Tado_zone_Wohnzimmer_Humidity/state"
payload: object
type: "Decimal"
value: "54.90"
type: "ItemStateEvent"

Je zou 2 messages moeten krijgen een ItemStateEvent en een ItemStateChangedEvent. Het ItemStateChanged event heeft in de payload een value en een oldvalue.

Dus capture de itemstatechangevent ipv de ItemStateEvent die je nu bekijkt.

Zie screenshot hieronder

1 Like

The oldValue is available in the second output of the openhab node:

2 Likes

thanks for your help! just learned, that the oldValue is only avaible, if the value really changed. thats the reason why i didnt see it.

i think i fell in love with node red as alternative rule engine :smile:

for everyone thats interested in: this is the example rule “translated” to node red.

I did too at some point and moved some rules to node-red but I went back to the rule engine. It is far more powerful for complicated rules. Also, you need to set flow or global variables to be able to query states in functions.
It still has it’s uses.
For example I moved today three items updates to node-red. Instead of the http binding.

thanks for sharing your experience. can you explain, at which point a rules gets too complicated you are going to prefer to build the rule in openhab itself?

also i would be interested whats the best practise / what are the cases to create seperated flows. i think, if i use a seperate flow for each rule, it doenst take long to loose the overview of the rules.

A complicated rule can use lots of nodes. It gets messy. Concise code is much easier to read and follow.

My point exactly. Now I just use node-red to get information if there isn’t a binding for OH. Or there is a function like the ramp-thermostat.
The rules logic are all in OH.

Hi

Would you be kind enough to post the exact content of your “Switch” node for “get.oldValue”
please?

I’m not entirely sure what I’m supposed to put in the Switch to extract that value.

(I’m sure it will be obvious when I see it)

Thanks

Stuart

Update

I’ve tried using a change mode and setting msg.payload to msg.payload.payload.oldValue but for some strange reason the debug window shows the oldValue correctly, but this new payload is exactly the same as the current state payload

@vzorglub
You wouldn’t happen to know the secret?

Ping me tomorrow to remind me.
I’ll have a look

Thanks Vincent.

There’s no rush, this is just something that came into my mind that I’d like to explore.

Create a function node:

if (msg.payload.type = 'ItemStateChangedEvent') {
    var oldValue = msg.payload.payload.oldValue;
    // Do what you want with the variable oldValue
} else {
    return;
}
msg.payload = oldValue;
return msg;
1 Like

Thanks a million. :smile: :+1:

I’ll give that go later today, or tomorrow.

Hi

Where am I supposed to put that code?

If I put it in a Function node, it throws errors.

If I put it in an expression of a Change node, not a lot happens other than lots of red makers.

What’s really annoying is that I can see a Topic with ItemStateChangedEvent, but how do I simply move that to the main payload?

02/12/2019, 11:17:56[node: ecd9f7a5.ad3468](http://192.168.178.42:1880/#)msg.payload : Object

object

topic: "smarthome/items/4RY_Intake_UnderStair/statechanged"

payload: object

type: "OnOff"

value: "OFF"

oldType: "OnOff"

oldValue: "ON"

type: "ItemStateChangedEvent"

@e36Alex has done it with a change node, but I just can’t work out how.

Try that:

if (msg.type = 'ItemStateChangedEvent') {
    var oldValue = msg.payload.payload.oldValue;
    // Do what you want with the variable oldValue
} else {
    return;
}
msg.payload = oldValue;
return msg;

Thanks

When I paste it into a Function node, it instantly shows this “Assignment in conditional expression” error

and “oldValue used out of scope”

image

Sorry:

var oldValue;
if (msg.type == 'ItemStateChangedEvent') {
    oldValue = msg.payload.payload.oldValue;
    // Do what you want with the variable oldValue
} else {
    return;
}
msg.payload = oldValue;
return msg;

No
 Nothing on the output


However


This does work


Contents of Function Node

var oldValue;
if (msg.payload.type == 'ItemStateChangedEvent') {
    oldValue = msg.payload.payload.oldValue;
    // Do what you want with the variable oldValue
} else {
    return;
}
msg.payload = oldValue;
return msg;

NodeRed flow

[
    {
        "id": "d54d5bb5.417438",
        "type": "debug",
        "z": "de1c75c7.742f68",
        "name": "",
        "active": true,
        "tosidebar": false,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "x": 800,
        "y": 580,
        "wires": []
    },
    {
        "id": "fd9e0f56.414e7",
        "type": "debug",
        "z": "de1c75c7.742f68",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 790,
        "y": 460,
        "wires": []
    },
    {
        "id": "b371164b.0ea8b8",
        "type": "function",
        "z": "de1c75c7.742f68",
        "name": "old Value",
        "func": "var oldValue;\nif (msg.payload.type == 'ItemStateChangedEvent') {\n    oldValue = msg.payload.payload.oldValue;\n    // Do what you want with the variable oldValue\n} else {\n    return;\n}\nmsg.payload = oldValue;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 650,
        "y": 460,
        "wires": [
            [
                "fd9e0f56.414e7",
                "d54d5bb5.417438"
            ]
        ]
    },
    {
        "id": "d38817d4.6d5f98",
        "type": "openhab2-in",
        "z": "de1c75c7.742f68",
        "name": "Understairs Light",
        "controller": "deaa963.282a968",
        "itemname": "4RY_Intake_UnderStair",
        "x": 210,
        "y": 160,
        "wires": [
            [
                "1da423ef.5345dc",
                "596d57c9.f74a98"
            ],
            [
                "c7790272.fc3de",
                "b371164b.0ea8b8"
            ]
        ]
    },
    {
        "id": "1da423ef.5345dc",
        "type": "debug",
        "z": "de1c75c7.742f68",
        "name": "",
        "active": true,
        "tosidebar": false,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "x": 470,
        "y": 80,
        "wires": []
    }
]

Thanks for your perseverance, It’s certainly something I would not have been able to work out.

So it is almost like my first attempts except that I had the scope of the variable wrong.
Glad you got there

1 Like