SyntaxError: Unexpected token N in JSON at position 0
at JSON.parse ()
at Request._callback (/homebridge/node_modules/homebridge-sonoff-tasmota-http/index.js:34:29)
at Request.self.callback (/homebridge/node_modules/homebridge-sonoff-tasmota-http/node_modules/request/request.js:185:22)
at Request.emit (events.js:210:5)
at Request. (/homebridge/node_modules/homebridge-sonoff-tasmota-http/node_modules/request/request.js:1161:10)
at Request.emit (events.js:210:5)
at IncomingMessage. (/homebridge/node_modules/homebridge-sonoff-tasmota-http/node_modules/request/request.js:1083:12)
at Object.onceWrapper (events.js:299:28)
at IncomingMessage.emit (events.js:215:7)
at endReadableNT (_stream_readable.js:1183:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
I added a fix to wrap the JSON parse which fixes the issue:
SonoffTasmotaHTTPAccessory.prototype.getState = function(callback) {
var that = this
request("http://" + that.hostname + "/cm?user=admin&password=" + that.password + "&cmnd=Power" + that.relay, function(error, res
if (error) return callback(error);
that.log("Sonoff HTTP: " + that.hostname + "body: " + body);
try{
var sonoff_reply = JSON.parse(body); // {"POWER":"ON"}
that.log("Sonoff HTTP: " + that.hostname + ", Relay " + that.relay + ", Get State: " + JSON.stringify(sonoff_reply));
switch (sonoff_reply["POWER" + that.relay]) {
case "ON":
callback(null, 1);
break;
case "OFF":
callback(null, 0);
break;
}
}catch(e){
return callback(error);
}
})
}
I am getting the following error:
SyntaxError: Unexpected token N in JSON at position 0 at JSON.parse ()
at Request._callback (/homebridge/node_modules/homebridge-sonoff-tasmota-http/index.js:34:29)
at Request.self.callback (/homebridge/node_modules/homebridge-sonoff-tasmota-http/node_modules/request/request.js:185:22)
at Request.emit (events.js:210:5)
at Request. (/homebridge/node_modules/homebridge-sonoff-tasmota-http/node_modules/request/request.js:1161:10)
at Request.emit (events.js:210:5)
at IncomingMessage. (/homebridge/node_modules/homebridge-sonoff-tasmota-http/node_modules/request/request.js:1083:12)
at Object.onceWrapper (events.js:299:28)
at IncomingMessage.emit (events.js:215:7)
at endReadableNT (_stream_readable.js:1183:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
I added a fix to wrap the JSON parse which fixes the issue:
SonoffTasmotaHTTPAccessory.prototype.getState = function(callback) {
var that = this
request("http://" + that.hostname + "/cm?user=admin&password=" + that.password + "&cmnd=Power" + that.relay, function(error, res if (error) return callback(error);
that.log("Sonoff HTTP: " + that.hostname + "body: " + body);
try{
var sonoff_reply = JSON.parse(body); // {"POWER":"ON"}
that.log("Sonoff HTTP: " + that.hostname + ", Relay " + that.relay + ", Get State: " + JSON.stringify(sonoff_reply));
switch (sonoff_reply["POWER" + that.relay]) {
case "ON":
callback(null, 1);
break;
case "OFF":
callback(null, 0);
break;
}
}catch(e){
return callback(error);
}
})
}