Using scripts to control lights

Does anyone else use scripting to control lights using OpenHAB? I recently have been on a quest to get a script file (bash on a linux box) to run thru OpenHAB which controls my Lifx and Hue bulbs. So far I have found lightsd to control my Lifx bulbs and if you use a Hue hub, the hub actually has a interface built in. Using GET and Put commands you can control your Hue bulbs. So far, I have gotten a script to run using executeCommandLine and run it from within OpenHAB. (I will post step by step instructions to do all this in this thread as soon as I can)

OK, great, but why bother?
OpenHAB can do a great job controlling the lights. Most the time this is true for switching them on and off based on motion, daylight, whatever using rules. But one thing it doesn’t do very well. This may sound really dumb but… I wanted to blink the lights. Why? because it gets my attention. Like sounds, blinking the light can signal the phone is ringing or… the building is on fire!

It is easily possible to blink the lights using OpenHAB rules and I will post my working example in this thread as well (derived from threads on this forum by the masters) but, I’ve noticed doing to much more then blinking them starts not working so great. In other words, if I need to send commands to change the color, dimmer value and blink on and off with expire binding timers and such, if tested on it’s own works but combined with other activity on the event bus (such as dealing with whatever triggered the lights to blink) gets sluggish, skips steps or gets stuck half way thru in a weird state. There is also a limit in how fast they will blink. For me on a small pc, that is about once a second

This quickly starts to feel like I am trying to drive a nail into a piece of wood but instead of using a hammer, all I have is a pipe wrench. It works but not that well.

This is where lightsd and the Hue bridge api come in. Using a command line to run a script, OpenHAB fires the script and moves on doing other important stuff OpenHAB can do and does not waste resources doing something stupid it was never made to do. I have one working script using a single waveform command to my Lifx bulb that makes it look like there is a police car in the living room. I will post my working scripts and instructions as soon as I can.

Discussion points:
Anyone have tips or working examples of using executeCommandLine and the exec binding or experience setting up permissions and stuff (permissions and user right seem to be the really difficult part of this and I will post what I’ve learned)

other api for other bulbs or whatever you do that works and I haven’t thought of

using scripts to do other cool stuff (I’m also using all this to play sounds)

What cool stuff you use your lights for in automating you setup using OpenHAB

reserved for examples

Example of rule to blink light

var Timer mytimer1 = null
rule "AlertFlashLight"
	Item alertboxmsg changed
	if(mytimer1 !== null) return; // if already blinking ignore this event
	// Create a timer to blink the light for 30 seconds
        val timeStarted = now
        mytimer1 = createTimer(now, [  
        MyLightSwitch.sendCommand(if(MyLightSwitch.state == ON) OFF else ON)
		if(now.isBefore(timeStarted.plusSeconds(10))) mytimer1.reschedule(now.plusSeconds(1))
        else mytimer1 = null	

I think a lot of this might be a combination of sluggish Rules and a limitation on the Hue bulbs/hub themselves. I’ve seen reported that Hue cannot handle sending commands at it at too fast of a rate. I wrote the Design Pattern: Gate Keeper to address this issue. Essentially the commands get put on a queue and worked off at the maximum safe rate.

On-the-other-hand, some users who have wanted to do complex stuff with lighting (e.g. rapid dimming for devices that don’t support it) find they get much better performance from JSR223 Rules over Rules DSL.

I’m not claiming these are panaceas, but they are approaches others have used to deal with the described problems.

There is a posting for people trying to get executecommandLine working at least once a month. Search and you will find lots of examples and trouble shooting examples. How to solve Exec binding problems should be useful as well.

However, I’d recommend, if you can, setting up a Python program that you can send commands to using MQTT or HTTP. This will be, in my experience, a cleaner way for OH to interact with the scripts. You don’t even need to write the program, you can use mine :wink: . This also works well for those running OH in a more constrained environment like Docker containers.

This is through sensorReporter (see link above) instead of executeCommandLine or Exec binding (I run in docker so don’t have access to the libraries or other stuff I’d need).

  • At 1 AM send the bedroom Roku to the home screen. The Roku binding is in limbo so I have a script that issues the command to the Roku. This used to be much more involved as I would use the Roku’s built in broadcast to discover the IP address of the Rokus and keep track of it which I couldn’t do from my OH in the container. But I’ve since started using statically assigned IP addresses I can move this to the HTTP binding. I’m just lazy.

  • Again, because of a limitation in the Docker container, namely the lack of ping and arp, I implemented iPhone Presence Detection with hping3 and ARP in a script which, using sensorReporter, polls for the presence of our phones on my network even when they are asleep.

  • When I used Dash buttons, I implemented Dash button press detection in sensorReporter. This was before there was a Dash binding.

  • All of my GPIO stuff is done through either NodeMCUs running ESP Easy or on an RPi using sensorReporter. For the RPi stuff I have my garage doors controller and I’ve it wired to the sensors the previous owner’s security service left in place. The took everything else but the sensors were left.

Here is an example that will flash the lights in morse code. :smiley:

It is true that openHAB is not a real-time system, and is never going to be very good at directly working something like chaser lights or motor speed controls.

Along with your other suggestions in this thread, I intend to check out the JSR223 Rules stuff soon. Thanks also for python script, great!

But… the limit on commands to the Hue api is 20 per second, and it is a suggested limit. The developer docs simply state that above that rate you should test first. How fast is 20 commands per second? I can blink the lights like a strobe light. Turn them blue and it is like a police car is in the living room. This is using the HTTP (?, using GET and POST) api bridge that is built into every Hue hub and available to everyone who owns a Hue hub.

This is the reason I wanted to start this discussion.

The Lifx library I linked to also can blink the lights at any color, for any duration at any rate using one single command. Doing so from OpenHAB has thus far been unsuccessful but give me time