devWaves / SwitchBot-MQTT-BLE-ESP32

Allows for multiple SwitchBot bots and curtains to be controlled via MQTT sent to ESP32. ESP32 will send BLE commands to switchbots and return MQTT responses to the broker. Also supports Temperature, Motion, Contact sensors
MIT License
545 stars 68 forks source link

adding Contact Sensor & Motion Sensor #27

Closed BangerTech closed 2 years ago

BangerTech commented 3 years ago

Hi there,

first of all a huuuge Thank you for the great work!!!

i was wondering if ther would/ will be a way to connect the contact & motion sensor in the near future?

thanks again

greets

devWaves commented 2 years ago

There is a new version here... v6Alpha5 https://github.com/devWaves/SwitchBot-MQTT-BLE-ESP32/blob/v6MotionContact/SwitchBot-BLE2MQTT-ESP32.ino

It should have all the data elements for contact and sensor except for lastMotion and lastContact because I do not understand the switchbot documentation for lastMotion and lastContact.

if you see an issue where the esp32 reboots randomly, let me know. I think I fixed that though. You would see this in MQTT

switchbot/esp32
"{"status":"boot"}"

it means the ESP32 rebooted. If you get that and you didn't purposely restart the esp32, then that is the issue I am trying to figure out. looks like a memory issue

devWaves commented 2 years ago

I think I fixed all the issues. try v6Alpha5 from https://github.com/devWaves/SwitchBot-MQTT-BLE-ESP32/blob/v6MotionContact/SwitchBot-BLE2MQTT-ESP32.ino and let me know please

you will want to delete your previous contact and motion devices if you created any

If you don't have issues with this version I will release on main branch

I will add some changes after for getting the last trigger contact/motion timestamps but I don't understand the documentation at the moment and I don't think anyone else has coded for that yet either to reference. It is a //TODO in the python code. The last trigger timestamps can be used to figure out if a motion/contact was missed while controlling a bot, and resend the message if needed

1) the contact/motion sensors will update whenever there are changes 2) the bot/curtain/meter will only scan for changes when requested. So if you use the switchbot app you need to wait for the next scan to get the latest status. You can set rescanTime to a low value and it will scan somewhat continuously if you want quicker updates if you are also using the switchbot app

just keep an eye out for a error reboot mqtt message. Those are not good

switchbot/esp32
"{"status":"boot"}"
BangerTech commented 2 years ago

thanks! sooo for me i get no reboots, runs for like 3 hours now and no reboot so far. i´ve tested each device and the sensor work fine, i get all informations and they change values immediately. But i get into some changes which you could make later. the only device which makes problems right now is the bot. so for instance if control the bot via mqtt with publishing messages: (in mode:press) if i send ON i get esp32 : status: idle then immediately esp32: status: scanning and a state change in the attributes to ON . But if i send OFF i get esp32: status : idle then immediately esp32: status: scanning and a attributes message but without a change of state and the bot doesn´t do anything. If i send a PRESS message the bot works all the time but no state change in the attributes i get a attributes message though.

ahhh just saw that you updated the code so i do some test again ;-) so don´t take this upper stuff ;-) i´ll be testing the new one

BangerTech commented 2 years ago

soo now for the bot: (press mode)

soo then i change to switch mode:

devWaves commented 2 years ago

yes that all sounds correct. an "OFF" command should be ignored if the bot is in press mode

when the bot is in press mode, only send "PRESS" or "ON"

the state will return back to "OFF" when in press mode.

devWaves commented 2 years ago

you should see a message under the button topic for contact devices. you can use the button on the contact device to trigger other things in your smarthome (non switchbot) whenever the payload is "PUSHED"

BangerTech commented 2 years ago

if i control the bot via Switchbot APP: PressMode:

ok just got your message, ok got it would be cool if it jumps between on and off even in pressmode to get some kind of knowing what state the device you want to control is in or am i wrong? maybe its not possible

devWaves commented 2 years ago

yep you can do that. This is when you want to add your device also to the simulated on/off list

this is already a feature

BangerTech commented 2 years ago

ahh sorry just saw it in the code ;-)

devWaves commented 2 years ago

when the device is also in the botsSimulateONOFFinPRESSmode list you can use "OFF" command

BangerTech commented 2 years ago

awesome thanks! so the bot works as expected i´ll check on the other devices now

BangerTech commented 2 years ago

so the sensors work fine i think everything is working as expected. Great Work Buddy!!!! i´m running a youtube channel and i did a video about the switchbots can i quote you on the next one when i´ll show how to implement the devices with your code in openHAB?

devWaves commented 2 years ago

oh ya cool! go ahead. Send me a link. I'll post the newer version later today

BangerTech commented 2 years ago

just checked again on the PressOnOFF mode but it doesn´t work as expected so i set switchbotone: false so default state is OFF. now when i press the button in the app i get updates on mqtt as set (240sec) but with state: OFF and in the attributes state: OFF too, the mode in attributes is in PressONOFF. But it should be in state ON right

BangerTech commented 2 years ago

If i only control it with mqtt so publish ON than a few min later OFF everything works perfect i get the right states and everything

devWaves commented 2 years ago

if you use the switchbot app to action a bot when in simulated on/off mode you need to manually call "STATEON" or "STATEOFF" to fix the state on esp32

I am only guessing the state by toggling between on/off , and have no idea when the switchbot app sends a press command. Nothing I can do there sorry

I can only get the real state from switchbot when in switch mode

BangerTech commented 2 years ago

yaa for me thats no problem at all ;-) i´m controlling these devices only with mqtt anyways ;-) thanks again for the great work! was a pleasure

devWaves commented 2 years ago

new version 6.0 released for motion sensor and contact sensor support

cheers

devWaves commented 2 years ago

found a small issue for meter in v6. I will release a v6.1 later today Meter isnt updating every 60 seconds like it should