Light switch with LED Push Button and a PIR Sensor on Sonoff Basic (Tasmota)

One of the things I’m trying to do with HA and my Sonoff’s is to create light switches around the house that would have a momentary push button and a PIR sensor. The goal is to be able to see the push button LED when it is dark in the house and also have a PIR sensor to set up some future automation to turn lights on or off when there is no activity for some time. Also, there are three requirements that I really wanted to have in place 1) Be able to control the onboard button so that I can restart, reset, etc if needed without opening up the switch. 2) Be able to see all onboard led error indicators (rapid flashing, restarts, etc.) directly on the momentary push button. 3) Do not toggle or do anything to main Relay1 when PIR changes state (Thanks to @DrZzs for helping me figure out that I need to set one of the pins to Relay2 so that Switch2 does not control Relay1).

1) In the first version, I was able to to get the push button LED to be on when the PIR is triggered, see the push button LED when someone is close enough to click it, and keep the LED OFF when the light is On (It’s also OFF when the light in OFF but temporarily visible when PIR is active). However, the disadvantage here is that there is no LED indication of the board’s status.

Module parameters:

  • GPIO3 (Serial Out) > Relay2 (LED)
  • GPIO14 (Sensor) > Switch2 (PIR AM312)
  • SwitchMode2 1 (0 is OFF and 1 in ON)
  • LedState 1 (Show power state on led)

Wiring:

  • Used 10k resistor to decrease the brightness of push button LED since it will be on all night.

2) In the second version I was able to get the inverted effect of the onboard LED, see the push button LED when the light is OFF, and get notifications on push button LED of board status. However, the horrible disadvantage here is that when someone gets close to the switch to turn it on, the onboard LED will turn on and the push button LED would turn off, so it will disappear into the night (probably good setup for April fools joke).

Module parameters:

  • GPIO4 > Relay2 (n/a)
  • GPIO14 (Sensor) > Switch2 (PIR AM312)
  • SwitchMode2 1 (0 is OFF and 1 in ON)
  • LedState 1 (Show power state on led)

Wiring:

3) This last version is probably the one I will stick with. With this, I get the inverted LED effect on the push button, all the onboard LED notifications of status, and it lights up for a few seconds when someone comes close. The only annoyance here is that the PIR has to be always on by default so that onboard LED is also always on so that the push button LED is off unless PIR is triggered (changes state to OFF). I don’t think to have the light on inside the switch all the time would cause much extra to hear or cause it to burn out quickly, but I would have preferred it to be off.

Module parameters:

  • GPIO4 > Relay2 (n/a)
  • GPIO14 (Sensor) > Switch2 (PIR AM312)
  • SwitchMode2 2 (0 is ON and 1 in OFF)
  • LedState 1 (Show power state on led)

Wiring: (Same as above)


One of the things that is frequently mentioned in posts is to use a step-down resistor when connecting switches/sensors to the board. However, I did not use any and not sure if that applies to the different setups above.

Anyway, this was all done via trial and error by poking different wires into each and every available slot, so I would appreciate your feedback if you think this may later brow up or if there may be a better way to do it. Also, I don’t think I really understand why the inverted LED effect even works, so this would be nice to know too if anyone has some ideas.

10 Likes

Nice work!
Great post too. Will be helpful to others I’m sure.

This is a great idea, could you post photos of the finished wall switch? Keen to see how you laid out the momentary switch and the PIR sensor

I’m hoping to get at least one up this weekend, but the plan is just to drill two holes in a blank wall plate so that it looks something like this from the front:
image

BTW, I found out why the inverted push button lights work. I tested the voltage on all the LED pins and it turns out that that the left pin is 1v when the light if on and 3.3v when it’s Off: image

FWIW. I’m attempting to do something very similar. I gave up trying to use the LED pins to get my external button’s LED working the way I wanted. I ended up just making a new configuration entry defining LED as GPIO3 and then wiring up GPIO3 to my LED.

Here is a fork with the changes required for adding the new config.
https://github.com/wQQhce2g93Ei/Sonoff-Tasmota/commit/50dab12223bb0ed6d73686e5705f580cf083c973

Could you do a generic profile with this?

I had no idea there was a generic one! It looks like it should work. I’ll try it out.

Sometimes generic doesn’t expose all the pins you need though. Had this issue on a dual R2 so I had to mod my template.

Off on my search to see if the LED on the dual R2 already has a resistor in line. (thinking it does) need to add my own to this spot.

1 Like

Nope, this works perfectly. I hadn’t come across generic in any posts I was looking at and never even thought to look for it!

Thanks for mentioning it. I really appreciate it.

1 Like

I finally installed and got two on my bad boys working perfectly!

The first one: PIR Installed din the switch. For now, it triggers the LED light on the button so that it’s visible when someone walks by.

Second One: PIR Installed in the light fixture because the switch did not have a ground wire. So I installed the Sonoff with the PIR facing down in the light and just used the previously live wires to light wires to power the LED and GPIO button.

2 Likes

@trusty will you help me out in how did you setup in tasmota web UI and home assistant config?

Hey @ashfaaaa . The Sonoff/Tasmota settings depend on which implementation you’re doing…

If you are placing a PIR sensor in the lamp fixture and the pushbutton is just always on, then this is what I used:
Backlog GPIO4 22;GPIO14 10;SwitchMode1 4;SwitchMode2 1

If you are putting everything in the switch and the pushbutton should only light up when PIR is active, then use this:
Backlog GPIO4 22;GPIO14 10;SwitchMode1 4;SwitchMode2 2

Then in Home Assistant, you can create the following binary sensors to check if the PIR status:

- platform: mqtt
  name: "Sonoff 1 Motion"
  state_topic: "stat/sonoff1/POWER2"
  availability_topic: "tele/sonoff1/LWT"
  qos: 1
  payload_on: "OFF"
  payload_off: "ON"
  payload_available: "Online"
  payload_not_available: "Offline"
  sensor_class: motion

To control the power (or light), you can add one of the two (or both) to the configuration.yaml:

light:
  - platform: mqtt
    name: "Sonoff1 Light"
    command_topic: "cmnd/sonoff1/power1"
    state_topic: "stat/sonoff3/POWER1"
    availability_topic: "tele/sonoff1/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: true

switch:
  - platform: mqtt
    name: "Sonoff 1 Power"
    command_topic: "cmnd/sonoff1/power1"
    state_topic: "stat/sonoff1/POWER1"
    availability_topic: "tele/sonoff1/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: true

Btw, you might want to leave out the last “retain: true” flag. Been watching some videos saying it’s a bad idea since Tasmota already retains the state and it may randomly just change state if Home Assistant also retains the state and it restarts or losses connection to Sonoff. Can anyone confirm this?

2 Likes

thanks its working but i have another issue every 3 to 5mins mqtt disconnects and reconnects are you facing anything like this?

14:02:21 MQT: Attempting connection...
14:02:22 MQT: Connected
14:02:22 MQT: tele/RoomLight/LWT = Online (retained)
14:02:22 MQT: cmnd/RoomLight/POWER = 
14:02:22 MQT: stat/RoomLight/RESULT = {"POWER1":"ON"}
14:02:22 MQT: stat/RoomLight/POWER1 = ON
14:02:22 MQT: stat/RoomLight/RESULT = {"POWER1":"OFF"}
14:02:22 MQT: stat/RoomLight/POWER1 = OFF
14:03:58 MQT: Attempting connection...
14:03:59 MQT: Connected
14:03:59 MQT: tele/RoomLight/LWT = Online (retained)
14:03:59 MQT: cmnd/RoomLight/POWER = 
14:03:59 MQT: stat/RoomLight/RESULT = {"POWER1":"ON"}
14:03:59 MQT: stat/RoomLight/POWER1 = ON
14:03:59 MQT: stat/RoomLight/RESULT = {"POWER1":"OFF"}
14:03:59 MQT: stat/RoomLight/POWER1 = OFF
14:06:55 MQT: tele/RoomLight/STATE = {"Time":"2018-10-06T14:06:55","Uptime":"0T01:16:21","Vcc":3.427,"POWER1":"OFF","POWER2":"ON","Wifi":{"AP":1,"SSId":"Ashu","RSSI":76,"APMac":"5C:7D:5E:34:2C:3C"}}
14:06:55 MQT: tele/RoomLight/SENSOR = {"Time":"2018-10-06T14:06:55","Switch2":"OFF"}
14:07:54 MQT: stat/RoomLight/RESULT = {"POWER1":"ON"}
14:07:54 MQT: stat/RoomLight/POWER1 = ON
14:08:03 MQT: stat/RoomLight/RESULT = {"POWER1":"OFF"}
14:08:03 MQT: stat/RoomLight/POWER1 = OFF

I never realized it but apparently MQTT losses connection on one of my devices as well, anywhere from 2 - 30 minutes apart. However, it’s only happening on the device that if far away from Wifi, not on a device much closer. So I think it’s not related to this specific config, just might be distance or something else.

1 Like

First of all thanks to @DrZzs for the amazing tutorials and @trusty for this post!
Hey doing something similar, have everything wired up. Now trying to figure out how much does the PIR sensor need to extrude from the blank plate?

Only the top surface with the glass window thingy? Or the whole metallic cylinder part?
(It seems like I might not be able to extrude the whole PIR sensor metallic thingy)

Thanks!

should just be the bulbous part.

1 Like

Unfortunately my sonoff basic has been plaqued by the false PIR triggers. Yes, I have watched @Drzzs video on ghost switching, but this seems slightly different and can’t seem to figure out what to do in my situation.

I am using TX(GPIO1) and RX(GPIO3) pins with PIR (switch2n) and regular switch (switch1) respectively with SerialLog off.

It seems like my PIR is what’s causing the false triggers not the regular wall switch

I have disabled the PIR rules for turning power on. Ghost switching seems to be gone. But console definitely shows plenty of PIR state 1/0 changes. And keep seeing a lot of initialized , attempting connection, PIR state 1 then 0 in console.

This community has been extremely helpful so thought, this is going to be just the last part of the puzzle.

If this were because of the broadly discussed ghost switching thing, Where do I need to add these resistors and capacitors? Do I need to add these to both the PIR and the switch? (As it definitely seems like the false triggers are from PIR AM312 mini)

I had problems with a PIR connected to a Wemo d1 the false triggers were caused by the WiFi. I solved it with a capacitor. Somewhere on the PIR I’m.m not home so I can’t check now but google it you probably find where to put what.

1 Like

Maybe this: https://www.raspberrypi.org/forums/viewtopic.php?p=770173

Hey Thanks for the reply. Will check it out.

Has it been stable after adding the capacitor?