Wired opening sensor integration

I have a pair of magnetic reed sensors on my garage doors that I run off of an RPi 3. Ran them off of an RPi 1b before that. They work fine.

There was a security system installed in my house prior to moving in and there were a bunch of sensors about with wires all leading to the basement. I was able to get the reed sensors working with the 3.5v GPIO on an RPi 0w.

Note, I’m not doing I2C. Each sensor is direct wired to the GPIO and I use GitHub - rkoshak/sensorReporter: A python based service that receives sensor inputs and publishes them in various ways. to publish their state to MQTT. I don’t know if I2C will impose other problems/ restrictions.

If I were to do it over again (and I plan on doing so some day), I’d use ESP8266 or ESP32 instead of RPis to push the sensor states to MQTT. But I’ve been running with the RPis for about seven or eight years now without problem.

Not necessarily. See above for one solution. There’s several other options. You don’t need to mess with the GPIO stuff directly by OH. That’s not always practical anyway (e.g. the wires may not be where you want to run OH).

As for one rule, it largely depends on what you want to do in that rule. See Topics tagged designpattern for lots of different ways to make a rule to handle lots of different Items.

For one simple example, let’s say you want to send an alert if a sensor remains open for too long.

  1. Put all the open/closed sensors into a Group
  2. Create a rule that is triggered by any member of the Group created in 1 changing.
  3. In Blockly using openHAB Rules Tools [;] (which implements a lot of the Design Pattern posts):

It doesn’t matter if you have one sensor or 100, this one rule will log out if any one of them remains open for more than five minutes.

In JS Scripting the same rule, without using the OHRT library’s TimerMgr would look something like:

var timer = cache.private.get(event.itemName);
if(event.itemState.toString() == "OPEN") {
  cache.private.put(event.itemName, actions.ScriptExecution.createTimer(time.toZDT('PT5M'), (itemLabel) => {
    console.info(itemLabel + ' has been open for more than five minutes!');
  }, items[event.itemName].label);
else {

These ten lines of code can handle all your open sensors (assuming you want them all to behave the same way). Note, I’m using a brand new feature added to the openhab-js above that allows us to pass variables into the timer function.

There are also rule tempaltes that can handle these. For example, you can install and instantiate Threshold Alert and Open Reminder [;] with:

  • Triggering Group: the Group created in 1 above
  • Threshold State: OPEN
  • Alert Delay: PT5M
  • leave the rest of the properties at their defaults

This handles all your open sensors and you don’t have to code the rule. And as a bonus the rule template supports repeating the reminder, customized properties on a per Item basis, and a do not disturb period, all for free. All you need to do is write the code that responds (e.g. send the alert) when the sensor is OPEN for too long.

It depends on the distance of course. I can say they work for me with wires in the 30’-45’ range (really rough estimate, I can’t say I really know how these wires are routed). At least the magnetic reed sensors do. Since they operate with a simple open/close circuit just enough voltage needs to get through for the GPIO to pick up (1.8v for RPi, don’t know for ESPs). We aren’t powering LEDs here, just detecting if the circuit is open or closed so we have lots of room for voltage drops.

Feeding in what I know about the RPi GPIO specs (3.5v, 50mA) assuming PVC conduit, 8AWG copper wiring, and DC power of course, there’s only a drop of 0.11% in voltage over 500’. Obviously @derHerrdesSchreckens should put in their actual wire specs but as I mess around I can’t get more than a percent drop over 500’.

tl;dr: you it’s probably going to work for simple reed sensors.

1 Like