Read the statement by Michael Teeuw here.
MMM-Domoticz Cannot read property '0' of undefined
-
@sdetweil said in MMM-Domoticz Cannot read property '0' of undefined:
this is not set correctly if called in the domoRequest.onreadystatechange = function() {}
u should be using self… ‘this’ is context sensitive, means ‘inside this function’hi @sdetweil, i replace conole.log by Log.log and now i have error in the console
Could not load data.
refered to line 131.Sorry but i don’t unerstand your last post about this and self. Can you show me the good syntax or do you need the entire program?
thanks for your help
-
@MisterT change this line
Log.error(self.name + ": Could not load data.");
to this
Log.error(self.name + ": Could not load data. request status="+this.status);
this will show the error return code on the api url request for the data
-
hi @sdetweil , sorry for this late answer, i’m very busy at this moment
I applied your programe line and now, in the console i have:
:8080/modules/fabio/domoticz//domoticz.js:142 Uncaught TypeError: Cannot read property ‘0’ of undefined
at Class.processJson (:8080/modules/fabio/domoticz//domoticz.js:142)
at XMLHttpRequest.domoRequest.onreadystatechange (:8080/modules/fabio/domoticz//domoticz.js:128)and this:
domoticz.js:142 Uncaught TypeError: Cannot read property ‘0’ of undefined
at Class.processJson (domoticz.js:142)
at XMLHttpRequest.domoRequest.onreadystatechange (domoticz.js:128)
processJson @ domoticz.js:142
domoRequest.onreadystatechange @ domoticz.js:128
XMLHttpRequest.send (async)
updateDomo @ domoticz.js:136
(anonymous) @ domoticz.js:170and the js script below
/* global Module */ /* Magic Mirror * Module: Domoticz * * By Mathias Arvidsson */ Module.register("domoticz",{ defaults: { units: config.units, updateInterval: 50, animationSpeed: 0, timeFormat: config.timeFormat, lang: config.language, initialLoadDelay: 0, retryDelay: 2500, apiBase: "http://1xx.1xx.x.xx", // IP domoticz apiPort: "xxxx", // Port sensors: [ { idx: "1", symbolon: "fa fa-user", symboloff: "fa fa-user-o", hiddenon: false, hiddenoff: false, customTitle: "", }, ], }, firstEvent: false, getStyles: function() { return ['font-awesome.css']; }, // Define required scripts. getScripts: function() { return ["moment.js"]; }, // Define start sequence. start: function() { Log.info("Starting module: " + this.name); // Set locale. moment.locale(config.language); this.loaded = false; this.status1 = false; this.title = "Loading..."; this.scheduleUpdate(this.config.initialLoadDelay); this.sensors = []; for (var c in this.config.sensors) { var sensor = this.config.sensors[c]; var newSensor = {idx:sensor.idx, symbolon:sensor.symbolon, symboloff:sensor.symboloff, hiddenon:sensor.hiddenon, hiddenoff:sensor.hiddenoff, customTitle:sensor.customTitle, status:"", sname:"",type:""}; Log.log(sensor.idx); this.sensors.push(newSensor); } Log.log(this.sensors); }, // Override dom generator. getDom: function() { var wrapper = document.createElement("div"); if (!this.loaded) { wrapper.innerHTML = "Loading..."; wrapper.className = "dimmed light small"; return wrapper; } var tableWrap = document.createElement("table"); tableWrap.className = "small"; for (var c in this.sensors) { var sensor = this.sensors[c]; if((sensor.status=="On" && sensor.hiddenon)||(sensor.status=="Off" && sensor.hiddenoff)) continue; var sensorWrapper = document.createElement("tr"); sensorWrapper.className = "normal"; var symbolTD = document.createElement('td'); symbolTD.className = "symbol"; var symbol = document.createElement('i'); var symbolClass = sensor.symboloff if(sensor.status=="On") symbolClass = sensor.symbolon symbol.className = symbolClass; symbolTD.appendChild(symbol); sensorWrapper.appendChild(symbolTD); var titleTD = document.createElement('td'); titleTD.className = "title bright"; if(sensor.status=="Off") titleTD.className = "title light"; titleTD.innerHTML = sensor.sname; if(typeof sensor.customTitle !== 'undefined') titleTD.innerHTML = sensor.customTitle; sensorWrapper.appendChild(titleTD); var statusTD = document.createElement('td'); statusTD.className = "time light"; statusTD.innerHTML = sensor.status; sensorWrapper.appendChild(statusTD); tableWrap.appendChild(sensorWrapper); } wrapper.appendChild(tableWrap); return wrapper; }, updateDomo: function() { var i = 0; for (var c in this.sensors) { Log.log("this is c: " + c); var sensor = this.sensors[c]; var url = this.config.apiBase + ":" + this.config.apiPort + "/json.htm?type=devices&rid=" + sensor.idx; var self = this; var domoRequest = new XMLHttpRequest(); domoRequest.open("GET", url, true); domoRequest.onreadystatechange = function() { if (this.readyState === 4) { if (this.status === 200) { self.processJson(JSON.parse(this.response)); Log.log("Loaded data"); } else { Log.error(self.name + ": Could not load data. request status="+this.status); Log.log("Did not load data"); } } }; domoRequest.send(); i++; } }, processJson: function(data) { Log.log("****Parsing data: " + c + " " + data.result[0].Name); if (!data) { // Did not receive usable new data. // Maybe this needs a better check? return; } for (var c in this.sensors) { var sensor = this.sensors[c]; if(sensor.idx == data.result[0].idx){ this.sensors[c].sname = data.result[0].Name; this.sensors[c].status = data.result[0].Data; this.sensors[c].type = data.result[0].Type; } } this.loaded = true; this.updateDom(this.config.animationSpeed); }, scheduleUpdate: function(delay) { Log.log("Updating.."); var nextLoad = this.config.updateInterval; if (typeof delay !== "undefined" && delay >= 0) { nextLoad = delay; } var self = this; setInterval(function() { self.updateDomo(); }, 500 ); } });
for your information, the module seems to work but i have this error.
Have you a solution?
thanks a lot -
@MisterT could you email the module config (from config.js) to my email same userid as here on gmail…
-
@sdetweil there is also a newer module (2 months) same name from another user
https://github.com/SpoturDeal/MMM-Domoticz
this module (M-Arvidsson) hasn’t been updated in 3 yrs
-
@MisterT I have the same module domoticz (M.Arvidsson) and it works fine for me. I can seei n your domoticz.js that ievery row with log.log … , I have console.log. Maybe thats the problem.
-
@sdetweil i send you a personal mail with my domoticz module and config.js.
Thanks for your help -
@MisterT Hello again, I noticed that have changed the row “setInterval(function() {” to
“setTimeout(function() {” -
@MisterT sent response via email
-
Hi, I come back again because @sdetweil send me a MMM-Domoticz module. This module works with user and password of domoticz. So if you want see your domoticz devices on your MM you can use this code below.
Just copy it on text editor and save it as domoticz.js in your MM module folder and in a domoticz folder/* global Module */ /* Magic Mirror * Module: Domoticz * * By Mathias Arvidsson & editing by sdetewell */ Module.register("domoticz",{ defaults: { units: 0, updateInterval: 2000, animationSpeed: 1000, timeFormat: "HH:MM:ss", lang: "en", initialLoadDelay: 0, retryDelay: 2500, apiBase: "http://xxx.xxx.x.xx", apiUser: "xxxxx", apiPw: "xxxxxxxxx", sensors: [ { idx: "1", symbolon: "fa fa-user", symboloff: "fa fa-user-o", hiddenon: false, hiddenoff: false, customTitle: "", }, ], }, keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", // sample data for parse testing foo:'{ "result": [\ {"Name":"test1", "Data":"foobar1","type":"something1"},\ {"Name":"test2", "Data":"foobar2","type":"something2"},\ {"Name":"test3", "Data":"foobar3","type":"something3"},\ {"Name":"test4", "Data":"foobar4","type":"something4"}\ ]}', firstEvent: false, Base64_encode : function(input) { var output = ""; var chr1, chr2, chr3, enc1, enc2, enc3, enc4; var i = 0; input = this.Base64_utf8_encode(input); while (i < input.length) { chr1 = input.charCodeAt(i++); chr2 = input.charCodeAt(i++); chr3 = input.charCodeAt(i++); enc1 = chr1 >> 2; enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); enc4 = chr3 & 63; if (isNaN(chr2)) { enc3 = enc4 = 64; } else if (isNaN(chr3)) { enc4 = 64; } output = output + this.keyStr.charAt(enc1) + this.keyStr.charAt(enc2) + this.keyStr.charAt(enc3) + this.keyStr.charAt(enc4); } return output; }, Base64_decode : function(input) { var output = ""; var chr1, chr2, chr3; var enc1, enc2, enc3, enc4; var i = 0; input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); while (i < input.length) { enc1 = this.keyStr.indexOf(input.charAt(i++)); enc2 = this.keyStr.indexOf(input.charAt(i++)); enc3 = this.keyStr.indexOf(input.charAt(i++)); enc4 = this.keyStr.indexOf(input.charAt(i++)); chr1 = (enc1 << 2) | (enc2 >> 4); chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); chr3 = ((enc3 & 3) << 6) | enc4; output = output + String.fromCharCode(chr1); if (enc3 != 64) { output = output + String.fromCharCode(chr2); } if (enc4 != 64) { output = output + String.fromCharCode(chr3); } } output = this.Base64_utf8_decode(output); return output; }, Base64_utf8_encode : function(string) { string = string.replace(/\r\n/g, "\n"); var utftext = ""; for (var n = 0; n < string.length; n++) { var c = string.charCodeAt(n); if (c < 128) { utftext += String.fromCharCode(c); } else if ((c > 127) && (c < 2048)) { utftext += String.fromCharCode((c >> 6) | 192); utftext += String.fromCharCode((c & 63) | 128); } else { utftext += String.fromCharCode((c >> 12) | 224); utftext += String.fromCharCode(((c >> 6) & 63) | 128); utftext += String.fromCharCode((c & 63) | 128); } } return utftext; }, Base64_utf8_decode : function(utftext) { var string = ""; var i = 0; var c = 0, c1 = 0, c2 = 0, c3 = 0; while (i < utftext.length) { c = utftext.charCodeAt(i); if (c < 128) { string += String.fromCharCode(c); i++; } else if ((c > 191) && (c < 224)) { c2 = utftext.charCodeAt(i + 1); string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); i += 2; } else { c2 = utftext.charCodeAt(i + 1); c3 = utftext.charCodeAt(i + 2); string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); i += 3; } } return string; }, getStyles: function() { return ['font-awesome.css']; }, // Define required scripts. getScripts: function() { return ["moment.js"]; }, // Define start sequence. start: function() { Log.info("Starting module: " + this.name); // Set locale. moment.locale(config.language); this.loaded = false; this.status1 = false; this.title = "Loading..."; this.scheduleUpdate(this.config.updateInterval);//this.scheduleUpdate(this.config.initialLoadDelay); this.sensors = []; for (var c in this.config.sensors) { var sensor = this.config.sensors[c]; var newSensor = {idx:sensor.idx, symbolon:sensor.symbolon, symboloff:sensor.symboloff, hiddenon:sensor.hiddenon, hiddenoff:sensor.hiddenoff, customTitle:sensor.customTitle, status:"", sname:"",type:""}; Log.log(sensor.idx); this.sensors.push(newSensor); } Log.log(JSON.stringify(this.sensors)); }, // Override dom generator. getDom: function() { var wrapper = document.createElement("div"); if (!this.loaded) { wrapper.innerHTML = "Loading..."; wrapper.className = "dimmed light small"; return wrapper; } var tableWrap = document.createElement("table"); tableWrap.className = "small"; for (var c in this.sensors) { var sensor = this.sensors[c]; if((sensor.status=="On" && sensor.hiddenon)||(sensor.status=="Off" && sensor.hiddenoff)) continue; var sensorWrapper = document.createElement("tr"); sensorWrapper.className = "normal"; var symbolTD = document.createElement('td'); symbolTD.className = "symbol"; var symbol = document.createElement('i'); var symbolClass = sensor.symboloff if(sensor.status=="On") symbolClass = sensor.symbolon symbol.className = symbolClass; symbolTD.appendChild(symbol); sensorWrapper.appendChild(symbolTD); var titleTD = document.createElement('td'); titleTD.className = "title bright"; if(sensor.status=="Off") titleTD.className = "title light"; titleTD.innerHTML = sensor.sname; if(typeof sensor.customTitle !== 'undefined') titleTD.innerHTML = sensor.customTitle; sensorWrapper.appendChild(titleTD); var statusTD = document.createElement('td'); statusTD.className = "time light"; statusTD.innerHTML = sensor.status; sensorWrapper.appendChild(statusTD); tableWrap.appendChild(sensorWrapper); } wrapper.appendChild(tableWrap); return wrapper; }, updateDomo: function() { var i = 0; var self = this let userid= this.Base64_encode(this.config.apiUser) let password=this.Base64_encode(this.config.apiPw) for (var sensor of this.sensors) { var url = this.config.apiBase + ":" + this.config.apiPort + "/json.htm?username="+userid + "&password=" +password + "&type=devices&rid=" + sensor.idx; let domoRequest = new XMLHttpRequest(); domoRequest.open("GET", url, true); domoRequest.onreadystatechange = function() { if (this.readyState === 4) { // if the api response return code is 200 (ok) if (this.status === 200) { // if there was data returned // example https://github.com/driverdan/node-XMLHttpRequest/blob/master/example/demo.js // uses this.responseText , not this.response // this.response is an object if(this.responseText && this.responseText.length>0){ // process it self.processJson(JSON.parse(this.responseText)); Log.log("Loaded data"); } else { // oops, no data returned Log.error(self.name + ": no data returned."); } } else { Log.error(self.name + ": Could not load data."); Log.log("Did not load data"); // dummy call to verify parse works ok // even where there are no devices self.processJson(JSON.parse(self.foo)); } } }; // watch out having multiple requests in progress at the same time. // onreadystatechange doesn't happen til LONG time after send.. // for loop starts another request immediately domoRequest.send(); i++; } }, processJson: function(data) { if (!data) { // Did not receive usable new data. // Maybe this needs a better check? return; } Log.log("data="+data); for (var c in this.sensors) { // moved log entry here, as c is not set until start of for loop // watch out if data does not contain "result":[] array if( data.result !=='undefined' && typeof data.result ==='object' && data.result.length >0) { Log.log("****Parsing data: " + c + " " + data.result[0].Name); var sensor = this.sensors[c]; if(sensor.idx == data.result[0].idx){ this.sensors[c].sname = data.result[0].Name; this.sensors[c].status = data.result[0].Data; this.sensors[c].type = data.result[0].Type; } } } this.loaded = true; this.updateDom(this.config.animationSpeed); }, scheduleUpdate: function(delay) { Log.log("Updating.."); var nextLoad = this.config.updateInterval; if (typeof delay !== "undefined" && delay >= 0) { nextLoad = delay; } //var self = this; setInterval(() => { this.updateDomo(); }, nextLoad); } });
After this in your config.js just add for exemple:
{ module: 'domoticz', position: 'top_right', header: 'Températures', config: { apiBase: "http://xxx.xxx.x.xx", apiPort: "xx", apiUser: "xxx", apiPw: "xxxxxx", sensors: [ { idx: "183", symbolon: "fa fa-thermometer-half", symboloff: "fa fa-thermometer-half", customTitle: "Cuisine", }, { idx: "5", symbolon: "fa fa-thermometer-half", symboloff: "fa fa-thermometer-half", customTitle: "Raspberry Domotique", }, ] } },
And it works
Thanks @sdetweil for your help