Rule for Blinds

I’m a little rusty on my OpenHAB rules configs, as I haven’t touched my very basic setup in about a year.

I’ve just setup a mysensors arduino servo controller and I’m using the arduino as a serial gateway for OpenHAB. I think I have everything setup correctly, however my remote arduino does not seem to be receiving the commands. I believe my controller is not sending them to the gateway.

Here is the portion of my current rule file that I’m working with:

rule "Test Blinds"
when
Item Blinds01 changed
then
var String value
if (Blinds01.state != value) {
sendCommand(Arduino, “105;10;1;1;3;” + value + “\n”)
println("Updated value to " + value +)
}
end

This is my first “slider” item, and I’m not sure I’m implementing it correctly in the rules. The events.log file show multiple entries that say:

Blinds01 received command xx

where xx is the value the slider is on. I think I’m close.

A state is a state and not a value :slightly_smiling:
So you have to use: if (Blinds01.state as DecimalType != value)

Sorry… missed a point. If you define value as a String, it should be if (Blinds01.state.toString != value)
but I don’t understand, why you compare a state, which is between 0 and 100 with an uninitialized var…

So I think, what you really want is

rule "Test Blinds"
when
    Item Blinds01 changed
then
    sendCommand(Arduino, "105;10;1;1;3;" + Blinds01.state.toString + "\n")
    println("Updated value to " + Blinds01.state.toString + "\n")
end

That makes sense, but is there a reference for this? I couldn’t find
anything.

Should I even use “state” or is there a preferred way to do this besides
the way you suggested?

It depends on what you want to do :slightly_smiling:
If you want to refer to the state, you have to use theItem.state.
If the Item is a switch, the state will be ON, OFF or uninitialized. (remember: without quotations)
If the Item is a rollershutter, the state will be UP, DOWN, STOP, uninitialized or a Number [0 - 100].
If you want to refer to the number, you have to cast it to BigDecimal. If you want to use the state as a string, you have to convert it to a string. There is a method for this, so .toString is the correct way to do this.

In fact, there are many examples at https://github.com/openhab/openhab or even here, plus reference guides for XTend and XText. I didn’t read this, but sometimes it comes across when I’m searching for the correct expression of something :wink: and yes, it drove me mad the first time I tried to write rules…

Ok, I’ve made a good bit of progress, maybe both backwards and forwards.

I can control my servo remotely using my gateway and openhab. My code is very basic, but it is helping me to understand and learn java, which I’m clearly not very experienced in.

// Servo
rule Servo
when
        Item Servo received command
then

        sendCommand(Arduino, "105;10;1;1;3;" + Servo.state.toString + "\n")
        println("Updated value to " + Servo.state.toString +)

end

On the remote arduino side, it is looking for a value of 0 - 180 (range of the servo) to be sent by OpenHAB. OpenHAB only seems to work in 0-100, so a conversion is in order. I think a basic conversion of ‘Servo.state’ * 1.8 should get me close. I can’t seem to figure out how to accomplish this.

Am I missing something simple? Would this be better to implement on the remote side arduino code?

Hi, did you manage to resolve this issue? i’m about to embark on automating some blinds…

I haven’t been able to dedicate any time to this, but if you get somewhere and can post your results here, I’m sure me and others would appreciate it.