Hey
I hope someone can help me with my rule…
I have a API call where I get a JSON response with X devices, I sort them with the json value “product_id”
f.e. product_id=1; product_id=6; product_id=4;
now i need a value for each product id starting with 1 and if there are more than 1 entires for this product_id 1 needs to increase to 2 and so on…in my code its the “var count_xxx” for each 3 product_ids
here is the code i have… but i cant find a way the count the right way.
'use strict';
load(Java.type("java.lang.System").getenv("OPENHAB_CONF")+'/automation/lib/javascript/core/rules.js');
var me = "001_surehub_start.js";
var json_devices_list = []
var json_hub_list = []
var json_feeder_list = []
var json_flap_list = []
JSRule({
name: "SureHub START",
description: "Line: "+__LINE__,
triggers: [
// its a big API call, so just lets call this only every 6 hours
TimerTrigger("0 0 0/6 1/1 * ? *")
],
execute: function( module, input){
var token = "TOKEN"
var timeout = 5000;
var httpmethod = "GET";
var url = "https://app.api.surehub.io/api/me/start"
var json = executeCommandLineAndWaitResponse("/etc/openhab2/scripts/surehub.sh " + httpmethod + " " + token + " " +url, timeout);
logDebug("results executeCommandLineAndWaitResponse: ", json);
if (json == "") return;
// only 1 Hub
var action = getAction("Transformation").static;
// count all devices
json_devices_list = JSON.parse(json).data.devices;
logInfo(me, "Surehub ALL Devices List: " + json_devices_list.length + " devices were found!");
// count hub devices
json_hub_list = JSON.parse(json).data.devices.filter(function(item){
return item.product_id==1;
});
logInfo(me, "Surehub HUB Devices List: " + json_hub_list.length + " Hub devices were found!");
// count flap devices
json_flap_list = JSON.parse(json).data.devices.filter(function(item){
return item.product_id==6;
});
logInfo(me, "Surehub FLAP Devices List: " + json_flap_list.length + " Flap devices were found!");
// count feeder devices
json_feeder_list = JSON.parse(json).data.devices.filter(function(item){
return item.product_id==4;
});
logInfo(me, "Surehub FEEDER Devices List: " + json_feeder_list.length + " feeder devices were found!");
for(var i = 0; i < json_devices_list.length; i++)
{
logInfo(me, "for each ALL Devices List: " + i + "!");
// parse the hub data (product_id = 1)
var hubData = "$.data.devices["+i+"][?(@.product_id==1)]";
var hubDataTf = action.transform("JSONPATH", hubData, json);
if (hubDataTf != NULL) {
var count_hub = json_devices_list.length - json_feeder_list.length - json_flap_list.length;
var count_hub2 = json_devices_list.length - i - json_feeder_list.length - json_flap_list.length;
logInfo(me, "TEST - " + hubData + " --- " + hubDataTf + " --- " + count_hub + " --- " + count_hub2);
}
// parse the flap data (product_id = 6)
var flapData = "$.data.devices["+i+"][?(@.product_id==6)]";
var flapDataTf = action.transform("JSONPATH", flapData, json);
if (flapDataTf != NULL) {
var count_flap = json_devices_list.length - json_hub_list.length - json_feeder_list.length;
var count_flap2 = json_devices_list.length - i - json_hub_list.length - json_feeder_list.length;
logInfo(me, "TEST - " + flapData + " --- " + flapDataTf + " --- " + count_flap + " --- " + count_flap2);
}
// parse the feeder data (product_id = 4)
var feederData = "$.data.devices["+i+"][?(@.product_id==4)]";
var feederDataTf = action.transform("JSONPATH", feederData, json);
if (feederDataTf != NULL) {
var count_feeder = json_devices_list.length - json_hub_list.length - json_flap_list.length;
var count_feeder2 = json_devices_list.length - i - json_hub_list.length - json_flap_list.length;
logInfo(me, "TEST - " + feederData + " --- " + feederDataTf + " --- " + count_feeder + " --- " + count_feeder2);
}
}
}
});
Thanks for your help