Read the statement by Michael Teeuw here.
Trying to write my own Module...
-
@nbrenn I’m assuming that you saved your server response into this.xml
getDom: function(){ var wrapper = document.createElement("div"); if(this.xml){ var channels = this.xml.getElementsByTagName("channel"); for(var i = 0; i < channels.length; i++){ for(var n = 0; n < channels[i].children.length; n++){ if(channels[i].children[n].tagName === "name" || channels[i].children[n].tagName === "watts"){ var element = document.createElement("div"); element.classList.add(channels[i].children[n].tagName); element.innerHTML = channels[i].children[n].textContent; wrapper.appendChild(element); } } } } else { wrapper.innerHTML = "NO DATA"; } return wrapper; }this would create two
divcontainer for every channel one for thenameand the other forwatts. each of them would have the css class.nameor.watts -
Thanks for the great support.
I currently have no errors with what you’ve helped provide me with. However, it displays the
elsecondition, andNO DATA.Right now, my
MMM-EMonitor.jslooks like the following:Module.register ("MMM-EMonitor", { //default module config. defaults: { // Insert defaults here interval: 900000 // Every 15 mins }, getStyles: function() { return ["MMM-EMonitor.css"]; }, // Define the start sequence start: function() { Log.log("Starting module: " + this.name); //this.securitykey = REDACTED; this.url = 'https://api.emonitor.us/location/getCurrentData?security_key=REDACTED'; this.getCurrentData(this); }, getCurrentData: function(that) { that.sendSocketNotification('GET-CURRENT-DATA', that.url); setTimeout(that.getCurrentData, that.config.interval, that); }, getDom: function(){ var wrapper = document.createElement("div"); if(this.xml){ var channels = this.xml.getElementsByTagName("channel"); for(var i = 0; i < channels.length; i++){ for(var n = 0; n < channels[i].children.length; n++){ if(channels[i].children[n].tagName === "name" || channels[i].children[n].tagName === "watts"){ var element = document.createElement("div"); element.classList.add(channels[i].children[n].tagName); element.innerHTML = channels[i].children[n].textContent; wrapper.appendChild(element); } } } } else { wrapper.innerHTML = "NO DATA"; } return wrapper; } });and my
csslooks like the following:.MMM-EMonitor .centered { text-align: center; }Like I mentioned, the
NO DATAis showing up. The security key is working, and returns the XML which I have in my original post. -
You don’t have
this.xmldefined anywhere in your script. If you look at @strawberry-3-141’s post, it says:I’m assuming that you saved your server response into this.xml
You need to create a function that uses the URL and saves that data in a variable called
xml. Since your program never does that,this.xmlisn’t defined so your if/else always goes to “NO DATA”.Why do you have
getCurrentData: function(that) { that.sendSocketNotification('GET-CURRENT-DATA', that.url); setTimeout(that.getCurrentData, that.config.interval, that); },in your program? Did you just see it in another persons code? This code snippet sends your URL to node_helper.js. Are you using that node_helper to pull the XML data from the website? If so, you’re going to need a
socketNotificationReceivedfunction to get the data and put it into thexmlvariable.The modules documentation shows you some examples of that.
-
Ahh, that makes sense. I do not have a
node_helper.js, and I had thegetCurrentDatafunction because I tried to replicate a different module. I will remove these lines of code since they serve no purpose for me at the moment.I’ll try to take a look at examples to see how to save the data into the
xmlvariable, so thatthis.xmlcan be used. -
MMM-EMonitor.js
Module.register ("MMM-EMonitor", { //default module config. defaults: { // Insert defaults here interval: 900000, // Every 15 mins security_key: null, animationSpeed: 1000 }, getStyles: function() { return ["MMM-EMonitor.css"]; }, // Define the start sequence start: function() { Log.log("Starting module: " + this.name); this.loaded = false; this.xml = null; this.url = 'https://api.emonitor.us/location/getCurrentData?security_key='; setInterval(this.getCurrentData(), this.config_interval); }, getCurrentData: function() { var self = this; var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { self.xml = self.parseXML(this.responseText); self.loaded = true; self.updateDom(self.config.animationSpeed); } }; xhttp.open("GET", this.url+this.config.security_key, true); xhttp.send(); }, getDom: function(){ var wrapper = document.createElement("div"); if(!this.loaded) { wrapper.innerHTML = "Loading..."; return wrapper; } if(this.xml !== null){ var table = document.createElement("table"); table.classList.add("xsmall", "table"); var channels = this.xml.getElementsByTagName("channel"); for(var i = 0; i < channels.length; i++){ var row = document.createElement("tr"); for(var n = 0; n < channels[i].children.length; n++){ if(channels[i].children[n].tagName === "name" || channels[i].children[n].tagName === "watts"){ var element = document.createElement("td"); element.classList.add(channels[i].children[n].tagName); element.innerHTML = channels[i].children[n].textContent; row.appendChild(element); } table.appendChild(row); } } wrapper.appendChild(table); } else { console.log("Returned no Data"); wrapper.innerHTML = "NO DATA"; } return wrapper; }, parseXML: function(xmlStr){ return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml"); } });Your config.js should have:
{ module: 'MMM-EMonitor', position: 'top_left', config: { security_key: 'XXXXXXXXXXXXXXX' //your security key } }This just displays everything in a large table. You may want to modify the for loop to not display data when the wattage is 0, that should get rid of a bunch of rows.
-
This is awesome.
A few clarifying questions:
-
Is the purpose of the
xhttp.open("GET"...line to make the API call to the website? Is this when the connection is made to the XML page? -
So, you’re going through each
"channel"and creating a row in the table for each channel? And then for each channel’s name and wattage, a data object is created? -
If I didn’t have any
.cssfile, would the data still show up?
Thanks for the clarifications, and again, thanks for the great support. This community is top-notch!
-
-
- yes the xmlhttprequest is also better known as ajax request
- for each channel a row will be created and for name and watts a column will be created with their specific values
- the data will still be displayed, but without any custom styling
-
I’m trying to display just the
nameswhere thewattsthat are != 0. I want both the values greater than 0 and the values less than 0 (in order to include the negative values from the solar generation).I attempted to modify the
getDom. It is displaying everynamesin the .XML and shows thewattsvalue only if it is != 0. But how do I also remove thenamesthat are 0? The output currently looks like the following:Power Panel 66 Outlets 20 Heat Pump Oven East Solar Panel -70 West Solar Panel -50getDom: function(){ var wrapper = document.createElement("div"); if(!this.loaded) { wrapper.innerHTML = "Loading..."; return wrapper; } if(this.xml !== null){ var table = document.createElement("table"); table.classList.add("xsmall", "table"); var channels = this.xml.getElementsByTagName("channel"); for(var i = 0; i < channels.length; i++){ var row = document.createElement("tr"); for(var n = 0; n < channels[i].children.length; n++){ if(channels[i].children[n].tagName === "name" || channels[i].children[n].tagName === "watts"){ var element = document.createElement("td"); element.classList.add(channels[i].children[n].tagName); if (channels[i].children[n].textContent != 0){ element.innerHTML = channels[i].children[n].textContent; row.appendChild(element); } } table.appendChild(row); } } wrapper.appendChild(table); } else { console.log("Returned no Data"); wrapper.innerHTML = "NO DATA"; } return wrapper; }, -
Looks like you need to modify your other if/then statement. The one you’ve modified checks both the name and watts to see if they are !=0. Since the name always !=0, it’s displayed.
-
It looks like the values are not updating. (The wattage values didn’t change at all overnight).
Should the line:
setInterval(this.getCurrentData(), this.config_interval);be changed to:
setInterval(this.getCurrentData(), this.interval);Are we calling the
intervalvariable in the config incorrectly?Thanks!
-
@nbrenn
this.config.interval -
For posterity, I modified the if statement as follows, to remove the row items that have a 0 for wattage:
if (channels[i].children[n].textContent != 0) { element.innterHTML = channels[i].children[n].textContent; row.appendChild(element); table.appendChild(row); } else { table.removeChild(row); } -
Glad it all worked out for you!
-
I’m working on getting historical data (my API has a function
getHistoricalData). The XML looks like the following:<Time Time="2016-11-03 00:00:00"> <channel channel="194882" name="Main Power Main Panel"> <kWh>0.319</kWh> </channel> <channel channel="194885" name="Outlet 1"> <kWh>0.000</kWh> </channel> <channel channel="194886" name="Solar Panel 1"> <kWh>-5.737</kWh> </channel> <channel channel="194887" name="Solar Panel 2"> <kWh>-4.877</kWh> </channel>The difference between this XML output, and the XML output from my
getCurrentDatacall, is thatnameis not it’s own element. With the old function,<name>was in brackets. Now, it isname = ....Would be
getDombe the same as it was forgetCurrentData, or is it different since it’sname=and not it’s own<name> ... </name> -
You’re going to have to modify the code to get that info. Take a look here for info on how it’s done.
-
@mochman Thanks for sharing the link to the w3 schools page! I managed to get it working by following the example on the website. Glad I learned something in the process! Happy New Year!
-
@mochman ->
How would I go about formatting my table? I would like to have each column have it’s own title, like “Load” and “kWh”. Is it also possible to put them into a tabulated table?
My
getDOMcurrently looks like the following:getDom: function(){ var wrapper = document.createElement("div"); if(!this.loaded) { wrapper.innerHTML = "Loading..."; return wrapper; } if(this.xml !== null){ var table = document.createElement("table"); table.classList.add("xsmall", "table"); var channels = this.xml.getElementsByTagName("channel"); for(var i = 0; i < channels.length; i++){ var row = document.createElement("tr"); for(var n = 0; n < channels[i].children.length; n++){ name = channels[i].getAttribute('name'); if(channels[i].children[n].tagName === "kWh"){ var element = document.createElement("td"); element.classList.add(channels[i].children[n].tagName); if (channels[i].children[n].textContent != 0){ element.innerHTML = name + channels[i].children[n].textContent; row.appendChild(element); table.appendChild(row); } } } } wrapper.appendChild(table); } else { console.log("Returned no Data"); wrapper.innerHTML = "NO DATA"; } return wrapper; }, parseXML: function(xmlStr){ return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml"); }
-
You should just need to put
var row = document.createElement("tr"); var header = document.createElement("th"); var header2 = document.createElement("th"); header.innerHTML = "Load"; row.appendChild(header); header2.innerHTML = "KWh"; row.appendChild(header2); table.appendChild(row);before
var channels = this.xml.getElementsByTagName("channel");You can add a title by modifying your
config.jsfile to add a header.
The documentation for that is here. -
@mochman Thanks a lot! I’ll take a look at the documentation and try your suggestion.
Have a good weekend!
-
@mochman I have another question. As shown below in the attached image, I am appending the
nameto thekWhvalues. How do I put the name into theLoadcolumn and the numbers into theirkWhcolumn?In this case, the “name” is in the same object as the channel, so that’s why I extracted it the way I do in my code… I have the
nameas a variable, but I can’t seem to put it into it’s own column.
Code:
getDom: function(){ var wrapper = document.createElement("div"); if(!this.loaded) { wrapper.innerHTML = "Loading..."; return wrapper; } if(this.xml !== null){ var table = document.createElement("table"); var row = document.createElement("tr"); var header = document.createElement("th"); var header2 = document.createElement("th"); header.innerHTML = "Load"; row.appendChild(header); header2.innerHTML = "kWh"; row.appendChild(header2); table.appendChild(row); table.classList.add("xsmall", "table"); var channels = this.xml.getElementsByTagName("channel"); for(var i = 0; i < channels.length; i++){ var row = document.createElement("tr"); for(var n = 0; n < channels[i].children.length; n++){ name = channels[i].getAttribute('name'); if(channels[i].children[n].tagName === "kWh"){ var element = document.createElement("td"); element.classList.add(channels[i].children[n].tagName); if (channels[i].children[n].textContent != 0){ element.innerHTML = name + " " + channels[i].children[n].textContent; row.appendChild(element); table.appendChild(row); } } } } wrapper.appendChild(table); } else { console.log("Returned no Data"); wrapper.innerHTML = "NO DATA"; } return wrapper; },
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login