- Platform information:
- Hardware: i7/250Gb SSD/16Gb RAM
- OS: Ubuntu 19.10
- Java Runtime Environment: OpenJDK Version 1.8.0_252
- openHAB version: 2.5.7-1
- Issue of the topic: JS Transformation in rule does not appear to run
default.items:
String ZM_Camera2 "Camera-2 Front House [%s]" { channel="mqtt:topic:mc:zoneminder:Cam2"}
mqtt.things:
Thing topic zoneminder "Zoneminder" @ "MQTT" {
Channels:
Type string : Cam2 "Camera-2" [ stateTopic="zoneminder/2"]
} // end of thing
zoneminder.rules:
rule "zm_Front of house alarmed"
when
Item ZM_Camera2 received update
then
if (ZM_Camera2.state == NULL)
{
logInfo("zoneminder.rules", "Camera2 Item is null, cancelling...")
return;
}
var payload = ZM_Camera2.state.toString
logInfo("zoneminder.rules", "Incoming payload: " + payload)
var eventtype = transform("JSONPATH", "$.eventtype", payload)
logInfo("zoneminder.rules", "Event Type: " + eventtype)
var eventid = transform("JSONPATH","$.eventid", payload)
if (eventtype=="event_start"){
// Check that this is triggered by the FrontGatePerson zone
var zone = transform("JS", "esmotiontrigger.js", payload)
logInfo("zoneminder.rules", "Zone triggered: " + zone)
// If this is the FrontGatePerson zone //
if (zone=="FrontGatePerson")
{
// Lets check if a person was the trigger of this zone
var wasperson = transform("JS", "esnotificationperson.js", payload)
if (wasperson==true)
{
logInfo("zoneminder.rules", "Person has been detected in the FrontGatePerson zone. Event ID:" + eventid)
}
}
// Add other zones here
if (zone=="Driveway")
{
// Lets check if a person was the trigger of this zone
var wasperson = transform("JS", "esnotificationperson.js", payload)
if (wasperson==true)
{
logInfo("zoneminder.rules", "Person has been detected in the Driveway zone. Event ID:" + eventid)
}
// Lets check if a car was the trigger of this zone
var wascar = transform("JS", "esnotificationcar.js", payload)
if (wascar==true)
{
logInfo("zoneminder.rules", "Car has been detected in the Driveway zone. Event ID:" + eventid)
}
}
} // end of event_Start eventtype
end
esmotiontrigger.js:
(function(i) {
var data = JSON.parse(i);
var result = "";
var namesummary = data.name;
const regex = /(?<=Motion\s).\w+/gm;
result = regex.exec(namesummary);
return result[0];
})(input)
Sample of JSON that comes in to item ZM_Camera2:
{"hookvalue":"0","monitor":"2","detection":[{"confidence":"97.32%","type":"object","box":[315,288,595,674],"label":"car"},{"label":"person","confidence":"54.27%","box":[320,608,442,674],"type":"object"}],"eventtype":"event_start","name":"Front_House:(3482) [a] detected:person:54% Motion Driveway","state":"alarm","eventid":"3482"}
Logged data
2020-09-02 17:07:12.748 [INFO ] [rthome.model.script.zoneminder.rules] - Incoming payload: {"hookvalue":"0","monitor":"2","detection":[{"confidence":"97.32%","type":"object","box":[315,288,595,674],"label":"car"},{"label":"person","confidence":"54.27%","box":[320,608,442,674],"type":"object"}],"eventtype":"event_start","name":"Front_House:(3482) [a] detected:person:54% Motion Driveway","state":"alarm","eventid":"3482"}
2020-09-02 17:07:12.749 [INFO ] [rthome.model.script.zoneminder.rules] - Event Type: event_start
2020-09-02 17:07:12.752 [INFO ] [rthome.model.script.zoneminder.rules] - Zone triggered: {"hookvalue":"0","monitor":"2","detection":[{"confidence":"97.32%","type":"object","box":[315,288,595,674],"label":"car"},{"label":"person","confidence":"54.27%","box":[320,608,442,674],"type":"object"}],"eventtype":"event_start","name":"Front_House:(3482) [a] detected:person:54% Motion Driveway","state":"alarm","eventid":"3482"}
Problem
Testing the esmotiontrigger.js code seems to result in the correct value (in this case: “Driveway”) to be returned from the function.
When it is in the rule as shown this should result in the variable “zone” in the rules file to have the value “Driveway”, however when it is run, it simply results in the payload being reported back, as if it does not process the JS at all. Consequently the rest of the rule fails.
I have modified esmotiontrigger.js to return “Test” and this does not change the variable “zone” when logging this - which seems to indicate to me that it is not executing the transform.
In PaperUI, under Add-Ons -> Transformations I have enabled "Javascript Transformation - transformation-javascript-2.5.8
I am unsure why this is not executing the transform. Have I called it incorrectly in the rule to be able to have it process the transformation?
I know I can also do some stuff with JSONPATH, however I would like to be able to expand on this script in future to detect and report on multiple events, which is easier in JS to write and return value to OpenHAB.