Command-line Light Control

Although I like Control4’s touchscreens, the Android app, and Control4’s SR-250 remote, sometimes when I want to adjust my office lights or turn on / off the office ceiling fan, I’d rather do that with a simple command-line application.

Since I typically have a terminal window or two open, I can quickly type ‘lights 20’ or ‘fan off’, much faster than I can start up the Control4 Android App, or find my SR-250.

To that end, a while back I created a small Lua script that does just that.

A Zipped version is here: C4Light.lua (Zipped).

NOTE: I’ve only used this on Linux, but it should also work on Unix / OSX, and maybe even Windows, with Lua For Windows.

The script requires that you have Lua and LuaSocket installed. Google is a good resource for this for various situations.

To use the script, first you need to unzip the script, and put the C4Light.lua somewhere on the executable path. I’ve installed mine in /usr/local/bin/.

For convenience, in Linux / Unix you can execute the following lines to make symbolic links to the script, so you can just say ‘lights 20’, or ‘light 33’ or ‘fan on’, instead of C4Light.lua 20. The script uses the name it’s called with to determine whether to send commands to the fan or the light.

ln -s /usr/local/bin/C4Light.lua light
ln -s /usr/local/bin/C4Light.lua lights
ln -s /usr/local/bin/C4Light.lua fan

Once the script is setup and you have symbolic links for fan, light, and lights, you need to edit the C4Light.lua file to customize it for your Control4 setup:

  • Change DirectorIP (‘192.168.29.226’) to the IP Address of your main Control4 controller.
  • Change defaultLight (78) to the Device ID of the light you wish to control (See Below)
  • Change defaultFan (80) to the Device ID of the fan you wish to control

To determine the light and fan Device ID’s, you can use the following trick:

In Composer (Homeowner’s Edition should work fine for this), create a single line of programming with the light or fan whose Device ID you wish to know. Right-click on the programming, and copy the programming to the clipboard. Paste the programming into a text editor (or XML editor), and you will see a Device Command inside the ‘cutitems’ section, like this:

<cutitems>
    <item>
        <Proxy>light</Proxy>
        <Type>Command</Type>
        <DeviceID>78</DeviceID>
        <Description>Turn on the NAME</Description>
        <XmlData>
            <devicecommand owneridtype="" owneriditem="-1">
            <command>ON</command>
            <params/>
        </devicecommand>
        </XmlData>
    </item>
</cutitems>

Once you have the script setup, you can also create shortcuts in your OS to call the scripts.

With any luck and a small amount of brains and effort, you can be just as lazy as me!

🙂

4 thoughts on “Command-line Light Control

  1. This is what I have been looking for, a way to control lights from my android phone without using C4s software. When I try to run the script I get an error. 70:bad argument #1 to ‘lower’ (sting expected, got nil) stack traceback: [C]: in function ‘lower’ I am trying this in windows with C4Light.lua right under program files(x86)\Lua\5.1\

    Thoughts as to why this is erroring or I could be doing something really stupid. I did get the device IDs and IP address in the script.

  2. I use it by creating links to programs called ‘fan’ and ‘lights’, so the first parameter needs to be the program name, like it works in Linux / Unix. I suspect that’s your issue. You may have to modify the script yourself if it’s not working, as I’ve never tried it in Windows.

    Sorry.

  3. What would it take to write a php socket to send these commands? I would like to create my own web page to send these commands. Once you have a socket open what info needs to be send to the controller? Is it the xml data that you grab from the copy and paste? Below is my paste from my kitchen light.

    lightCommand14Set Level on the NAME to 100SET_LEVELLEVEL100

Leave a Reply

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