Last active
March 9, 2018 14:58
-
-
Save digitmind-net/d61a89ef1b92ce9273bc0887b2181c04 to your computer and use it in GitHub Desktop.
Aktuelllen Stromverbrauch mithilfe von Volkszähler in ioBroker speichern: https://digitmind.net/aktuelllen-stromverbrauch-mithilfe-von-volkszaehler-in-iobroker-speichern/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var url = "http://192.168.0.xxx/middleware.php/data.json?from=now&uuid[]=KANAL1_UUID&uuid[]=KANAL2_UUID"; | |
var pfad = "Volkszaehler."; | |
var idAKTUELLER_VERBRAUCH = pfad + "AKTUELLER_VERBRAUCH"; | |
var idAKTUELLE_ERZEUGUNG = pfad + "AKTUELLE_ERZEUGUNG"; | |
// Vorgegebene Funktion, die nur dazu dient, das JSON einzulesen | |
// Angelehnt an: http://www.iobroker.net/docu/?page_id=3691 | |
var request = require("request"); | |
function readJson(url, callback) { | |
request(url, function(err, state, body) { | |
if (body) { | |
var json = {}; | |
if (body !== "") { | |
try { | |
json = JSON.parse(body); | |
} catch (ex) { | |
json = {}; | |
} | |
if (!json) { | |
json = {}; | |
} | |
} | |
callback(null, json); | |
} else { | |
var error = "(" + err + ") ERROR bei Abfrage von: " + url; | |
log(error, "warn"); | |
callback(error, null); | |
} | |
}); | |
} | |
// Erzeugen der Objekte in ioBroker | |
createState(idAKTUELLER_VERBRAUCH, 0, { | |
name: 'Aktueller Verbrauch', | |
desc: 'Aktueller Verbrauch', | |
type: 'number', | |
role: 'value', | |
unit: 'W' | |
}); | |
createState(idAKTUELLE_ERZEUGUNG, 0, { | |
name: 'Aktuelle Erzeugung', | |
desc: 'Aktuelle Erzeugung', | |
type: 'number', | |
role: 'value', | |
unit: 'W' | |
}); | |
// Startet das Skript alle 2 Sekunden neu | |
schedule("*/2 * * * * *", function() { | |
readJson(url, function(err, json) { | |
if (!err) { | |
var wert1 = json.data[0].tuples[0][1]; | |
var wert2 = json.data[1].tuples[0][1]; | |
// Setzen der Werte in ioBroker | |
// Man füllt die oben erzeugten Objekte jetzt mit den Werten | |
setState(idAKTUELLER_VERBRAUCH, wert1); | |
setState(idAKTUELLE_ERZEUGUNG, wert2); | |
} else { | |
log("Fehler beim Auslesen des JSON fuer Volkszaehler. Keine Daten erhalten.", "warn"); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment