General best practices for maintainable rule and item arrangements

Got a question …
I have a lambda function defined in a rule file that I also want to use into another rule file. How to ?

Cannot be done. Rules can only access lambdas that are in the same file.

You might look into Scripts and see if what you want to do can be done within the limitation of Scripts.

Another alternative is to use a virtual switch (a switch without a binding) to trigger a rule and put your lambda in that rule.

Thanks for the reply.
Biggest issue with these alternatives are passing my lambda function parameters.
Callscript does not accept any parameter passing but I could use executeCmdLine to call a python script and pass along my five parameters … not exactly kind of integration I had in mind…
Using a virtual item switch to trigger my function would also need some “virtual” items to pass parameters …
Maybe the simplest solution will be to merge my two rules files !
Thanks anyway. Now I know the limitations and will make my way over it!.

This is one of the main (but not only) reason I strongly recommend organizing rules files by function (e.g. lighting) instead of other organization schemes such as location (e.g. room) or binding or whatever you can think of. The chances that you would want to share a global val/var (which a lambda is only one type) between files drops to near zero when organized in this way. At least in my experience.

Actually my lambda function is used to play MP3’s thru UpNp to a DLNA media renderer. It is quite generic usage and used wherever I want to alert someone in my house when any specific event occur. I agree with organizing rules by function and that is what I have mainly done.
Regards

I have implemented something like this (in my case Pushbullet alerts) using a separate rule and String Items.

In my other rules files I NotifiyAlert.sendCommand(“my message”) or NotifyInfo.sendCommand(“my message”) and in my rule I do some time of day checking and other processing to figure out where to send the alert and how to deliver it (e.g. only send Alarms to me at night via Pushbullet, send alerts to both me and my wife during the day via my.openhab, only send infos to me by Pushbullet during the day, otherwise save them up and send an email with all the infos that happened over night in the morning.

You could easily do the same, just encode the String you send in the command to easily parse out the info assuming there is more than one piece of info you need to pass.

This approach really made implementing and changing how I do alerts really easy to experiment with and maintain, even more so than a lambda which is what I started out trying to implement.

Very interesting and elegant solution. I will experiment this for sure.
Btw, is a Pushbullet binding available ?

There was an unofficial binding posted in this forum but I can’t figure out whether there was ever a pull request to add it to the baseline or whether the request was rejected. The link to the binding is now dead and the original author is MIA.

If Pushbullet is something you really want, mqttWarn is a good solution to bridge between MQTT and pushbullet. I was using that before I started using this unofficial binding and will probably go back to that since the binding seems to be going nowhere.

But, because of how I set up my rules as described above, swapping between the two will be very little work.

Just to let you know that I did implement a rule following your advice and it is all OK like that ! Nice solution.
Thanks again for sharing your knowledge.
I will check to bridge Mosquito with PushBullet using mqttWarn.

1 Like

Hey Rich,

I Need your help on below topic.

Actually I am quite new to Openhab…
I want to control my home appliances using openhab and arduino.
All I want to control is Lights and dimmers and also want to include some sensors like humidity and gas sensor.
1)I want to know how to send a massage to my arduino? I mean which communication would be better TCP or HTTP?
2)Do I need to write any rule to simply switch the light on and off??If yes can you help me the rule??
3)And how to work with Slider dimmer?

Thanks in advance for your reply…

MQTT might be worth a look Nishit. There’s an arduino library for it and openhab support too.

No need for rules, you can setup mqtt to control items via the bindings, this might not be 100% but somthing like:

Switch myLight “Light” {mqtt="<[mybroker:myHome/myLight:command:default]"}

For a dimmer you’ll need to publish a number (percent power) rather than an ON/OFF command and change your item to a Dimmer.

Google around and you can find similar examples e.g.


  1. There is an infinite range of approaches that you could take. The three most commonly used, based on the number of questions I see on this forum are:
  • MQTT: set up a message broker (Mosquitto is a common one) and both OH and Arduinos exchange messages through this broker
  • HTTP: set up your Arduinos to respond to HTTP REST requests and/or to make HTTP REST requests to OH’s REST API
  • MySensors: This one is a TCP/IP based binding. A lot of newcomers have trouble with this approach though so proceed with caution.

