I made an MQTT Motion Detector

Yeah, I tested everything with a Wemos D1 before hacking up the Sonoff S20. I’m busy at a loss as to what to use my motion detector before besides lighting which doesn’t really interest me. Lights coming on automatically just always seems to clumsy to me, even with clever software. I’ll just have to wait and see what automations come to mind eventually.

Controlling lights typically is a big use case for motion sensors I would say.
The only other thing I can think of is a warning if someone approaches your front door. Our mailmen are notorious for leaving the “we missed you” card instead of delivering the actual package, even if we’re home. Having a sensor that tells you someone is approaching even before they ring the doorbell could be useful. But that obviously depends on the setup of your house, and the amount of traffic or wildlife around it that could trigger false positives.

Well they might be good for detecting if some wildlife you want to film is around, the same could be said for the occasional thief so that the cameras aren’t always on (mine use a lot of juice filming nothing). These days I agree that automating lighting is probably not so important (in terms of electricity usage - due to LEDs) but I love it when my lights come on when I walk in :smiley: and go off when I leave a room all automatically (not so good when I’m reading tho’ :stuck_out_tongue: ). Also heating and cooling can be tailored to rooms you use most and are in, the same could be said for music following you around (I live alone so I’m happy for my music to follow me - probably not so good for WAF - unless you both like the same music!). I’m sure there are lots of other examples and I’m sure you will come up with a few more :slight_smile:

I have a use for the PIR that I plan to try once my parts arrive. Wemos D1 mini’s with a temp/humidty sensor, photoresistor,adjustable pir and controlling ws2813 strip(new). LED strip to run length of the bed and headboard. using pir to detect when feet hit the floor to illuminate it. And using addressable leds, should be able to only turn on the side that is detected, so the headboard will be used as accent lighting at other times.

Sounds like that S20 would work too for my project if I wanted to plug in a wallwart with just a LED striplight.

Here is an idea for your cool hacked S20: Towel warmer automation.
I am a HA noob.
@roofuskit, how do you get HA to send you a message when motion is detected?

My plan is to detect motion and humidity near the tub to automate a towel warmer, wife should like it a lot. S20 takes up two outlets, wife needs hair implement power, so S20 won’t work.

I integrated the same PIR module into a Sonoff basic flashed to tasmota 5.6. It’s integrated in the case on gpio 14. I have a DHT22 on order. PIR motion detection exercises the relay until the tasmota switchtopic command is entered at the console.
The Sonoff relay works fine, I can turn it on and off with HA. According to the device tasmota console, it fires off an MQTT command every time the PIR triggers.

I am unsure how to connect the dots to have HA pay attention to the PIR and the DTH22.
I want it to:
If motion triggers,
record current humidity
monitor humidity for 20 minutes,
If humidity climbs by ~10%
turn on towel warmer for two hours

Any suggestions/pointers to info/guidance is appreciated.

Sparky

3 Likes

i want to do something similar except to use PIR in the living room . but setup is same. i used espurna firmware which is feeding temp and humidity to HA via mqtt. but can quite understand how to get PIR data fed into HA. i am now going to try tasmota which supports configuring sensors against gpio on the fly.

Hey everyone, sorry I haven’t followed up with directions for the PIR sensor here but I work long hours and have a long commute currently!

Hopefully this weekend I can get a chance to double check my settings and give you the exact commands to setup the motion sensor to talk to HASS. But I can tell you I found the answer on this page. https://github.com/arendst/Sonoff-Tasmota/wiki/Commands It involves changing the switch mode for the specific switch number you set when you assigned the GPIO pin in the configuration. I just can’t remember what the settings I settled on were.

EDIT: I forgot to mention, those commands can be executed through the command line page on the web interface for each device. To me that’s the easiest way to change the necessary settings.

OK everyone who’s been asking. The settings for getting it to talk to HASS are as follows. My PIR sensor is connected to GPIO3, so under the GPIO3 dropdown menu I have selected option 12 which is “Switch 4”. Once you have saved your settings there you must go to the “Console” page from the main menu. Bow that our PIR sensor is considered “Switch 4” we want to set it to Switch Mode 1.

Below is how the Tasmota Firmware Wiki reads:

SwitchMode<x> - 1 - Set switch mode to FOLLOW (0 = Off, 1 = On)

So where <x> is the number of the switch we picked, we type into the console. “SwitchMode4 1” and hit enter. It will respond with something like this this.

23:21:44 MQTT: stat/sonoff7/RESULT = {“SwitchMode4”:1}

So, now when your PIR sensor turns on the Tasmota firmware will send out an MQTT message with the payload “ON”, when it turns off it will send out a message with the payload “OFF” as seen below

23:33:14 MQTT: cmnd/sonoff7/POWER4 = ON
23:33:16 MQTT: cmnd/sonoff7/POWER4 = OFF

The MQTT topic reads: cmnd/(your device name)/POWER(the switch number you picked)

Now, if you add the code below to your config file, you’ll have a binary sensor that tells you when it detects motion.

   sensor:
      - platform: mqtt
        state_topic: "cmnd/sonoff7/POWER4"
        name: "Bedroom Motion Detector"
        qos: 0
        payload_on: "ON"
        payload_off: "OFF"

This is a simple dumb sensor, there is absolutely no delay, once the sensor stops detecting motion it sends the OFF command. If motion starts again 10 seconds later it will send the ON command again. If you want to add a delay for something like a light automation you’ll need to add an automation and an input boolean.

input_boolean:
  motion_detected:
    name: Motion Detected
    initial: off

And the automation is below:

  - alias: Motion Detected in Bedroom
    trigger:
      - platform: state
        entity_id: binary_sensor.bedroom_motion_detector
        to: 'on'
      - platform: state
        entity_id: binary_sensor.bedroom_motion_detector
        to: 'off'
        for:
          minutes: 5
          seconds: 00
    action:
      service_template: >
        {% if trigger.to_state.state == "on" %}
        homeassistant.turn_on
        {% elif trigger.to_state.state == "off" %}
        homeassistant.turn_off
        {% endif %}
      entity_id: input_boolean.motion_detected

The time you see above “minutes: 5” that’s the number of minutes it must go without motion in order to switch off the input boolean. And you would use that device “input_boolean.motion_detected” to trigger your automation on or off, changing that time to whatever delay you want.

Here is what my modified Sonoff S20 looks like. I have a small PIR and a DHT22 temp/humidity sensor in there. The PIR is off center because that’s where it would fit, same with the DHT22.

3 Likes

Hi @rufuskit,
That’s really neat, you’ve inspired me to give it a go! Thanks for posting. :smiley:
Andrew

Sir,

How did you get a binary_sensor while in config you made a “sensor”?

According to bruh automation guy, that’s the only pir sensor that has no interferance from nearby wifi antenas.
My 2c :slight_smile:

I am going to copy your project and was wondering if I can connect the output 3.3V of these PIR to the gpio of the sonoff or do I need a relay in between? Thanks

Specifications
Supply voltage 4.5 V to 20 V DC
Voltage output high level 3.3 V
Maximum detection distance Adjustable between 3 m and 7 m
Maximum detection angle 110° solid angle
Delay time Adjustable between 5 s and 200 s (default 5 s)
Blocking time 2.5 s

http://cdn.openimpulse.com/blog/wp-content/uploads/wpsc/downloadables/DYP-ME003_PIR_Sensor_Module_Datasheet.pdf

I use those sensors and have them connected directly to both arduinos and esp32866s.

I am under the impression that the esp8266 input pins are 5v tolerant. However the PIR sensor I used runs on 3.3v. I’m not sure if there is a 5v supply available in the Sonoff S20, Worst case, you can fit a small boost converter in there.

I think ill try to find one that runs off the 3.3V on the sonoff basic. Thanks for pointing this out.

I’m doing the same thing with a wemos d1 mini and a PIR sensor but for some reason my PIR keeps triggering even when no motion is detected had anything else had this problem?

Yes

Any idea how to fix the ghosting? I tried putting a resistor on the ground pin from pir but didn’t help

Not sure which module you are using, but adding a capacitor like this solved the problem for me

https://redfox314.tweakblogs.net/blog/16895/esp8266-and-hc-sr501-false-positives

1 Like

this sucker runs on 3.3v https://www.amazon.com/gp/product/B0732VQ4Q2/ref=ppx_yo_dt_b_asin_title_o01_s00?ie=UTF8&psc=1