RPi GPIO Input Inverting

514 views
Skip to first unread message

rlno...@gmail.com

unread,
Jul 31, 2017, 2:54:27 PM7/31/17
to Node-RED
I am using the rpi gpio pins to monitor some normally closed switch contacts (doors). But with the door closed, a 1 is sent from the input pin.
My application requires the input to be inverted so a closed door sends 0 and open sends 1.
I have not been able to find an inverting node to do this. What is the proper code to use in a function node to do the same thing?

Julian Knight

unread,
Jul 31, 2017, 3:29:48 PM7/31/17
to Node-RED
I'm afraid most of us far too easily fall back on function nodes and I'm sure there are other way. But a really simple way would be to use a function node with the following code - assuming your data is on the payload and can only be a numeric 0 or 1:

msg.payload = ! msg.payload;
return msg;

Check if it comes out as text though, in which case you may have to force it back to a number. Also make sure it can't be anything other than 0 or 1, if it can, you need to do additional error checks.

rlno...@gmail.com

unread,
Jul 31, 2017, 3:34:09 PM7/31/17
to Node-RED
Yes that works but it is in text.  How do I "force" it back to a number? Noob here.

Julian Knight

unread,
Jul 31, 2017, 3:46:17 PM7/31/17
to Node-RED
No problem.

Try something like 

msg.payload = ! parsInt(msg.payload);
return msg;

https://www.w3schools.com/jsref

Dave C-J

unread,
Jul 31, 2017, 4:14:03 PM7/31/17
to node...@googlegroups.com
That should be
   parseInt
With an e
   
--
Sent from phone.

Colin Law

unread,
Jul 31, 2017, 4:22:27 PM7/31/17
to node...@googlegroups.com
I generally use Number(msg.payload) as that does its best to make
sense of anything you send to it. If it cannot make sense of it then
it returns NaN. Parseint will fail if you give it a number rather
than a string, I think. So I tend to sloppily stick in Number()
whenever I think there is any doubt about the format. Probably that is
reprehensible behaviour on my part.

Colin
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/fe38a7ed-6217-4b30-8ff9-837fec10709d%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

rlno...@gmail.com

unread,
Jul 31, 2017, 4:49:35 PM7/31/17
to Node-RED
I added the missing letter E but still get a true or false instead of 0 or 1.

Dave C-J

unread,
Jul 31, 2017, 4:59:37 PM7/31/17
to node...@googlegroups.com
Ooops, maybe
   msg.payload = (msg.payload == 1) ? 0 : 1;
   return msg;
--
Sent from phone.

rlno...@gmail.com

unread,
Jul 31, 2017, 5:38:33 PM7/31/17
to Node-RED
Yup! That was it!  Thanks guys. One problem solved.  If there are more problems, I know where to find the experts.
Thanks again!

Lasse Klein

unread,
Nov 20, 2017, 8:27:47 AM11/20/17
to Node-RED


This works if you move the ! inside the paranthesis: 

msg.payload = Number(! msg.payload);
Reply all
Reply to author
Forward
0 new messages