Twinkly christmas lights integration

Interesting implementation, and it’s fascinating to see it fit entirely within openhab rules.

For what it’s worth, for any others deciding on which path to use, the Python script does not invalidate the twinkly app (at least for me).
I also just stumbled across this fascinating read:

https://labs.f-secure.com/blog/twinkly-twinkly-little-star/

Lots to go on here.

Hi Ben,

thank you very much for your detailed explanation.
You are right, it needs bit more time by using an sonoffS20, but its just for 4 weeks in that use;-)
but you are right, its much comfortable to communicate with twinkly directly.

My OH runs on a Synology, but no big problem to fit your explanation to this circumstances.

With this, it runs. On/off… next step will be to implement the brigthness.

Regards and have a relaxing xmas time,
Daffy

“just for 4 weeks”

Do you mind if I use this rationale with my family? :laughing:
I can’t claim complete innocence, though, as I may have been involved in making some decorations more extravagant than they needed-to, per say.

All the best
Ben

Hi there!

Your implementation seems like an easy way to get some kind of ON / OFF functionality working without the hassle to get into python.

I tried to use the code pretty much as is, just replacing the IP to the twinkly and i seem to get a token. So far so good. But… It throws an error in the console and it’s not really working.

[ERROR] [untime.internal.engine.RuleEngineImpl] - Rule ‘Turn off christmas lights’: An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.HTTP.sendHttpPostRequest(java.lang.String,java.lang.String,java.lang.String,int) on instance: null

Any ideas here?

Best regards,
Thed

Ah yes, forgot about that!

The sendHttpPostRequest method which includes the headers is available from OH 3.0
Since I am running 2.5 is used a work around.
You need to put the following jar in your addons folder:
https://github.com/jimtng/binaries/blob/master/org.openhab.core.model.script-2.5.0.jar

Thanks to @JimT in this topic: https://community.openhab.org/t/solved-how-to-use-sendhttppostrequest-with-http-headers/104396/4

1 Like

Thank’s a bunch! I can confirm that works. Great! :+1:

1 Like

I copied the twinkly.py to /var/lib/openhab and /var/lib/openhab2 (I’m running openhab3 with openhabian).
Running the python command in the commandline works.
But from the rule I got an error:

2020-12-18 08:34:36.819 [WARN ] [rg.openhab.core.io.net.exec.ExecUtil] - Error occurred when executing commandLine '[python3 /var/lib/openhab/twinkly.py 192.168.10.118 on]'
java.io.IOException: Cannot run program "python3 /var/lib/openhab/twinkly.py 192.168.10.118 on": error=2, No such file or directory

I tried it with and without fullpath.

Which is the starting folder if I don’t use a full path?
And what is not foud? python or the script?

Have you confirmed the paths that openhab uses in your installation?
It sounds like a case for those more familliar with openhab3, unfortunately.

For anybody who used my rule above, my initial post incorrectly used the twinklyTree.state, which doesn’t reliably get updated in time for the rule operation. I have now updated it to use receivedCommand instead, and the rule seems to reliably function as intended now.

rule "Twinkly Christmas Tree Power State"
when 
  Item twinklyTree received command
then
  if (receivedCommand == ON) {
    executeCommandLine("python3 twinkly.py 192.168.0.209 on")  // turn the christmas tree on
  }
  else {
    executeCommandLine("python3 twinkly.py 192.168.0.209 off")  // turn the christmas tree off
  }
end

So, here’s brightness functionality, I use a slider just with the item below:

Item:
Number twinklyTree_Brigthness “Brightness Twinkly Christmas Tree”

Rule:
rule “Twinkly Christmas Tree Brightness”

when
Item twinklyTree_Brigthness changed
then
executeCommandLine("python3 twinkly.py 192.168.xxx.xxx brightness " + twinklyTree_Brigthness.state.toString)}

end

If twinkly is off, it switches automatically on, when changing brightness. but its only one-way. if you change it in the app, theres no state for openhab. And if you set 0 for brightness, it switches twinkly off.

Works good.

Greetings,
Daffy

1 Like

Try:

~$ which python3
/usr/bin/python3

Ha, Worked straight out of your ‘box’.
Thanks!
Still on 2.5.11, so the python route is fine.

Good to hear you can use it. That was my intention why I shared it, so people could use it!

I noticed when I wrote the rules, when I control Twinkly brightness from the app on my Android phone, it sends something like: 35.0, in stead of 35. And the Twinkly api doesn’t accept ‘.0’ at the end, and the brightness wouldn’t change. On my laptop from the Basic UI I don’t have this problem.

So a made a workaround in my rules, i’m not so good at python to fix this so i did it from within the Openhab rules :slight_smile: .


rule "Twinkly Brightness"
when
    Item Twinkly_brightness changed
then
    var Number numValue = (Twinkly_brightness.state as DecimalType).intValue
    executeCommandLine("python3 twinkly.py 192.168.0.55 brightness " + numValue)
end

My Tinkly_brightness item is a dimmer item, not sure if i done something wrong there.

1 Like

Hi All,

I have created a very basic native Twinkly binding. It currently only supports ON/OFF. It is my first ever binding so i have learned a lot while developing, and there is probably a lot more to improve.

However, it seems to work for me. The sources can be found on openhab-addons/bundles/org.openhab.binding.twinklytree at twinklytree · mvanhulsentop/openhab-addons · GitHub

It is branched from the 2.5 tree, because that’s the version i use. 3.0 branch suggests to me interface breaks, and my main objective was to make it work on my own setup. But now i’m having trouble performing a succesful complete build. It does build my own project so that’s good enough for me.

So if you’re interested, you can try to build yourself from source, or you can send me an e-mail. I would not mind publishing the jar but i don’t know where.

Kind regards,

Maarten

1 Like

thanks, Maarten.
It woul be great, if you could send me the jar to m.aschenborn@gmail.com

Michael

Just posting the OH3 flavour:

executeCommandLine("/usr/bin/python3", "/home/omr/twinkly.py", "192.168.1.57", "off");
2 Likes

Hi folks!

I’ve just bought me some pretty Twinkly lights :christmas_tree:
Naturally I’ve wanted to connect them to OH3 and found this thread here.
Is there a demand for an OH3 binding or any features?

@Maarten_v_Hulsentop I’ve forked your repo here, refactored it for OH3 and it works as intended - thanks for the great job! :smiley:
Are you still on it?

BR Pav

1 Like

@Pavion Would you be willing to send the .jar file out? or post it?

It’s in the Release section on GitHub, just look on the right side (above link).

1 Like

Twinkly Binding

This is the OH3 fork of @Maarten_v_Hulsentop binding.
As of now it can switch your lights on and off, current state can be obtained by sending REFRESH to the corresponding channel. No fancy stuff :slight_smile:
Fancy stuff might be added on request :face_with_monocle:

Feedback appreciated! :slight_smile: