# \[SOLVED\] Node-Red and previous.state

**URL:** <https://community.openhab.org/t/solved-node-red-and-previous-state/49262>\
**Category:** 3rd Party\
**Created:** [August 12, 2018, 4:30pm UTC](https://community.openhab.org/t/solved-node-red-and-previous-state/49262 "2018-08-12T16:30:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![e36Alex](https://community.openhab.org/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@e36Alex](https://community.openhab.org/u/e36Alex)\
**Post date:** [August 12, 2018, 4:30pm UTC](https://community.openhab.org/t/solved-node-red-and-previous-state/49262/1 "2018-08-12T16:30:39Z")

</div>

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:

```php
// 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

---

<div class="post-metadata">

**Author:** ![rswennen](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/839c29/32.png) [@rswennen](https://community.openhab.org/u/rswennen)\
**Post date:** [August 12, 2018, 5:14pm UTC](https://community.openhab.org/t/solved-node-red-and-previous-state/49262/2 "2018-08-12T17:14:52Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![e36Alex](https://community.openhab.org/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@e36Alex](https://community.openhab.org/u/e36Alex)\
**Post date:** [August 12, 2018, 5:23pm UTC](https://community.openhab.org/t/solved-node-red-and-previous-state/49262/3 "2018-08-12T17:23:56Z")

</div>

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

```auto
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"

```

---

<div class="post-metadata">

**Author:** ![rswennen](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/839c29/32.png) [@rswennen](https://community.openhab.org/u/rswennen)\
**Post date:** [August 12, 2018, 5:47pm UTC](https://community.openhab.org/t/solved-node-red-and-previous-state/49262/4 "2018-08-12T17:47:20Z")

</div>

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

 ![25](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/a/7/a77ca8a96867af71d866b82ed09d5c39fc0ce045.png)

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [August 12, 2018, 6:44pm UTC](https://community.openhab.org/t/solved-node-red-and-previous-state/49262/5 "2018-08-12T18:44:44Z")

</div>

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

 ![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/6/c/6ca64c0ac55a91da0702d2c09008bc87521596ca.png)

---

<div class="post-metadata">

**Author:** ![e36Alex](https://community.openhab.org/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@e36Alex](https://community.openhab.org/u/e36Alex)\
**Post date:** [August 13, 2018, 7:54pm UTC](https://community.openhab.org/t/solved-node-red-and-previous-state/49262/7 "2018-08-13T19:54:04Z")

</div>

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 😄

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

 ![nodered](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/6/4/6412fceaf9341621af51127f728e876b53ef8d73.png)

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [August 13, 2018, 9:09pm UTC](https://community.openhab.org/t/solved-node-red-and-previous-state/49262/8 "2018-08-13T21:09:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![e36Alex](https://community.openhab.org/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@e36Alex](https://community.openhab.org/u/e36Alex)\
**Post date:** [August 14, 2018, 5:12pm UTC](https://community.openhab.org/t/solved-node-red-and-previous-state/49262/9 "2018-08-14T17:12:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [August 14, 2018, 6:50pm UTC](https://community.openhab.org/t/solved-node-red-and-previous-state/49262/10 "2018-08-14T18:50:39Z")

</div>

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

> [@e36Alex](#):
>
> if i use a seperate flow for each rule, it doenst take long to loose the overview of the rules.

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.

---

<div class="post-metadata">

**Author:** ![MDAR](https://community.openhab.org/user_avatar/community.openhab.org/mdar/32/57900_2.png) [@MDAR](https://community.openhab.org/u/MDAR)\
**Post date:** [November 30, 2019, 4:07pm UTC](https://community.openhab.org/t/solved-node-red-and-previous-state/49262/11 "2019-11-30T16:07:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [November 30, 2019, 10:46pm UTC](https://community.openhab.org/t/solved-node-red-and-previous-state/49262/12 "2019-11-30T22:46:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MDAR](https://community.openhab.org/user_avatar/community.openhab.org/mdar/32/57900_2.png) [@MDAR](https://community.openhab.org/u/MDAR)\
**Post date:** [December 1, 2019, 9:28am UTC](https://community.openhab.org/t/solved-node-red-and-previous-state/49262/13 "2019-12-01T09:28:19Z")

</div>

Thanks Vincent.

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

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [December 1, 2019, 9:45am UTC](https://community.openhab.org/t/solved-node-red-and-previous-state/49262/14 "2019-12-01T09:45:30Z")

</div>

Create a function node:

```javascript
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;

```

---

<div class="post-metadata">

**Author:** ![MDAR](https://community.openhab.org/user_avatar/community.openhab.org/mdar/32/57900_2.png) [@MDAR](https://community.openhab.org/u/MDAR)\
**Post date:** [December 1, 2019, 10:20am UTC](https://community.openhab.org/t/solved-node-red-and-previous-state/49262/15 "2019-12-01T10:20:37Z")

</div>

Thanks a million. 😄 👍

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

---

<div class="post-metadata">

**Author:** ![MDAR](https://community.openhab.org/user_avatar/community.openhab.org/mdar/32/57900_2.png) [@MDAR](https://community.openhab.org/u/MDAR)\
**Post date:** [December 2, 2019, 11:27am UTC](https://community.openhab.org/t/solved-node-red-and-previous-state/49262/16 "2019-12-02T11:27:09Z")

</div>

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.

 ![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/e/5/e5f9ee348439017181ab6ed3f3a797eb7f1a5ba0.png)

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [December 2, 2019, 2:35pm UTC](https://community.openhab.org/t/solved-node-red-and-previous-state/49262/17 "2019-12-02T14:35:35Z")

</div>

Try that:

```javascript
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;

```

---

<div class="post-metadata">

**Author:** ![MDAR](https://community.openhab.org/user_avatar/community.openhab.org/mdar/32/57900_2.png) [@MDAR](https://community.openhab.org/u/MDAR)\
**Post date:** [December 2, 2019, 3:28pm UTC](https://community.openhab.org/t/solved-node-red-and-previous-state/49262/18 "2019-12-02T15:28:23Z")

</div>

> [@vzorglub](#):
>
> 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;

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](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/0/9/09263e942a7ef3239066507d0047f4d05fdbc727.png)

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [December 2, 2019, 4:15pm UTC](https://community.openhab.org/t/solved-node-red-and-previous-state/49262/19 "2019-12-02T16:15:08Z")

</div>

Sorry:

```javascript
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;

```

---

<div class="post-metadata">

**Author:** ![MDAR](https://community.openhab.org/user_avatar/community.openhab.org/mdar/32/57900_2.png) [@MDAR](https://community.openhab.org/u/MDAR)\
**Post date:** [December 2, 2019, 5:33pm UTC](https://community.openhab.org/t/solved-node-red-and-previous-state/49262/20 "2019-12-02T17:33:27Z")

</div>

> [@vzorglub](#):
>
> 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": []
    }
]

```

 ![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/2/6/2692c3cc83f6214ee5a63936d867821f1d794a95.png)

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

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [December 2, 2019, 6:11pm UTC](https://community.openhab.org/t/solved-node-red-and-previous-state/49262/21 "2019-12-02T18:11:42Z")

</div>

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

[Next page](https://community.openhab.org/t/solved-node-red-and-previous-state/49262.md?page=2)
