RavenSystem / esp-homekit-devices

Advanced firmware to add native Apple HomeKit and custom configurations, compatible with any SoC based on ESP32, ESP32-S, ESP32-C and ESP8266 series. (Shelly, Sonoff, Electrodragon, Tuya...)
Other
2.76k stars 361 forks source link

motion sensor type 5 for sonoff basic #528

Closed vsidlu closed 5 years ago

vsidlu commented 5 years ago

would like to add motion sensor to sonoff basic gpio 14. can not figure out right json for sensor to work. was able to add motion accessory but thats all. need json sample for type 5. thanks in advance.

bj-tracer commented 5 years ago

I guess it seems like a "a":[{"t":5,"0":{"f0":[{"g":14}]},"1":{"f1":[{"g":14}]}}] I think it need for inverting state of HK sensor and real buttons. ->"a":[{"t":5,"0":{"f1":[{"g":14}]},"1":{"f0":[{"g":14}]}}]

RavenSystem commented 5 years ago

@bj-tracer Sorry, you are wrong. "f0" and "f1" can not be into "0" and "1". They are different things.

A motion sensor accessory could be: {"t":5,"f0":[{"g":14,"t":0}],"f1":[{"g":14}]}

Or if you want a better approach, you can use inching to keep the HomeKit motion sensor activated for 30 seconds, for example: {"t":5,"i":30,"f1":[{"g":14}]} See that "f0" is removed because when "f1" is launched, after 30 seconds the HomeKit motion sensor state will be 0 again automatically, without any external interaction.

An additional option could be adding hardware actions when an event occurs, so here is where "0" and "1" can be used. For example, when sensor detects movement, trigger internal relay to turn on a siren for 10 seconds: {"t":5,"i":30,"f1":[{"g":14}],"1":[{"g":12,"v":1,"i":10}]}

mafyata commented 5 years ago

How did you do the sonoff basic to have motion sensor? Is it hardware connected to gpio pins?

vsidlu commented 5 years ago

@RavenSystem thanks for explanation and samples. I will use sonoff basic with motion sensor to turn on outside lights when there is movement (and send snapshot from camera). So last sample is not for this situation as it will turn on lights also in daytime. So I will use second json and add automation in home app to work only nighttime. Thanks for your effort!

vsidlu commented 5 years ago

How did you do the sonoff basic to have motion sensor? Is it hardware connected to gpio pins?

yes! you connect PIR sensor to gpio 14. my PIR sensor also needed 5v, so additional soldering was necessary.

mafyata commented 5 years ago

Can you post the link with instruction?

vsidlu commented 5 years ago

Can you post the link with instruction?

i still need to check if json is working and then i can post how i managed it!

bj-tracer commented 5 years ago

@RavenSystem Thanks for reply. I just supposed ;)

gerardwr commented 5 years ago

@RavenSystem said:

Or if you want a better approach, you can use inching to keep the HomeKit motion sensor activated for 30 seconds, for example: {"t":5,"i":30,"f1":[{"g":14}]}

Had quite some trouble composing this in a valid JSON for HAA, but in the end I succeeded using:

{"a":[{"t":12,"i":10,"f1":[{"g":14}]}]}

Note that:

If you check the Serial out it says:

15:47:36.141 -> HAA > ACCESSORY 0
15:47:36.141 -> HAA > Accessory type=12
15:47:36.141 -> HAA > Enable button GPIO 14, type=1, inverted=0
15:47:36.141 -> HAA > Sensor bool activated

The MotionsSensor is recognised by the IOS Home App, and changes status to "MotionDetected" when the signal on GPIO14 changes from LOW to HIGH. After 10 seconds the status in the App is changed to the default state, ready for the next Low-to-High transition on GPIO14.

In the IOS App you can configure the MotionSensor to Notify when Motion is detected, both by a NotificationBar and by a Sound.

vsidlu commented 5 years ago

Can you post the link with instruction?

i still need to check if json is working and then i can post how i managed it!

As promised link for Sonoff basic with motion sensor - https://www.ljcaesar.com/2018/02/sonoff-smart-wifi-switches-tasmota-external-sensor-5v-pir-mod/

Instead of Tasmota use HAA with json bellow - {"c":{"o":1,"l":13,"i":1},"a":[{"t":1,"0":{"r":[{"g":12,"v":0}]},"1":{"r":[{"g":12,"v":1}]},"b":[{"g":0,"p":1,"t":1}]},{"t":12,"i":10,"f1":[{"g":14}]}]}

all thanks to @RavenSystem

RavenSystem commented 5 years ago

If you want, you can remove default values and UART log output, and add a button to enter setup mode (holding down 10 seconds):

{"c":{"l":13,"i":1,"b":[{"g":0,"t":5}]},"a":["0":{"r":[{"g":12}]},"1":{"r":[{"g":12,"v":1}]},"b":[{"g":0}]},{"t":12,"i":10,"f1":[{"g":14}]}]}

vsidlu commented 5 years ago

