Here you are assigning 100 to i. You want to use == to perform a simple equality test.
…paste your JavaScript into an online tester like this, and replace input with different values to see how it works. With fixing the equality test, it looks like it will work.
rule "Query Lights"
when
Time cron "0/30 * * * * ?"
then
Light_GF_Garage_CeilingQuery.sendCommand("I0400")
//Thread::sleep(500)
Light_GF_Door_WallQuery.sendCommand("I0100")
end
rule "Update Light_GF_Garage_Ceiling"
when
Item Light_GF_Garage_CeilingQuery received update
then
logDebug("myscripts", "Update garage : Light ID = "+Light_GF_Garage_CeilingQuery.state.toString.substring(4,6))
if(Light_GF_Garage_CeilingQuery.state.toString.substring(4,6)=="04"){
logDebug("myscripts", "Update garage : "+Light_GF_Garage_CeilingQuery.state.toString.substring(6,8))
switch Light_GF_Garage_CeilingQuery.state.toString.substring(6,8) {
case "00": Light_GF_Garage_Ceiling.postUpdate(OFF)
case "01": Light_GF_Garage_Ceiling.postUpdate(ON)
}
}
end
rule "Update Light_GF_Door_Wall"
when
Item Light_GF_Door_WallQuery received update
then
logDebug("myscripts", "Update door : Light ID = "+Light_GF_Door_WallQuery.state.toString.substring(4,6))
if(Light_GF_Door_WallQuery.state.toString.substring(4,6)=="01"){
switch Light_GF_Door_WallQuery.state.toString.substring(6,8) {
case "00": Light_GF_Door_Wall.postUpdate(OFF)
case "01": Light_GF_Door_Wall.postUpdate(ON)
}
}
end
It seems that when I send data over one of the String items, the result hits both of my Update rules.
So I added an extra check to make sure I’m not updating all my lights when just one is turned on.
Is this a good approach?
If this is the way it works, I would only need 1 String item to do the query in my opinion.
That makes sense. Have a rule for each Switch item be transformed and forwarded to the String item, and have a rule that triggers on received update, parse the answer and address the specific item. So a single String item does all the in’s and out’s to and from the TCP socket.
I finally had the time to finish me upgrade to openhab2 and finish my basic configuration.
This what I ended up with
The simplified version is this
rule "Query Relay"
when
Time cron "0/30 * * * * ?"
then
for(var i=0;i<16;i++)
{
query="command with i as one of the parameters"
Status_Query.sendCommand(query)
}
end
rule "Update Relay"
when
Item Status_Query received update
then
if(Status_Query!=null && Status_Query.state.toString.length>6){
var relayId=Status_Query.state.toString.substring(4,6)
var status=Status_Query.state.toString.substring(6,8)
var onOff=OFF
switch status{
case "00": onOff=OFF
case "01": onOff=ON
}
switch relayId{
case "00":Light_00.postUpdate(onOff)
case "01":Light_01.postUpdate(onOff)
case "02":Light_02.postUpdate(onOff)
case "03":Light_03.postUpdate(onOff)
..
}
}
end
I left out the command itself since I’m not allowed to post that by the manufacturer. It’s working just fine. The only thing is that there is some delay. And I think it could be because I could send a new command before I get a response on the previous one. (if that is possible with the TCP binding)
I could go for the dummy way and put in some short delay. But I would rather go for a kind of “wait untill response received” method. Some suggestions?
May I ask how exactly you manage the respons you get (I000XXYY01) when sending a command through UDP (for example S0400)?
Is it effectively by the rule “Update Light 4”? Or is that rule only to check wat the status is after sending the I0400 command?