Hallo,
ich experimentiere gerade mir meinem WLAN Grillthermometer.
Dieses sendet per MQTT einen Status im JSON Format.
Hier ein Beispiel:
{"system":{"time":"1555763739","unit":"C","soc":87,"charge":false,"rssi":-76,"online":2},"channel":[{"number":1,"name":"Smoker","typ":0,"temp":33.70,"min":95.00,"max":150.00,"alarm":0,"color":"#FFC100"},{"number":2,"name":"Fleisch Un","typ":0,"temp":999.00,"min":10.00,"max":94.00,"alarm":1,"color":"#22B14C"},{"number":3,"name":"Fleisch Ob","typ":10,"temp":999.00,"min":10.00,"max":94.00,"alarm":1,"color":"#EF562D"},{"number":4,"name":"Kanal 4","typ":0,"temp":999.00,"min":10.00,"max":35.00,"alarm":0,"color":"#FFC100"},{"number":5,"name":"Kanal 5","typ":0,"temp":999.00,"min":10.00,"max":35.00,"alarm":0,"color":"#A349A4"},{"number":6,"name":"Kanal 6","typ":0,"temp":999.00,"min":10.00,"max":35.00,"alarm":0,"color":"#804000"},{"number":7,"name":"Kanal 7","typ":0,"temp":999.00,"min":10.00,"max":35.00,"alarm":0,"color":"#5587A2"},{"number":8,"name":"Kanal 8","typ":0,"temp":999.00,"min":10.00,"max":35.00,"alarm":0,"color":"#5C7148"}],"pitmaster":{"type":["off","manual","auto"],"pm":[{"id":0,"channel":1,"pid":2,"value":100,"set":110.00,"typ":"auto","set_color":"#ff0000","value_color":"#000000"}]}}
Ich würde nun gerne daraus ein entsprechendes Objekt im ioBroker erzeugen.
Dazu habe ich nun mittels javascript angefangen aus dem JSON ein Objekt zu erzeugen.
Das klappt prima mittels JSON.parse recht elegant.
// path to json data
varJSON = 'mqtt.0.WLanThermo.Nanotherm.status.data';
// get json data
var rawJSON = getState(varJSON).val;
// JSON to object
obj = JSON.parse(rawJSON);
// Test output
console.log(obj.system.time);
console.log(obj.system.unit);
console.log(obj.system.soc);
console.log(obj.system.charge);
console.log(obj.system.rssi);
console.log(obj.system.online);
Wie erstelle ich denn jetzt am einfachsten aus dem bestehenden javascript Objekt ein Objekt im ioBroker?
Muss ich das für jedes Element einzeln machen, oder gibt es sogar eine Möglichkeit das mit einem Befehl zu machen?
Zu Fuß habe ich nun so angefangen:
id = 'javascript.0.nanotherm.system.time'
createState(id, false,{
name: 'System time',
type: 'number',
});
id = 'javascript.0.nanotherm.nystem.unit'
createState(id, false,{
name: 'System unit',
type: 'string',
});
Das wäre ok, da ich aber noch recht neu bin in ioBroker und javascript wollte ich lieber mal fragen ob das so der richtige Weg ist.