Hello,
i want to reduce the code of same kind of items and use one rule for that.
actually i using a powerstrip with 6 ports with one rule for a port
its possible to use one rule with variables for all ?
Here an example with 2 Ports:
rule "Steckdose1"
when
Item gPowerStrip1Port1 received command
then
switch(receivedCommand) {
case ON :{
var poweron = URLEncoder::encode('{"port":1,"state":1}','UTF-8');
sendHttpGetRequest("http://192.168.22.12/?cmd=200&json=" + poweron);
logInfo("Steckdose1", "eingeschaltet");
}
case OFF:
{
var poweroff = URLEncoder::encode('{"port":1,"state":0}','UTF-8');
sendHttpGetRequest("http://192.168.22.12/?cmd=200&json=" + poweroff);
logInfo("Steckdose1", "ausgeschaltet");
}
}
end
rule "Steckdose2"
when
Item gPowerStrip1Port2 received command
then
switch(receivedCommand) {
case ON :{
var poweron = URLEncoder::encode('{"port":2,"state":1}','UTF-8');
sendHttpGetRequest("http://192.168.22.12/?cmd=200&json=" + poweron);
logInfo("Steckdose2", "eingeschaltet");
}
case OFF:
{
var poweroff = URLEncoder::encode('{"port":2,"state":0}','UTF-8');
sendHttpGetRequest("http://192.168.22.12/?cmd=200&json=" + poweroff);
logInfo("Steckdose2", "ausgeschaltet");
}
}
end
the same for cron job:
rule "Request data from Powerstrip"
when
Time cron "0/10 * * * * ?"
then
var String json = sendHttpGetRequest("http://192.168.22.12/?cmd=511")
val String PowerStrip1Port1Status = transform("JSONPATH", "$.data.switch[0]", json)
val String PowerStrip1Port2Status = transform("JSONPATH", "$.data.switch[1]", json)
val DecimalType PowerStrip1Port1Watt = new DecimalType(transform("JSONPATH", "$.data.watt[0]", json))
val DecimalType PowerStrip1Port2Watt = new DecimalType(transform("JSONPATH", "$.data.watt[1]", json))
//logInfo("Powerstatus", PowerStrip1Port1Status );
switch(PowerStrip1Port1Status) {
case "1" : postUpdate(gPowerStrip1Port1, ON)
case "0" : postUpdate(gPowerStrip1Port1, OFF)
}
switch(PowerStrip1Port2Status) {
case "1" : postUpdate(gPowerStrip1Port2, ON)
case "0" : postUpdate(gPowerStrip1Port2, OFF)
}
postUpdate(gPowerStrip1Port1Watt,(PowerStrip1Port1Watt/1000))
postUpdate(gPowerStrip1Port2Watt,(PowerStrip1Port2Watt/1000))
end