Könntest du mir bitte helfen wo ich die Schleife einbauen muss?
Ich denke ich habe es gefunden. Aber wenn du noch mal drüber schauen würdest wär ich dankbar.
Hier der aktuelle Code:
//...Teil der Funktion
readJson(urlDevices, function(err,json) {
if(!err) {
myJson = json;
var arr = myJson.result.items;
for(let i = 0; i < arr.length; i++) {
if(arr[i].tagValues) {
let tags = arr[i].tagValues;
if(tags.PowerIn) {
log(tags.PowerIn.value);
setState(idDPowerIn ,tags.PowerIn.value);
}
if(tags.PowerSelfSupplied) {
log(tags.PowerSelfSupplied.value);
setState(idDPowerSelfSupplied ,tags.PowerSelfSupplied.value);
}
//...usw
//der alte Code
var urlValues = 'http://192.168.xxx.xxx/rest/kiwigrid/eps/powerValues';
var urlDevices = 'http://192.168.xxx.xxx/rest/kiwigrid/wizard/devices';
var path = "solarwatt.";
var request = require("request");
var myJson = {};
//Values
var idpowerACOut = path + "powerACOut";
var idpowerConsumed = path + "powerConsumed";
var idpowerProduced = path + "powerProduced";
var idpowerIn = path + "powerIn";
var idpowerOut = path + "powerOut";
createState(idpowerACOut, 0, { name: 'powerACOut aus', desc: 'xxxx', type: 'number', role: 'value'});
createState(idpowerConsumed, 0, { name: 'Gesamtverbrauch', desc: 'xxxx', type: 'number', role: 'value'});
createState(idpowerProduced, 0, { name: 'powerProduced', desc: 'xxxx', type: 'number', role: 'value'});
createState(idpowerIn, 0, { name: 'powerIn', desc: 'xxxx', type: 'number', role: 'value'});
createState(idpowerOut, 0, { name: 'powerOut', desc: 'xxxx', type: 'number', role: 'value'});
//Devices
var idDPowerIn = path + "PowerInD";
var idDPowerSelfSupplied = path + "PowerSelfSupplied";
var idDPowerConsumedFromStorage = path + "PowerConsumedFromStorage";
var idDStateOfCharge = path + "StateOfCharge";
var idDPowerConsumedFromGrid = path + "PowerConsumedFromGrid";
var idDPowerBuffered = path + "PowerBuffered";
var idDPowerConsumedFromProducers = path + "PowerConsumedFromProducers";
createState(idDPowerSelfSupplied, 0, { name: 'Eigenverbrauch', desc: 'xxxx', type: 'number', role: 'value' });
createState(idDPowerIn, 0, { name: 'Stromzukauf', desc: 'xxxx', type: 'number', role: 'value' });
createState(idDPowerConsumedFromStorage, 0, { name: 'Batterieversorgung', desc: 'xxxx', type: 'number', role: 'value' });
createState(idDStateOfCharge, 0, { name: 'Ladestand', desc: 'xxxx', type: 'number', role: 'value' });
createState(idDPowerConsumedFromGrid, 0, { name: 'Netzbezug', desc: 'xxxx', type: 'number', role: 'value' });
createState(idDPowerBuffered, 0, { name: 'Batterieaufladung', desc: 'xxxx', type: 'number', role: 'value' });
createState(idDPowerConsumedFromProducers, 0, { name: 'Eigenverbrauch', desc: 'xxxx', type: 'number', role: 'value' });
function parseJson(text) {
if (text === "") return {};
try { json = JSON.parse(text); }
catch (ex) { json = {};}
if(!json) json = {};
return json; }
function readJson(url, callback) {
request(url, function (err, state, body){
if (body) {
var json = parseJson(body);
callback(null, json);
} else { var error = "(" + err + ") ERROR bei Abfrage von: " + url; log(error, "warn"); callback(error, null);}
});}
function main() {
readJson(urlValues, function(err,json) {
if(!err) {
myJson = json;
log("powerACOut: " + myJson[0].powerACOut,"info");
log("powerConsumed: " + myJson[1].powerConsumed,"info");
log("powerProduced: " + myJson[1].powerProduced,"info");
log("powerIn: " + myJson[1].powerIn,"info");
log("powerOut: " + myJson[1].powerOut,"info");
setState(idpowerACOut ,parseInt(myJson[0].powerACOut));
setState(idpowerConsumed ,parseInt(myJson[1].powerConsumed));
setState(idpowerProduced ,parseInt(myJson[1].powerProduced));
setState(idpowerIn ,parseInt(myJson[1].powerIn));
setState(idpowerOut ,parseInt(myJson[1].powerOut));
} else {
log("Error reading JSON from solarwatt. No data!","warn");
myJson = {};
}
});
readJson(urlDevices, function(err,json) {
if(!err) {
myJson = json;
log("PowerIn: " + myJson.result.items[2].tagValues.PowerIn.value,"info");
setState(idDPowerIn ,myJson.result.items[2].tagValues.PowerIn.value);
log("PowerSelfSupplied: " + myJson.result.items[5].tagValues.PowerSelfSupplied.value,"info");
setState(idDPowerSelfSupplied ,myJson.result.items[5].tagValues.PowerSelfSupplied.value);
log("PowerConsumedFromStorage: " + myJson.result.items[5].tagValues.PowerConsumedFromStorage.value,"info");
setState(idDPowerConsumedFromStorage ,myJson.result.items[5].tagValues.PowerConsumedFromStorage.value);
log("StateOfCharge: " + myJson.result.items[10].tagValues.StateOfCharge.value,"info");
setState(idDStateOfCharge ,myJson.result.items[10].tagValues.StateOfCharge.value);
log("PowerConsumedFromGrid: " + myJson.result.items[5].tagValues.PowerConsumedFromGrid.value,"info");
setState(idDPowerConsumedFromGrid ,myJson.result.items[5].tagValues.PowerConsumedFromGrid.value);
log("PowerBuffered: " + myJson.result.items[5].tagValues.PowerBuffered.value,"info");
setState(idDPowerBuffered ,myJson.result.items[5].tagValues.PowerBuffered.value);
log("PowerConsumedFromProducers: " + myJson.result.items[5].tagValues.PowerConsumedFromProducers.value,"info");
setState(idDPowerConsumedFromProducers ,myJson.result.items[5].tagValues.PowerConsumedFromProducers.value);
} else {
log("Error reading JSON from solarwatt. No data!","warn");
myJson = {};
}
});
}