rule "Incoming Transformation"
when
Member of gZigbeeLightString received update
then
val trigIn = triggeringItem
val String zigbeestate = transform("JSONPATH", "$.state", trigIn.state.toString)
val Number zigbeebright = Float::parseFloat(transform("JSONPATH", "$.brightness", trigIn.state.toString))
val dimcontrol= gZigbeeLightControl.members.findFirst[ t | t.name == trigIn.name.toString.split('_').get(0) ] as DimmerItem
logInfo("User.Testing","Incoming transformation. State = " + zigbeestate + " Brightness = " + zigbeebright + "Dimmer control = " + dimcontrol.name)
if (zigbeestate == "OFF") {
dimcontrol.postUpdate(0)
} else {
var Number brightIn = Math::round(zigbeebright.floatValue() / 255 * 100 )
dimcontrol.postUpdate(brightIn)
logInfo("User.Testing","Incoming brightness = " + brightIn)
}
end
I know i can do the following in Javascript but I donât know how to do it in a rule.
if(json.hasOwnProperty('click')){
result = json.click;
} else {
result = "none";
}
So am I to guess you are using the MQTT binding to receive these JSON strings from zibgee2mqtt?
I ask because the MQTT 2 binding supports chaining transformations so you can put a REGEX transformation to look for the âclickâ property and it will only get passed to the JSONPATH transform if itâs there.
If the key doesnât exist in the JSON string, transform returns the original string that was passed to it. Itâs counter-intuitive, but thatâs the way it works. SoâŠ
val String s = transform("JSONPATH", "$.brightness", trigIn.state.toString)
if (s.equals(trigIn.state.toString)) {
// JSON string doesn't contain brightness key
} else {
// JSON string contains brightness key
}
I didnât think so, but I honestly donât remember. Iâll check.
Float::parseFloat will throw an exception (NumberFormatException, I think) if the argument is non-numeric. So, when transform returns the full JSON string (because $.brightness doesnât exist), it will throw the exception.