NEWS
Text2command und javascript
-
Hallo iobroker-Gemeinde,
ich habe ein Problem mit dem "External rules with javascript" vom github: https://github.com/ioBroker/ioBroker.text2command
Wenn ich das Script einbaue und in dem Object javascript.0.textProcessor einen Text eingebe, z.B. Mist, dann erhalte ich im Log eine Fehlermeldung.
javascript.0 2017-04-17 18:33:40.920 error at Object.parse (native)
javascript.0 2017-04-17 18:33:40.920 error SyntaxError: Unexpected token M
javascript.0 2017-04-17 18:33:40.912 error message javascript.0.textProcessor [object Object] Unexpected token M
createState("textProcessor", '', function () { // text2command writes the value with ack=false. Change "any" is important too, to process repeated commands. on({id: "javascript.0.textProcessor", ack: false, change: 'any'}, function (obj) { var task = JSON.parse(obj.state.val); // log (obj.state.val); if (task.command === "Test") { log ("Keywort gefunden","info"); setState("javascript.0.textProcessor", 'Testnachricht empfangen.', true); } else { // let it process with predefined rules log ("Keywort nicht gefunden","info"); setState("javascript.0.textProcessor", '', true); } }); });
Mir ist nicht klar, warum dieser Fehler entsteht. Ersetze ich task.command durch obj.state.val scheint es zu funktionieren, aber viele andere scheinen das Original zu nutzen.
-
Diese Variable ist nicht für manuelle Bedienung.
text2Command schriebt ein JSON Objekt rein. Du einfach Text.
Modifiziere die Zeile:
var task = JSON.parse(obj.state.val);
auf
var task = obj.state.val[0] === '{' ? JSON.parse(obj.state.val) : {command: obj.state.val};
-
Hallo Bluefox,
vielen Dank für die schnelle Antwort. Hat prima funktioniert. Und mit deiner Erklärung kann ich auch zukünftig etwas anfangen.
Problem gelöst.