Sonoff Basic with Motion and Tasmota

Have a MAME box with to many games

Want the Screen to come on when I walk up to it

shopping List

https://www.itead.cc/sonoff-wifi-wireless-switch.html


only needs 3.3v to run

Motion-Pin

red = 3.3v
yellow = GPI0 14
Black = GND

Flash the Sonoff with Tasmota 5.14

So I see the MQTT messages I setup GPI03 as a Relay2 (dummy one)
GPIO14 as Switch2
back to main screen

check the Log
log

looks good

now only problem is it send a ON them a OFF comand so I used nodered

The Nodered code

[{"id":"4695784b.b0ae88","type":"mqtt in","z":"d664133d.f572c","name":"MAME Motion","topic":"stat/MAME/POWER2","qos":"2","broker":"987c4047.b2d81","x":90,"y":360,"wires":[["aac420af.2c7df"]]},{"id":"69e6eee8.548e2","type":"debug","z":"d664133d.f572c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":570,"y":420,"wires":[]},{"id":"9b992535.2929c8","type":"delay","z":"d664133d.f572c","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":120,"y":480,"wires":[["792b35c4.6d4e7c"]]},{"id":"8c1ae8b0.fc9488","type":"mqtt out","z":"d664133d.f572c","name":"","topic":"nodered/motion2","qos":"","retain":"","broker":"987c4047.b2d81","x":590,"y":340,"wires":[]},{"id":"aac420af.2c7df","type":"function","z":"d664133d.f572c","name":"","func":"if (msg.payload ==\"ON\") {\n    msg.reset = true;\n    msg.payload = \"ON\"\n}\nelse{\n    \n    msg.payload = \"ON\"\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":320,"wires":[["8c1ae8b0.fc9488","69e6eee8.548e2","9b992535.2929c8"]]},{"id":"1becfca.4d5af03","type":"mqtt out","z":"d664133d.f572c","name":"","topic":"nodered/motion2","qos":"","retain":"","broker":"987c4047.b2d81","x":590,"y":520,"wires":[]},{"id":"792b35c4.6d4e7c","type":"function","z":"d664133d.f572c","name":"","func":"if (msg.payload ==\"ON\") {\n    msg.payload = \"OFF\"\n}\nelse{\n    msg.payload = \"OFF\"\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":460,"wires":[["1becfca.4d5af03","69e6eee8.548e2"]]},{"id":"987c4047.b2d81","type":"mqtt-broker","z":"","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""}]

Now the HA automation

binarysensor

  - platform: mqtt
    name: "MAME Motion"
    state_topic: "nodered/motion2"
    qos: 2
    retain: true
    payload_on: "ON"
    payload_off: "OFF"

Switch

  - platform: mqtt
    entity_id: mamescreen
    name: "Mame Screen"
    state_topic: "stat/MAME/POWER1"
    command_topic: "cmnd/MAME/POWER1"
    payload_on: "ON"
    payload_off: "OFF"
    optimistic: false
    qos: 0
    
- id: '1530486159608'
  alias: Mame Screen ON
  trigger:
  - entity_id: binary_sensor.mame_motion
    platform: state
    to: 'on'
  condition: []
  action:
  - data:
      entity_id: switch.mame_screen
    service: switch.turn_on
- id: '1530486245034'
  alias: Mame Screen OFF
  trigger:
  - entity_id: binary_sensor.mame_motion
    platform: state
    to: 'off'
  condition: []
  action:
  - data:
      entity_id: switch.mame_screen
    service: switch.turn_off

walk upto MAME box and the Screen comes on stay on until I walk away after about 2mins ±

image

hope this help someone

2 Likes

Cool project! When I use the dummy relay, GPIO 4 usually works well since it isn’t hooked up at all on the chip, keeps the serial pins open in case you need to tag into those for some reason for troubleshooting later.

cool project. thanks for sharing!

Hi, could you please explain to me better how you did the programming part. thank you

AS motion sensor Send a ON then a OFF HA would just see a ON then OFF

So I use Nodered to look at the stats/MAME/POWER2 if it get a ON it send the ON to the nodered/motion2 and put a delay OFF for 2m and then send the OFF to nodered/motion2 then if it get a only ON ( I MOVED) it just send the ON again and reset the delay. waits for and other 2m

HA DOES NOT KNOW ABOUT “stats/MAME/POWER2” only nodered/motion2

if you have node red install just paste this

[{"id":"4695784b.b0ae88","type":"mqtt in","z":"d664133d.f572c","name":"MAME Motion","topic":"stat/MAME/POWER2","qos":"2","broker":"987c4047.b2d81","x":90,"y":360,"wires":[["aac420af.2c7df"]]},{"id":"69e6eee8.548e2","type":"debug","z":"d664133d.f572c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":570,"y":420,"wires":[]},{"id":"9b992535.2929c8","type":"delay","z":"d664133d.f572c","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":120,"y":480,"wires":[["792b35c4.6d4e7c"]]},{"id":"8c1ae8b0.fc9488","type":"mqtt out","z":"d664133d.f572c","name":"","topic":"nodered/motion2","qos":"","retain":"","broker":"987c4047.b2d81","x":590,"y":340,"wires":[]},{"id":"aac420af.2c7df","type":"function","z":"d664133d.f572c","name":"","func":"if (msg.payload ==\"ON\") {\n    msg.reset = true;\n    msg.payload = \"ON\"\n}\nelse{\n    \n    msg.payload = \"ON\"\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":320,"wires":[["8c1ae8b0.fc9488","69e6eee8.548e2","9b992535.2929c8"]]},{"id":"1becfca.4d5af03","type":"mqtt out","z":"d664133d.f572c","name":"","topic":"nodered/motion2","qos":"","retain":"","broker":"987c4047.b2d81","x":590,"y":520,"wires":[]},{"id":"792b35c4.6d4e7c","type":"function","z":"d664133d.f572c","name":"","func":"if (msg.payload ==\"ON\") {\n    msg.payload = \"OFF\"\n}\nelse{\n    msg.payload = \"OFF\"\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":460,"wires":[["1becfca.4d5af03","69e6eee8.548e2"]]},{"id":"987c4047.b2d81","type":"mqtt-broker","z":"","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""}]

into node red
He

1st has this in it

2nd has this

Dont understand how it works but got it to work

then use automation to turn on/off the screen

hope this make cents

When I saw this I got very excited and and decided to do this in every room I have Sonoff in, but I couldn’t get past the first step.

When I connect the pir sensor to the board it fails to connect to wifi and restarts every few seconds. When I take the sensor off every thing goes back to normal.
I’ve tried to flip the wires just in case I was mistaken but it didn’t help.

These are the pir sensors I’m using.
https://s.click.aliexpress.com/e/cb9qed8y

What pin did u attach it to and what verson of sonoff was it

Sonoff basic running Tasmota. At first I tried +, GND, GPIO 14 then every possible option. Still nothing

Did u try just using a wire from gpio14 to ground that will tell us if it’s the pir playing up

I’ve tried using another sonoff device and now it works like a charm.

What’s the best way to add this as a binary sensor to HA? Should I create a template sensor?
I don’t use node red
Many thanks

As the pir send a on then a off that why I used node red to control the off send but if u can work out a way to keep it all in HA that make me a happy camper also