Use Siri to Control Wireless Power Outlets (Homebridge)

This is a follow up for my guide on how to control cheap wireless power outlets using a raspberry pi. This is part 1 of a 3 part series on controlling the wireless power outlets using your voice. Part 2 is on Google Home and part 3 covers the Amazon Echo.

With Apple’s Home app, you can control any device that is HomeKit enable from your iOS device. Lucky for us an open source project called HomeBridge was created so we can control the devices from a raspberry pi. HomeBridge is a great open source project that has many plugins and allows you to customize your home automation.

Steps to Setup HomeBridge to control Wireless Power Outlets

  1. Install HomeBridge on your raspberry pi
  2. Setup HomeBridge to start on bootup
  3. Install CmdSwitch2, a plugin for HomeBridge that allows you to execute commands via HomeKit
    1. npm install -g homebridge-cmdswitch2
  4. Edit your HomeBridge config.json file
    1. Type this command to edit your config file.
    2. sudo nano /var/homebridge/config.json
    3. Add this section to the platforms array. Example of my config.json
    4. {
       "platform":"cmdSwitch2",
       "switches":[
         {
           "name":"Lamp",
           "on_cmd":"/var/www/html/rfoutlet/codesend ******",
           "off_cmd":"/var/www/html/rfoutlet/codesend ******"
         }
        ]
      }
    5. Replace the asterisks with your 6 digit code. Verify that /var/www/html/rfoutlet/codesend is the correct path to your codesend executable.
    6. You can add multiple lights just be sure to name them appropriately.
    7. Save and exit (Use “ctrl + x” then “y” to save the file) then restart HomeBridge:
    8. sudo systemctl restart homebridge
    9. Install the Home app on your iOS device.
    10. Connect to the same network as your raspberry pi and you should now see your accessories
    11. Say “Hey Siri, turn on the lamps” or “Turn off the lamps”

 

Here is a guide if you would like to control your lights from when you are away from home. You must have an iPad or Apple Tv to act as your HomeKit hub.

Please leave a comment below if you have any questions.


Also published on Medium.

