General best practices for maintainable rule and item arrangements

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?

I don’t use Dimmers so I don’t really know if the following has any chance of working.

I think you will need to create a proxy Item (i.e. an Item that is not bound to anything) that is a Dimmer. Put this Dimmer on your sitemap using a Slider. Create a rule when this Dimmer receives a command and based on the command received send the message to your controller.

You may be able to do this in the Item itself with a Mapping or something but I don’t have any experience with Dimmers so can’t say for sure.

A Dimmer is also a Switch so you can send it ON and OFF and it will go to 100 and 0 respectively. You can put it on your sitemap as a Switch. You can also add “switchSupport” to the end of your entry on the sitemap and a single click on the up side will send ON and a single click on the down side will send an OFF.

Thank you for the suggestion Rich,

It is working by using the demo rule for slider, I am receiving the percentage values.

Hi ,

Anybody have rule for checking state of battery operated devices…?
i want to know state of battery operated devices like fibaro motion sensors… if battery is removed.

Thanks in advance

If they are zwave and they implement the BATTERY command class, then you just set up an Item on that command:

Number N_V_COSmoke_Main_Battery   "Main Floor Smoke/CO Alarm Battery [%d]" <temperature> (gCOSmoke_Battery, gBattery) {zwave="5:command=battery"}

Not all battery powered zwave devices support the BATTERY command class (e.g Minimotes).

You should be able to tell in Habmin what command classes the device supports I think.

For other technologies I’ll be of no help.

[quote=“rlkoshak, post:12, topic:1316”]Item gLights received update

Unfortunately this causes three triggers…
[/quote]

Hi Rich,

have you ever found a solution for the trigger being raised multiple times?

Is there a way to find out what raised the trigger:
e.g. on

Item xyz received command

if (receivedCommand==ON) {…}

is possible.

Is there anything alike for

Item gLights received update
if …?