No announcement yet.

HS4 and Switchbot with Node-RED

  • Filter
  • Time
  • Show
Clear All
new posts

    HS4 and Switchbot with Node-RED

    I don't own any Switchbot products, but I am going to buy some now


    cloud control =

    local control =
    (Linux only)

    There is an ongoing joke with my wife that she bought a fancy coffee machine that can only be controlled by a button push after power up. Now I can HS4 that, nice. lol
    So many more applications. If only they were easily available in Canada at a reasonable price

    Originally posted by dev120v View Post
    Found out about these recently and have some great ideas.
    Just wondering if it's worth getting the hub and then integrating that with node-red and HS, rather than the node install with bluetooth, as I've never liked bluetooth with linux due to the distro/stack/hardware combinations never being stable or just stopping working randomly.

    Please do let us know what your experience is, if anything I've learned over the years about HA, it's not to be dependent on one particular product or protocol, and hopefully the newer bluetooth stuff is worth bothering with.


      I was probably going to try the local bluetooth first. I have like 10 bluetooth dongles I've collected over the years. I can use those if the RPi onboard bluetooth doesn't work


        I just bought a 4 pack of switchbot button pushers. The deal is an alright price right now for valentines. 10% off code SWITCH4LOVE and the buttons are also on sale (4 pack is also on sale)

        I used this youtube guys affiliate link (I watch his videos here and there)

        I am not sure what I will use the other 3 for but they are also easy gifts if I can't find a place for them


          Amazon seemed to have 15% off today, so bought 2 - will try bluetooth on the PI 4, and see if they work ok and report back.


            So, using a RPI4 with integrated bluetooth - as usual, always fun and games.
            After the wrangling with the installs, it wouldn't work with various errors. Basically, it came down to
            pip3 download gattlib
            tar xvzf ./gattlib-0.20201113.tar.gz
            cd gattlib-0.20201113/
            sed -ie 's/boost_python-py34/boost_python-py37/'
            pip3 install .
            sudo bluetoothctl
            power on
            scan on
            sudo python3 -d xx:xx:xx:xx:xx:xx -c press

            Now to see if I can put this in the docker container and run it using dockerised node-red....


              nice! I am still waiting on mine. Thanks for doing the leg work

              but overall it is working good so far? next test is does it work reliably over time?

              I am guessing switchbot started to get a ton of orders once they released the api


                I nearly gave up as after doing the above, it kept coming back with a timeout and/or too many links errors. I tried various scripts to reset the bluetooth (hciconfig up/down, bluetoothctl power off/on) , but it just kept dying the same way.
                I think after getting it to use hcitool and constantly scan, it started working and it's not stopped since. Exactly why I do not like BT devices in Linux and I'm sceptical that this is a viable option once I let it soak test for a few days.

                Just a warning for those reading - I really made everything stupid hard with dockerizing nodered, won't be doing that in the future.
                The time learning and working out differences between compose and CLI, plus the quirks of networking and how each container is its own distro that will guaranteed to be somehow in strange territory where no-one has done it that particular way has poisoned that well for me. The nodered container is based on alpine, and just couldn't get BT to work with that even with docker configured right.

                Anyways, I went with creating a named pipe on the host, mounting that in the container, then creating a listener to basically call a script on the host:
                Then in nodered just created a flow that does an exec to the script which does the press:
                echo "/home/pi/python-host/" > /hostpipe
                Node red flow is now triggering the switchbot once an hour, will leave it a few days and see if it's still working and report back.


                  Ok, report back time.
                  There's some lag in switching - so I wouldn't use these in place of the usual tech like zigbee, zwave if there's an instant requirement
                  The LE is quite good at range and going through walls, etc.
                  Issuing 2 or 3 switches in rapid succession doesn't really work - I'd say throttle to around once every 10 seconds at the quickest.
                  There is the occasional timeout, and that presents a problem with the next point:
                  The python host scripts are.... a bit basic really. No error trapping, no retry, if it can't connect you get a lovely stack trace output and that's it. You'll need to have something in place to trap for that problem and reissue the command until you get a "connection succeeded" type output.

                  To elaborate on the above point - nothing to stop you sending 3 commands for off or on in a row over a minute to try and get a "guaranteed" switch action as 3 "ons" will end up with the switch being on. Problem is if you're using it on a device like I have where its doing a press, so if you get a timeout, you're still in the last state.

                  So, in conclusion - not a bad device really. Very useful for those oddball switch requirements, and if you can get a BT dongle which has an external antenna, I expect you'd get some serious range on it.


                    ya I definitely wouldn't use where a smart switch could be placed. Only for those random buttons that need a physical push or for temporary solutions

                    also I think they would be one off calls, like make coffee etc. I wouldn't call a bunch of them all at once

                    My switchbots should be here this week. I got digned for DHL customs from HK.

                    if I don't find a permanent spot for them all I can always just use them to trigger some dominos or a Rube Goldberg machine, lol


                      Yeah, they're cool little devices and seem to have a decent amount of torque in them. If they could integrate them into an actual wall plate switch and convert them to use zigbee, I'd probably replace my entire house with them.
                      Here in the UK we're seriously limited by not having the neutral in our light wiring, and no-one has ever really thought about attacking switch control with a physical actuator. You could also switch huge loads with such a setup too....


                        Furious So I got it working same as you but the signal strength is crap. I also tried with an asus small dongle and it would only work right next to it. I'll need to try another dongle

                        A couple things to consider with bluetooth signal strength
                        Bluetooth uses 2.4Ghz, so does 2.4ghz wifi (use 5ghz or lan cable instead)
                        USB 3.0 interferes with 2.4 ghz signal. My USB SSD on usb3 on my Rpi4 could be affecting this


                          So I looked at the gattool commands being used in the switchbot code. This would be the sequence of commands for a button press

                          If I call connect a bunch of times, eventually it does seem to connect, then once connected the 3rd command always seems to work

                          gatttool -b XX:XX:XX:XX:XX:XX -t random -I
                          char-write-cmd 0x0016 570100

                          I am able to get it to work across my house now, but I still need to test more. I sometimes have to call connect like 20 times. When the switchbot is in the same room, it takes at most 3 connects

                          I can see the MAC address when I do scanning so not sure why the connect fails alot


                            After struggling with linux bluetooth to cover the entire house with switchbots bluetooth signal reliably (I even tried with using a long range bluetooth adapter), I decided to just write some arduino code for an ESP32. They can be purchased for a couple bucks, super simple to use

                            Here is my code, Switchbot button to MQTT installed on ESP32 (So windows users can use this option also instead of direct Linux)


                            I could have just made a servo with ESP32 but this gives a better wireless look and seems to work as "reliably" as the switchbot app


                              If you are going to that length, wouldn't it be cheaper to add a servo to your arduino?
                              Karl S
                              HS4Pro on Windows 10
                              1070 Devices
                              56 Z-Wave Nodes
                              104 Events
                              HSTouch Clients: 3 Android, 1 iOS
                              Google Home: 3 Mini units, 1 Pair Audios, 2 Displays