Capture state change

OK, simple question, I think…

Below is our current flow that works. It’s a door sensor using the Switch Input on the Rio. We set the inject node to fire every 30 seconds and read the status (On, Off), put that state in a variable that is friendly to InfluxDB, and send to InfluxDB. The normal state of the door is Off (meaning it’s closed).

where the function is:
msg.payload = {
return msg;

Instead of using an inject node, can we simply populate InfluxDB whenever the state of the node is On (meaning the door is open)? If so, how?

I gave MQTT a try and I got it working as shown below, but I still do not know how to get it to automatically update the status of the door without using an inject node. The idea is that the door is closed for say, 3 hrs. No need to take a reading of the door every 30 or 60 seconds. Just transmit data / notify when the door goes open.

And it works now… I replaced the first node with this one (circled in blue)

Hi Ya Grant,

Sorry I let you drift for a while… That node was my first thought 2 days ago when you first posted, but I was sure you knew about it and so I figured there was more to your question that I could figure, so I pondered it for a while.

Glad you got things working to your liking… and sorry to pop your party balloon, but that node has a built in 1 second inject. You just don’t see the blue inject node…
So yeah, its polling the point in the background every 1 second.
No big deal, I mean, if you cant see the tree fall, it does not make a sound right?

Node-RED is asynchronous, it does no do anything unless told. Either by a msg.payload showing up on an MQTT node, a UDP node, or an inject node. It does not have a ‘scan timer’ like a PAC or PLC.

Bottom line, you are now checking the door status every second. Cool huh?

Thanks for the additional explanation. For what it’s worth, this exercise of identifying the state change prompted me to set up an MQTT broker (which was on my to do list for a while) and now that I have some MQTT action live and working, I can see tons of uses for this.

(Amateur tip: install the MQTT broker on a Linux or RPi, not on Windows…it’s dead simple if you follow one of the many tutorials)

I personally use MQTT a TON, man its amazing for moving information around.
Oddly enough, one of my jobs for this weekend is to move it from running on my Pi to my main Windows computer… You have me worried it will be harder than I thought it might be.