No, you should be able to handle it in your Item definition, no rule required. HOWEVER, if you are worried about writing a simple rule I highly recommend considering using a more off-the-shelf technology rather than DIY Arduino boards. I have seen too many non-technical people try to cobble together similar systems who are unable or unwilling to spend the time to learn the basics of programming and electronics.

Home automation is hard. DIY Arduino based home automation is even harder.

If you run into trouble please post specific questions to the forum. However, do not expect the forum members to write your rule for you. I do so frequently, but usually only after the requestor has shown a willingness to try to solve the problem themselves, or if the problem is particularly interesting. Furthermore, realize that debugging problems with these DIY Arduino based systems is exceptionally difficult to impossible to do without the devices being right in front of us. You will be on the hook to solve many of your own problems.

All that being said, the DIY Arduino approach can be a challenging but rewarding HA project. I’m not trying to dissuade you from pursuing it. I just want you to know what you are getting into.

There is a Dimmer Item type.

I highly recommend you spend some time on the wiki and experimenting with the Demo.

Thanks Rich and Toby,

Thanks for the reply guys,

I have completed my OH side and I took a look at demo items and sitemaps and rules, and understood how they works,
Now I would like to work on MQTT,
If there would be any doubt, I will post it on the community.

Hello guys!!

As per rick’s suggestion I worked on making an arduino to act as a http server.

For an example when I enter a link 192.168.1.101/on a pin related to this should go low.

it works well when I enter this link in my browser’s address bar. But when I tried it with http binding of open hab it is not working.

Here is my item configs:

/* Lights */
Dimmer Light_Dimmer_LR “Dimmer 1” (LR, Lights)
Switch Light_1_LR “Light 1” (LR, Lights) {http=">[ON:POST:http://192.168.1.101/on]>[OFF:POST:http://192.168.1.101/off]"}
Switch Light_2_LR “Light 2” (LR, Lights)
Switch Light_3_LR “Fall Lights” (LR, Lights)
Switch Light_4_LR “FAN” (LR, Lights)

I have bind only 1 item yet to check but it is not working.

Kindly suggest me the changes I need to make.

Use GET, not POST.

When you go to an address in a browser you are doing a GET.

I tried it using GET instead of POST but nothing is happening.
Kindly note that my server is using port no 80 and open hab is also using 8080 port.
can it create any problem?
My configuration setting are
#timeout in milliseconds for the http requests (optional, defaults to 5000)
#http:timeout=20000

#the interval in milliseconds when to find new refresh candidates
#(optional, defaults to 1000)
#http:granularity=5000

#configuration of the first cache item
#http:.url=
#http:.updateInterval=

#configuration of the second cache item
#http:.url=
#http:.updateInterval=

Am I I making any mistake?

Port 80 and port 8080 are distinct ports so there should be no problem. It is only a problem if they are the same number on the same machine. If that is the case one of them will not start because of the port conflict.

Please use code formatting when posting code. I can’t tell what you posted really looks like because this forum converts some code characters into formatting commands.

Indent each line by four spaces, highlight the text and press the </> button above, or put three ` on the line before and three on the line after your code.

It is working. As you said the ports 80 and 8080 where used by the same machine.

how am I suppose to send the dimmer value in the http binding?

I have seen certain examples of rules to get percentage values but how to bind it with http binding??

rule "Living Room Dimmer"
when
Item Light_LivRoom_AllLights_Dim received command
then
var Number bVal = 0
if(Light_LivRoom_AllLights_Dim.state instanceof PercentType) bVal = Light_LivRoom_AllLights_Dim.state as PercentType

	if(receivedCommand==INCREASE) bVal = bVal + 10
	if(receivedCommand==DECREASE) bVal = bVal - 10

	if(bVal<0)   bVal = 0
	if(bVal>100) bVal = 100
	
	sendCommand(Light_LivRoom_Steps_Dim, bVal)
	sendCommand(Light_LivRoom_Candles_Dim, bVal)
	
	if(bVal>0) postUpdate(Light_LivRoom_AllLights_Sw, ON)
	if(bVal==0) postUpdate(Light_LivRoom_AllLights_Sw, OFF)

end

See the HTTP Binding wiki page. Or if you just want to call URLs from your rules see the HTTP actions.

Hey Rich,

I just want to send an increase or decrease command whenever the slider is moved in respective direction, can you suggest me how should I do it, because I want to do the rest of the coding in my controller.

  1. Is it necessary to add a switch with dimmer item?