thanks! you where missing { in accessory - see if I made right correction - {"c":{"l":13,"i":1,"b":[{"g":0,"t":5}]},"a":[{"0":{"r":[{"g":12}]},"1":{"r":[{"g":12,"v":1}]},"b":[{"g":0}]},{"t":12,"i":10,"f1":[{"g":14}]}]}

RavenSystem commented 5 years ago

Sorry. Yes, you are right.

vsidlu commented 5 years ago

I also do not see accessory type 1. This could be right - {"c":{"l":13,"i":1,"b":[{"g":0,"t":5}]},"a":[{"t":1,"0":{"r":[{"g":12}]},"1":{"r":[{"g":12,"v":1}]},"b":[{"g":0}]},{"t":12,"i":10,"f1":[{"g":14}]}]}

How accessories are proceed to homekit? What happens if I change order - first t:12 and then t:1? Right now I see in homekit switch with accessory (motion). Is there way to add two separate accessories?

RavenSystem commented 5 years ago

Accessory type 1 is the default option and it is not necessary.

If there are 4 accessories or less, first accessory works as accessory bridge. All accessories are individuals, because each service uses its own accessory.

vsidlu commented 5 years ago

thanks! now it is clear for me!

mafyata commented 5 years ago

So with motion it will on/off , but can I switch to ON manually from home app? And second question- if I switch to ON from home app and detect motion will it switch to off after XX seconds by motion sensor?

vsidlu commented 5 years ago

So with motion it will on/off , but can I switch to ON manually from home app? And second question- if I switch to ON from home app and detect motion will it switch to off after XX seconds by motion sensor?

if you use this json - {"c":{"l":13,"i":1,"b":[{"g":0,"t":5}]},"a":[{"t":1,"0":{"r":[{"g":12}]},"1":{"r":[{"g":12,"v":1}]},"b":[{"g":0}]},{"t":12,"i":10,"f1":[{"g":14}]}]}

you will have two separate accessories - switch and motion sensor (not related to each other). you can make any automation using homekit automations. example i use (switch on for 10 minutes after there is no motion) - ~~automation rule 1 - if there is motion turn on switch for 1 hour. automation rule 2 - if there is no motion turn on switch for 10 min.~~ ios13 do not need two rules - now rule to turn on for x min will reset timer itself if there is motion during thees x minutes and countdown will restart x min.

if you want to trigger switch with motion without any rule you need to use @RavenSystem explained json - An additional option could be adding hardware actions when an event occurs, so here is where "0" and "1" can be used. For example, when sensor detects movement, trigger internal relay to turn on a siren for 10 seconds: {"t":5,"i":30,"f1":[{"g":14}],"1":[{"g":12,"v":1,"i":10}]}

mafyata commented 5 years ago

Where did you make automations? In Shortcuts app?

vsidlu commented 5 years ago

Apple Home app!

mafyata commented 5 years ago

Did you see this: http://techgurka.blogspot.com/2013/05/cheap-pyroelectric-infrared-pir-motion.html I think that I don't need to solder 5v cable on my sonoff basic.

vsidlu commented 5 years ago

yes i know this option. will check if it is reliable.

mafyata commented 5 years ago

So , my sonoff basic is r2 and I can’t use gpio14. Do you know which gpio I can use instead of gpio14?

mafyata commented 5 years ago

I found this on the net. CA840A14-0497-4D6C-B0A0-9D32DEC18D80 CE385D66-BCC4-4B7C-BE71-BF3C3E06C8F2

RavenSystem commented 5 years ago

You can use GPIO3.

mafyata commented 5 years ago

Do you mean RX ?

RavenSystem commented 5 years ago

GPIO3 is RX or TX, I can’t remember now.

bj-tracer commented 5 years ago

GPIO3 = RX And this board has GPIO2 instead GPIO14. But some sensors incorrect working with it.

mafyata commented 5 years ago

Can I use sonoff th16 instead of sonoff basic. It will be cool that I can make a plug for the pir sensor. 2F86EEE9-3D4A-4DB3-B48A-ED7A203FC6CD

RavenSystem commented 5 years ago

@mafyata You can use any device you want.

mafyata commented 5 years ago

Sorry , I mean with this json for sonoff basic.

claire078 commented 4 years ago

GPIO3 is RX or TX, I can’t remember now.

I'd like to use a motion accessory with sonoff R2 board. It didn't work. I tried simple switch with external switch on GPIO3 - RX, but it doesn't work too.

{"c":{"l":13,"b":[{"g":0,"t":5}]},"a":[{"0":{"r":[{"g":12}]},"1":{"r":[{"g":12,"v":1}]},"b":[{"g":0},{"g":3},{"g":3,"t":0}]}]}

RX should with GND close, right?

mafyata commented 4 years ago

Hey , I'm trying to set up Sonoff th16 with pir sensor on gpio14. On/Off work well , but pir is not detecting in home app. My json is : {"c":{"l":13,"i":1,"b":[{"g":0,"t":5}]},"a":[{"0":{"r":[{"g":12}]},"1":{"r":[{"g":12,"v":1}]},"b":[{"g":0}]},{"t":12,"i":10,"f1":[{"g":14}]}]} Is this json correct?

mafyata commented 4 years ago

Now trying json from wiki : {"c":{"l":13},"a":[{"t":1,"0":{"r":[{"g":12,"v":0}]},"1":{"r":[{"g":12,"v":1}]},"b":[{"g":0}]},{"t":12,"i":10,"f1":[{"g":14}]},{"0":{"r":[]},"1":{"r":[],"s":[{"a":1}]},"t":1,"b":[],"s":0}]} Same result, no motion detection.