59 thoughts on “Use Siri to Control Wireless Power Outlets (Homebridge)

  1. Thanks for writing another really good guide.

    I had a couple quick questions. Could you post a good example config.json file? Should there be an accessories section if all you’re trying to control are the RF outlets?

    Thanks.

  2. How about a way to control this over the internet? I’m aware of the ‘must be connected to same network’ limitation from many of my standard Apple hardware and apps like remote or Appletv, but I’m confused at the limitation given that whole ‘World Wide Web’ thing that’s always on and connected thru my router.
    With a ‘same network’ limitation, I would assume that it wouldn’t be possible to program the switches to turn on say, when you’re 2 miles away from your house based on gps, i.e., a ‘smart’ switch control for lights for when you’re about to arrive home from work, as opposed to waiting to get home, having some amount of lag for your phone to connect to home wifi, then once it does ask Siri to turn on lights, then lights come on. Honestly, in that scenario just placing the rf remote at my front door seems way more convenient and efficient than using Siri.
    I guess I need to teach myself computer programming and networking to figure this stuff out, but I only have so much free time.

      1. Hi Tim,

        Thanks for writing up this guide! Its really awesome. I was able to get my RF-based switches into Homekit!
        Though I could actually control these lights when I wasnt on my home wifi, i.e. from my office. My wife did confirm that the lights turned on/off.
        From what I have heard you need an apple tv 4th gen or an ipad to act as a homekit base (and stays within the home wifi). This also always you to set home automation tasks on your iphone. Hope that helps.

  3. Mine is failing on:

    [Fri Dec 23 2016 09:31:32 GMT+0000 (UTC)] Initializing platform accessory ‘Fönst er Lampa’…
    [Fri Dec 23 2016 09:31:32 GMT+0000 (UTC)] Homebridge is running on port 51925.
    [Fri Dec 23 2016 09:31:32 GMT+0000 (UTC)] Failed to determine Fönster Lampa stat e.
    [Fri Dec 23 2016 09:31:32 GMT+0000 (UTC)] /bin/sh: 1: undefined: not found

    homebridge-cmdswitch2homebridge-cmdswitch231:32 GMT+0000 (UTC)] Homebridge is running on port 51925.
    [Fri Dec 23 2016 09:31:32 GMT+0000 (UTC)] Failed to determine Fönster Lampa stat e.
    31:32 GMT+0000 (UTC)] Homebridge is running on port 51925.
    [Fri Dec 23 2016 09:31:32 GMT+0000 (UTC)] Failed to determine Fönster Lampa stat e.
    31:32 GMT+0000 (UTC)] Homebridge is running on port 51925.
    [Fri Dec 23 2016 09:31:32 GMT+0000 (UTC)] Failed to determine Fönster Lampa stat e.
    31:32 GMT+0000 (UTC)] Homebridge is running on port 51925.
    [Fri Dec 23 2016 09:31:32 GMT+0000 (UTC)] Failed to determine Fönster Lampa stat e.

    How can I fix this? (Fönster Lampa is swedish and means Window Lamp)

  4. This is my config:

    “bridge”: {
    “name”: “HomeBridge”,
    “username”: “DD:33:4F:E3:CE:30”,
    “port”: 51826,
    “pin”: “031-45-154”
    },

    “description”: “”,

    “accessories”: [
    {
    “accessory”: “DS18B20”,
    “name”: “Temperature Sensor”,
    “device”: “28-0000055d1fea”
    }
    ],

    “platforms”:[
    {
    “platform”:”cmdSwitch2″,
    “switches”:[
    {
    “name”:”Lamp”,
    “on_cmd”:”/var/www/html/rfoutlet/codesend 1361 -l 314 -p 0″,
    “off_cmd”:”/var/www/html/rfoutlet/codesend 1364 -l 314 -p 0″
    }
    ]
    }
    ]
    }

    .
    This is the output

    [email protected]:~ $ homebridge
    *** WARNING *** The program ‘node’ uses the Apple Bonjour compatibility layer of Avahi.
    *** WARNING *** Please fix your application to use the native API of Avahi!
    *** WARNING *** For more information see
    *** WARNING *** The program ‘node’ called ‘DNSServiceRegister()’ which is not supported (or only supported partially) in the Apple Bonjour compatibility layer of Avahi.
    *** WARNING *** Please fix your application to use the native API of Avahi!
    *** WARNING *** For more information see
    [Sun Dec 25 2016 10:52:25 GMT-0100 (GMT+1)] Loaded plugin: homebridge-cmdswitch2
    [Sun Dec 25 2016 10:52:25 GMT-0100 (GMT+1)] Registering platform ‘homebridge-cmdswitch2.cmdSwitch2’
    [Sun Dec 25 2016 10:52:25 GMT-0100 (GMT+1)] —
    [Sun Dec 25 2016 10:52:25 GMT-0100 (GMT+1)] Loaded plugin: homebridge-ds18b20
    [Sun Dec 25 2016 10:52:25 GMT-0100 (GMT+1)] Registering accessory ‘homebridge-temperature-ds18b20.DS18B20’
    [Sun Dec 25 2016 10:52:25 GMT-0100 (GMT+1)] —
    [Sun Dec 25 2016 10:52:25 GMT-0100 (GMT+1)] Loaded config.json with 1 accessories and 1 platforms.
    [Sun Dec 25 2016 10:52:25 GMT-0100 (GMT+1)] —
    [Sun Dec 25 2016 10:52:25 GMT-0100 (GMT+1)] Loading 1 platforms…
    [Sun Dec 25 2016 10:52:25 GMT-0100 (GMT+1)] Initializing cmdSwitch2 platform…
    [Sun Dec 25 2016 10:52:25 GMT-0100 (GMT+1)] Loading 1 accessories…
    [Sun Dec 25 2016 10:52:25 GMT-0100 (GMT+1)] [Temperature Sensor] Initializing DS18B20 accessory…
    Scan this code with your HomeKit App on your iOS device to pair with Homebridge:

    ┌────────────┐
    │ 031-45-154 │
    └────────────┘

    [Sun Dec 25 2016 10:52:25 GMT-0100 (GMT+1)] Initializing platform accessory ‘Lamp’…
    [Sun Dec 25 2016 10:52:25 GMT-0100 (GMT+1)] Homebridge is running on port 51826.
    [Sun Dec 25 2016 10:52:25 GMT-0100 (GMT+1)] Failed to determine Lamp state.
    [Sun Dec 25 2016 10:52:25 GMT-0100 (GMT+1)] /bin/sh: 1: undefined: not found
    .

    I ran sudo npm install -g homebridge-cmdswitch2 to install it.

    1. I were able to get it to function with siri when i said “Turn on Lamp” but i need to get it to work in the home app (The app that comes along ios 10) it always and seems to always worked with siri but i need to get it to work in the home app as well.

    2. Anton, did you solve your issue? I have exactly the same issue with:
      Failed to determine Lamp state.
      /bin/sh: 1: undefined: not found
      (I installed Homebridge and homebridge-cmdswitch2 correctly with sudo and global)

        1. Hi Tim perhaps it is just my inexperience 😉 . I have installed homebridge and the plugin globally with sudo and my config.json is located in ~/.homebridge (raspbian Jessy).
          Running ” tail -f /var/log/homebridge.log” I am getting:
          [2017-01-02 21:19:50] Loaded plugin: homebridge-cmdswitch2
          [2017-01-02 21:19:50] Registering platform ‘homebridge-cmdswitch2.cmdSwitch2’
          [2017-01-02 21:19:50] —
          which is good… but then the second phase fails
          [CMD Switch] Initializing platform accessory ‘Lamp1’…
          [CMD Switch] Failed to determine Lamp1 state.
          [CMD Switch] /bin/sh: 1: undefined: not found

          Here is a snippet from my config.json (i have two platforms defined):

          “platforms”: [
          {
          “platform”: “Nest”,
          “token” : “****************************”
          “clientId”: “*****************”,
          “clientSecret”: “****************”,
          “code”: “******”,
          “username” : “ton@*********”,
          “password” : “***************”
          },
          {
          “platform”:”cmdSwitch2″,
          “name”: “CMD Switch”,
          “switches”:[
          {
          “name”: “Lamp1”,
          “on_cmd”: “/var/www/rfoutlet/codesend 13857643”,
          “off_cmd”: “/var/www/rfoutlet/codesend 13857642”
          }
          ]
          }
          ]
          }

          Apologies in Advance 😉
          Tony

          1. p.s. some extra detail.
            In the IOS Home App the accessoire “Lamp1” shows “not responding”. And in the IOS home app the lamp1 can’t be turned on /off by pressing. But using Siri voice command you can turn it on/off). I also reached out to the plugin developper luisiam 😉

          2. Adding some findings on my previous thread:
            Adding a bogus state_cmd solves the state in the IOS home app and the lamp can now be turned on /off by pressing.
            My trick was: “state_cmd”: “/var/www/rfoutlet/codesend 13857642 | grep -i ‘bogus'”
            I can now see that at Homebridge startup the [CMD Switch] Initializing platform accessory goes well and [CMD Switch] reports Lamp1 (in a valid state) as off (obviously with an RF Lamp this is bogus and just a trick). This proofs that there is a software dependancy towards “state_cmd” in order to turn on / off the lamp by pressing in the IOS home app

          3. To close on this issue 😉 The developper (luisiam) suggested upgrading cmdSwitch2 to the latest node v7.4.0 as it contained some countermeasures. . This solved the issue!
            He suggested trying to update with
            sudo npm install -g git+https://github.com/luisiam/homebridge-cmdswitch2.git#1e10a252b3800dffac50ee16c425cc6e72989d2b

            regards, Tony

  5. Tim, your articles are great and helped me a lot. I started with the first one write up about controlling the outlets just from the RPi. Now I have it working with Homebridge. I have one issue I am hoping you can verify for me before I try to reinstall everything in an attempt to fix my issue.

    Can you setup Groups and Scenes with the outlets in the Apple Home app and get them to work? I can control all the outlets as long as I do them individually. As soon as I create a Group to turn them all off in one room no outlets work. It is the same with Scenes.

    If you can get them to work or think it should then I will have to rebuild Homebridge which I am not looking forward to.

    Any help would be much appreciated!

    1. Thanks! Glad they’ve helped. I just setup a scene and it worked. I added 2 accessories and set them in the on position. I don’t think you have anything wrong if it works with one outlet. I’ll do some more research and see if I can find out anything.

      1. I’m wondering the Home app isends multiple codes too fast and codesend errors out. With the PHP version you have a sleep added to pause a bit.

        1. Possibly..Are you calling the codesend directly? You could try chaining multiple commands together.
          /var/www/html/rfoutlet/codesend ****** && /var/www/html/rfoutlet/codesend ******

          1. I give this a try tonight. Can you confirm when you set up the Scene that you used two of the outlets and not any other accessories? I installed the fake light accessory and I can add that and one outlet to a scene and the outlet works. But anything with at least two outlets and none of the outlets turn off/on.

          2. Ok you are correct. It does not work with 2 outlets when using a scene. I was able to get it to work by adding a small delay.

            "on_cmd": "sleep .1 && codesend 349635",
            "off_cmd": "sleep .1 && codesend 349644"

          3. Okay, Tim. Oddly I don’t get any errors when I run any of this. BUT, no outlets are turned on or off. What makes this really odd is that I can run the index.html and toggle all the outlets on and off with the buttons.

            [email protected]:/var/www/html/rfoutlet $ /var/www/html/rfoutlet/codesend 1054003 && /var/www/html/rfoutlet/codesend 1054147
            Sending Code: 1054003. PIN: 0. Pulse Length: 189
            Sending Code: 1054147. PIN: 0. Pulse Length: 189
            [email protected]:/var/www/html/rfoutlet $ /var/www/html/rfoutlet/codesend 1054003
            Sending Code: 1054003. PIN: 0. Pulse Length: 189
            [email protected]:/var/www/html/rfoutlet $ ls -l
            total 268
            -rwsr-xr-x 1 root root 18832 Dec 19 17:58 codesend
            -rw-r–r– 1 root root 4120 Dec 20 13:35 index.html
            -rw-r–r– 1 root root 206213 Dec 19 21:00 pattern.jpg
            -rw-r–r– 1 root root 820 Dec 19 17:58 README.md
            -rwxr-xr-x 1 root root 18144 Dec 19 17:58 RFSniffer
            drwxr-xr-x 2 root root 4096 Dec 19 17:58 RFSource
            -rw-r–r– 1 root root 397 Dec 19 17:58 script.js
            -rw-r–r– 1 root root 1856 Dec 26 18:49 toggle.php
            [email protected]:/var/www/html/rfoutlet $

            Next I went into my toggle.php file and commented out the sleep line to see if that went too fast but sure enought when I turn all on or off in using the index.html they all work as expected but a bit faster without the sleep between calls.

            Here is my toggle.php just just in case you have any questions.
            array(
            “on” => 1054003,
            “off” => 1054012
            ),
            “2” => array(
            “on” => 1054147,
            “off” => 1054156
            ),
            “3” => array(
            “on” => 1054467,
            “off” => 1054476
            ),
            “4” => array(
            “on” => 1056003,
            “off” => 1056012
            ),
            “5” => array(
            “on” => 1062147,
            “off” => 1062156
            ),
            );

            // Path to the codesend binary (current directory is the default)
            $codeSendPath = ‘./codesend’;

            // This PIN is not the first PIN on the Raspberry Pi GPIO header!
            // Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/
            // for more information.
            $codeSendPIN = “0”;

            // Pulse length depends on the RF outlets you are using. Use RFSniffer to see w$
            $codeSendPulseLength = “183”;if (!file_exists($codeSendPath)) {
            error_log(“$codeSendPath is missing, please edit the script”, 0);
            die(json_encode(array(‘success’ => false)));
            }

            $outletLight = $_POST[‘outletId’];
            $outletStatus = $_POST[‘outletStatus’];

            if ($outletLight == “6”) {
            // 6 is all 5 outlets combined
            if (function_exists(‘array_column’)) {
            // PHP >= 5.5
            $codesToToggle = array_column($codes, $outletStatus);
            } else {
            $codesToToggle = array();
            foreach ($codes as $outletCodes) {
            array_push($codesToToggle, $outletCodes[$outletStatus]);
            }
            }
            } else {
            // One
            $codesToToggle = array($codes[$outletLight][$outletStatus]);
            }foreach ($codesToToggle as $codeSendCode) {
            shell_exec($codeSendPath . ‘ ‘ . $codeSendCode . ‘ -p ‘ . $codeSendPIN . ‘ $
            // sleep(1);
            }

            die(json_encode(array(‘success’ => true)));
            ?>

      1. Not good news. Here is my config file. To test the timing I bumped up the sleep to 2. When I click an outlet I notice the sleep delay. When I click two as fast as I can I notice the delay. But when I create a Scene and watch it execute from inside a Room you can see the accessories executing. All accessories happen at the same time. They don’t go sequentially in a Scene. The little spinning load icons all happen at the same time.

        I really wish I had some other 433mhz accessories so I was not only testing the outlets. I have some on the way from Amazon so Friday I can try two different accessories (lightbulb socket) and see if it has any difference.

        Once more question I guess. You didn’t happen to use the option Running Homebridge on Bootup (init.d) did you? That is what I use and I can turn on a log that show the state of the buttons changing also a err log that shows some stuff. If you used this method we could compare logs.

        This is done like so…

        tail -f /var/log/homebridge.log
        tail -f /var/log/homebridge.err

        I do each of those in a different terminal window to watch what is happening.

        but I think it only works if you use the init.d method to run Homebridge on boot. The state always change correctly in the .log so when it fails I have to click it twice to get the state back correctly. If you want I can post a quick bit from both of these when I try to execute a Group or Scene. There is one line in the err that I don’t understand that you might.

        See if you notice anything below that you might might cause any issues.
        {
        “bridge”:{
        “name”:”House”,
        “username”:”A1:B2:C3:E3:CE:AB”,
        “port”:36631,
        “pin”:”031-45-154″
        },
        “description”:”iHome”,
        “accessories”:[
        {
        “accessory”: “Fake-RGB”,
        “name”: “RGB Bulb”
        }
        ],
        “platforms”:[
        {
        “platform”:”cmdSwitch2″,
        “switches”:[
        {
        “name”:”1″,
        “on_cmd”: “sleep 2 && /var/www/html/rfoutlet/codesend 1056003”,
        “off_cmd”: “sleep 2 && /var/www/html/rfoutlet/codesend 1056012”
        },
        {
        “name”:”2″,
        “on_cmd”: “sleep 2 && /var/www/html/rfoutlet/codesend 1062147”,
        “off_cmd”: “sleep 2 && /var/www/html/rfoutlet/codesend 1062156”
        },
        {
        “name”:”3″,
        “on_cmd”: “sleep 2 && /var/www/html/rfoutlet/codesend 1054147”,
        “off_cmd”: “sleep 2 && /var/www/html/rfoutlet/codesend 1054156”
        },
        {
        “name”:”4″,
        “on_cmd”: “sleep 2 && /var/www/html/rfoutlet/codesend 1054467”,
        “off_cmd”: “sleep 2 && /var/www/html/rfoutlet/codesend 1054476”
        },
        {
        “name”:”5″,
        “on_cmd”: “sleep 2 && /var/www/html/rfoutlet/codesend 1054003”,
        “off_cmd”: “sleep 2 && /var/www/html/rfoutlet/codesend 1054012”
        }
        ]
        }
        ]
        }

  6. Okay, I have confirmed it is absolutely a timing issue. Here is what I did. I got to thinking about what you did with adding in the sleep .1 to the switch cmds. The problem with that is you used a .1 for every one of the outlets. In my case I used 2. But I did that for all of them as well. So if a Group or Scene executes the accessories at the same time like we think it does they are all paused for 2 second. They still all execute at the same time. Duh!

    Here is what I did to prove this. My #1 outlet sleeps 2 and my #2 outlet sleeps 5. Boom! They both came on at their respective sleep times 2, 5. So this tells us that codesend can’t do more than one send at a time. Any ideas on how to work around this? Everything I can think of is just a hack. Multiple codesends files, adding different sleeps for each outlet. Not a good option either way. I am going back now and seeif I can have a .1 and a .2 delay and still get them to work this way. I am not sure who long of a delay they really need between the executions of the on/off.

    {
    “name”:”1″,
    “on_cmd”: “sleep 2 && /var/www/html/rfoutlet/codesend 1056003”,
    “off_cmd”: “sleep 2 && /var/www/html/rfoutlet/codesend 1056012”
    },
    {
    “name”:”2″,
    “on_cmd”: “sleep 5 && /var/www/html/rfoutlet/codesend 1062147”,
    “off_cmd”: “sleep 5 && /var/www/html/rfoutlet/codesend 1062156”
    }

    1. Sounds right. Sleep is in seconds so just increment it by .1. Making multiple copies of codesend could work unless the issue is the rf chip can only be accessed by 1 program at a time and somehow it’s locked.

      1. Not that you mention it I bet it is the chip. That makes total sense. It physically can’t send two things at the same time. I am guessing that is why they wrote the All On and All Off options in the rfoutlets example to run through a loop and execute each command one at a time.

        For anyone else planning to use a fair amount of RF accessories here is what I am planning now that we have figured this out. The best option is to make sure you are using a handful of RPis in your house. Each one acting as a hub their their own RF transmitter. One in each room isn’t really necessary but I’d say anything more than 10 RF accessories being controlled by a single RPi is a good limit. But this is only needed if you want to take full advantage of Scenes, Groups, and building Automations. If someone doesn’t mind clicking on RF accessory at the same time then there is not limit.

        Big thank you Tim for your willingness to work through this with me. I didn’t know the sleep option was even possible. Without that I would still be banging my head against the wall.

        Now, if you can create a blog post on how to add a RPi USB cameras that is using Motion to Homebridge that would be awesome! No pressure buddy. 😉

  7. I have an RF outlet that’s out of range in my basement. If I want to control the RF outlets in the basement is the process to install a second copy of Homebridge on another RPi and then the Home app would just see anything I setup on it as another “zone”?

  8. Thanks – I had some issues with this due to a router problem but finally got it going. Thanks!

    If anyone else is having problems with a router running the Shibby Tomato firmware and you’re running your Pi on your wired network, try moving it to your wireless network. The firmware has problems allowing Bonjour to communicate from wired to wireless.

    Thanks again Tim.

  9. Would it be possible to install homebridge onto a Tomato/DDWRT Router w/ MIPS CPU? What about the a newer one with a ARM CPU?

  10. @ Tim & @Ed – Multiple RF outlets in a scene SOLVED

    I think I solved the issue with multiple RF outlets in one scene. I prefaced each invocation of the codesend command with “flock -x -w 10 /var/lock/codesend.lck “. Basically what that does is attempt to exclusively open the file “/var/lock/codesend.lck” before executing the codesend command. The “-w 10” parameters cause flock to retry for up to 10 seconds. So far this has successfully turned on/off three RF outlets in my living room scene and turned off all five of my RF outlets in another scene. This solution has somewhat of a brute force feel to it but it works well enough for now.

    Thanks for creating such a great tutorial.

  11. Hi, other than Etekcity outlets and transmitter hardware you have referred to, will your plugin any other eg. wifi outlets ?

    Thanks a lot for the plugin

      1. Thanks Tim for your reply and the guide. I’ll report back.

        Before I get my hands dirty, do you see any caveats of running Homebridge on a Mac instead of the rasbi3 for this purpose

        1. Homebridge should work. If the outlets are 433mhz you need a way to send the signal to turn them on/off. On the pi you can use the gpio pins. Not sure how you would do that on your mac

  12. Tim thanks v much for the heads up. I had read the guide but had skipped the hardware requirement that would confine it to a Rasbpi or similar. I am supposed to be receiving the estimote location beacons soon. They support GPIO so they may provide a solution to integrate with the mac.

  13. Hi Tim!!! Thank for the post!!!! I understood that if you want to turn on a light from far away, you need an AppleTV or Ipad acting as a hub at home, logged to the same cloud of the device you are using to activate the switch (touch or Siri)…. Does this eliminate the use of the Raspberry Pi? Or it is still on board and can take part of the link?
    Tank you for your answer!!!

  14. Im excited to do this, so thank you!
    i have questions regarding the raspberry pi B+, can it handle homebridge, power outlets, ha-bridge and alexa-pi altogether? I have setup my homebridge on a raspberry pi B and wondering if i should install the ha-bridge and alexa-pi on a separate raspberry pi B+? Any input is greatly appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *