Node-red, using two contact sensors for direction

What are thoughts around using two open closed sensors (storm door and regular door) to determine which way you are going through a door. Could you do it if you used time as the primary determinant in the rule?

The use case is the screen door opens then the regular door opens turn on the room light in the house, going the other way turns on the garage lights.

In my case the hub would be less than 10 get from the sensors, so I'm guessing that the network speed shouldn't be a huge factor.

Is there a better way to do it?

That sounds interesting.. maybe store a timestamp of each contact "open" event in a global and always compare the 2 to see which one is greater given a certain time limit - that could get you a "direction" possibly.. the issue though would be how would you handle the edge cases like someone coming in and someone going out nearly at the same time?

note: I edited this after thinking a bit

I was wondering why you chose closed... would you use a switch node to compare times?

I'm not sure the timing would work. You might have a better chance using Zigbee devices as they seem to respond faster.

How about this rule:

Trigger:

  • stormdoor change
  • maindoor change

IF stormdoor OPEN AND maindoor CLOSED THEN
action for coming in
ELSE-IF stormdoor closed and maindoor open THEN
action for going out
END-IF

Yeah I changed it to "open" after thinking about it.. there are many ways to handle this.. but was thinking global (or flow) vars might be an easy way.

I would do something like this maybe:


note: there are likely much better ways than my 5 second solution. A Function node could probably simplify stuff etc. Those last grey nodes on the right are just NOOP placeholder nodes.

As an aside - being able to do quick mockups is yet another reason I love the visual aspect of Node-RED.

1 Like

If you really wanted to have fun then in place of those NOOP nodes you could use the LinkIn/Out Nodes to connect to other sequences anywhere.
OR
You could publish the result to an MQTT server via the "MQTT Out" node and trap a direction "event" by using the "MQTT In" node similar to the way you would use the HE Device node.. the "MQTT in" node could then be used in any sequence you want.

There is a simple MQTT server in the palette you can use for this purpose called "node-red-contrib-aedes" - runs locally easy to set up.

One more thing - for the switch node in order to do the evaluations I sometimes use JSONATA like this:

Note: I set the rule evaluation to "stopping after first match" rather then the default of "checking all the rules".

2 Likes

Thank you for your ideas. I'm going to play around a little with it to see if this will be quicker than the motion sensors I'm using currently, or some combination so the lights get turned on quicker.

1 Like

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