# Keep state of sensor

**URL:** https://discourse.nodered.org/t/keep-state-of-sensor/59074
**Category:** General
**Created:** [27 February 2022 17:03 UTC](https://discourse.nodered.org/t/keep-state-of-sensor/59074 "2022-02-27T17:03:25Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Chura](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chura/32/32051_2.png) [@Chura](https://discourse.nodered.org/u/Chura)
#### Post date: [27 February 2022 17:03 UTC](https://discourse.nodered.org/t/keep-state-of-sensor/59074/1 "2022-02-27T17:03:26Z")

</div>

Hi

I have a contact sensor, that tell me if something stop the rollercoaster from shutting down.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/6/a6ecbb5515e4d31cf18a86acb00a298b31c004a7.png)

I want to add, that send the command on the right, only if other sensor is closed  
However, That other sensor report to MQTT, and I cannot query MQTT for current state. I need to keep that as a variable or flag, that I can query during my flow.

How can I achieve that functionality ?

---

<div class="post-metadata">

### Author: ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)
#### Post date: [27 February 2022 18:48 UTC](https://discourse.nodered.org/t/keep-state-of-sensor/59074/2 "2022-02-27T18:48:45Z")

</div>

You could use a change node to store the state in a flow variable, when a message comes from MQTT.

Then in a switch node check the value of the flow var.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [13 March 2022 18:49 UTC](https://discourse.nodered.org/t/keep-state-of-sensor/59074/3 "2022-03-13T18:49:36Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
