We’re going to get a bit more clever now. When we have taught Domoticz a list of devices (be they switches or lights or door contacts) we may want to list them all in one go without manually entering a long list of html for each device. Also, if we can get the HTML front end to gather all the information about each switch, it means that we can update the contents of the buttons automatically, for example if we change the name of a device, it will automatically update in the HTML front end.
I’ve created some code to do this and the commentary on this is over on my YouTube video.
Here’s the code and two css files that you’ll need. Please note that the css files contain more than is needed at the moment: however the css will be needed in future articles.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <head> <title>Home Control</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=0" /> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="mobile-web-app-capable" content="yes" /> <meta name="apple-mobile-web-app-status-bar-style" content="black" /> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta name="description" content="" /> <meta name="keywords" content="" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <link rel="shortcut icon" href="favicon.ico" /> <link rel="apple-touch-icon" href="iphone-icon.png"/> <link rel="icon" sizes="196x196" href="logo.png"> <link rel="icon" sizes="192x192" href="logo192.png"> <link rel="stylesheet" media="(orientation: portrait)" href="portrait.css"> <link rel="stylesheet" media="(orientation: landscape)" href="landscape.css"> <link href="https://fonts.googleapis.com/css?family=Baloo|Comfortaa" rel="stylesheet"> <script language="JavaScript" type="text/JavaScript"> <!-- <!-- var devicestodisplay =[36,35,31,141,32,134,132,29,136,140,33,1617]; var nod=0; var domoticzurl="192.168.1.163"; var domoticzport="8080"; function speak(textToSpeak) { // Create a new instance of SpeechSynthesisUtterance var newUtterance = new SpeechSynthesisUtterance(); // Set the text newUtterance.text = textToSpeak; // Add this text to the utterance queue window.speechSynthesis.speak(newUtterance); } function switchon(devicecode){ execute('PUT', 'http://'+domoticzurl+':'+domoticzport+'/json.htm?type=command¶m=switchlight&idx='+devicecode+'&switchcmd=On', ''); } function switchoff(devicecode){ execute('PUT', 'http://'+domoticzurl+':'+domoticzport+'/json.htm?type=command¶m=switchlight&idx='+devicecode+'&switchcmd=Off', ''); } function toggle(devicecode){ execute('PUT', 'http://'+domoticzurl+':'+domoticzport+'/json.htm?type=command¶m=switchlight&idx='+devicecode+'&switchcmd=Toggle', ''); } function dim(devicecode,dimlevel){ execute('PUT', 'http://'+domoticzurl+':'+domoticzport+'/json.htm?type=command¶m=switchlight&idx='+devicecode+'&switchcmd=Set%20Level&level='+dimlevel, ''); } function execute($method,$url,$message){ xmlhttp=new XMLHttpRequest(); xmlhttp.open($method,$url,true) xmlhttp.send($message); } window.onload = function() { // all of your code goes in here // it runs after the DOM is built //updateweather(); getalldevices(); updatealldevices(); } window.setInterval(function(){ updatealldevices(); }, 1000); function updatedevice(idx){ var location="DEVICE"+idx; console.log("checking status of idx "+idx); var xmlhttp = new XMLHttpRequest(); var url = 'http://'+domoticzurl+':'+domoticzport+'/json.htm?type=devices&rid='+idx; var onoff; xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { var myArr = JSON.parse(xmlhttp.responseText); onoff = myArr.result[0].Status; devicename = myArr.result[0].Name; // myFunction(myArr); document.getElementById(location).innerHTML='<span class="device">'+devicename+'</span>'; } if (onoff == "On") { document.getElementById(location).style.background = "red"; } if (onoff == "Off") { document.getElementById(location).style.background = "grey"; } if (onoff == "Open") { document.getElementById(location).style.background = "red"; } if (onoff == "Closed") { document.getElementById(location).style.background = "grey"; } } xmlhttp.open("GET", url, true); xmlhttp.send(); } function getalldevices(){ fLen = devicestodisplay.length; for (i = 0; i < fLen; i++) { preparediv(devicestodisplay[i]); getdevice(devicestodisplay[i]); } } function preparediv(deviceidx){ nod=nod+1; var div = document.createElement("div"); div.className = 'devicecontainer'; div.id = "DEVICE"+deviceidx; document.getElementById("devicesdiv").appendChild(div); } function updatealldevices(){ fLen = devicestodisplay.length; for (i = 0; i < fLen; i++) { updatedevice(devicestodisplay[i]); } } function getdevice(idx){ console.log("getting device idx" + idx); var xmlhttp = new XMLHttpRequest(); var url = "http://"+domoticzurl+":"+domoticzport+"/json.htm?type=devices&rid="+idx; xmlhttp.onreadystatechange = function() { console.log(devicestodisplay[i] + " " +xmlhttp.readyState) if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { var myArr = JSON.parse(xmlhttp.responseText); devicename = myArr.result[0].Name; devicestatus = myArr.result[0].Status; console.log(devicename + "(" + devicestatus+")"); adddevice(idx,devicename,devicestatus); } } xmlhttp.open("GET", url, true); xmlhttp.send(); } function adddevice(deviceidx,devicenametext,devicestatus){ nod=nod+1; var div = document.createElement("div"); //div.style.width = "100px"; //div.style.height = "100px"; //div.style.background = "red"; //div.style.color = "white"; div.className = 'devicecontainer'; div.innerHTML = devicenametext; div.id = "DEVICE"+deviceidx; if (devicestatus=="On"){ div.style.background = "red"; } if (devicestatus=="Open"){ div.style.background = "red"; } if (devicestatus=="Off"){ div.style.background = "grey"; } if (devicestatus=="Closed"){ div.style.background = "grey"; } console.log("-------------"+div.id); //document.getElementById("devicesdiv").appendChild(div); document.getElementById(div.id).addEventListener('click', function() { { toggle(deviceidx); }; }, false); } function myFunction(arr) { var out = ""; out += arr[0].result.Status; document.getElementById("id01").innerHTML = out; } //--> </script> <body> <audio id="scene" src="assets/sounds/beep2.mp3" preload="auto"></audio> <div align="left" class="toplinks"> <a href="index.htm"><span class="menutext">Home</span></a> <a href="lights.htm"><span class="menutext">Lights</span></a> <a href="devices.htm"><span class="menutext">Devices</span></a> <a href="audiotrial.htm"><span class="menutext">Audio</span></a> <a href="climate.htm"><span class="menutext">Environment</span></a> <a href="security.htm"><span class="menutext">Security</span></a> <a href="activities.htm"><span class="menutext">Other screens</span></a></div> <div id="notification" align="center" class="notificationpane" onClick="clearnotification(11);"></div> <div id="devicesdiv"></div> </body> </html>
Here’s the landscape.css file
body { background-color:#000000; font-family: 'Comfortaa', sans-serif; margin: 0 0 0 0; padding: 0 0 0 0 ; } .back-to-top { background: none; margin: 0; position: fixed; bottom: 50px; right: 20px; width: 150px; height: 50px; z-index: 100; display: none; text-decoration: none; } .back-to-top i { font-size: 60px; } .timerselecttext { font-size: 60px; color:#FFFFFF; } .clockdisplay { font-size: 250px; color:grey; } div.clocktext { font-size: 100px; color:#000000; } div.weathertext { font-size: 70px; color:#000000; } div.standardcontainer { background-color:grey; position:relative; width:25%; height:25vh; float:left; padding-top:10px; overflow:auto; color:white; font-size: 4vw; text-align:center; box-shadow:inset 0px 0px 0px 1px black; } div.doublecontainer { background-color:grey; position:relative; width:50%; height:25vh; float:left; padding-top:10px; overflow:auto; color:white; font-size: 4vw; text-align:center; box-shadow:inset 0px 0px 0px 1px black; } div.devicecontainer { background-color:grey; width:25%; height:12vh; float:left; padding-top:10px; overflow:auto; color:white; font-size: 2vw; box-shadow:inset 0px 0px 0px 1px black; } div.albumcontainer { width:28vw; height:28vw; float:left; padding-top:0px; color:white; font-size: 3vw; box-shadow:inset 0px 0px 0px 1px black; } div.trackcontainer { width:70vw; height:10vw; float:left; padding-top:0px; color:white; font-size: 3vw; box-shadow:inset 0px 0px 0px 1px black; } div.audiocontrols { background-color:grey; width:100vw; height:10vw; float:left; padding-top:0px; color:white; font-size: 3vw; box-shadow:inset 0px 0px 0px 1px black; } div.audioactivitycontainer { width:70vw; height:10vw; float:left; padding-top:0px; color:white; font-size: 3vw; box-shadow:inset 0px 0px 0px 1px black; } div.camera { background-color:grey; width:100vw; float:left; padding-top:0px; color:white; font-size: 3vw; box-shadow:inset 0px 0px 0px 1px black; } div.camcontrols { background-color:green; width:300px; float:right; padding-top:0px; color:white; font-size: 3vw; box-shadow:inset 0px 0px 0px 1px black; } span { background-color: #ffffff; color: #75099b; font-size:20px; display: inline-block; padding: 3px 10px; margin: 5px; font-weight: bold; border-radius: 5px; } div.nightclock { background-color:#333333; position:relative; width:100%; float:left; padding-top:10px; overflow:auto; color:#666666; font-size: 5vw; text-align:center; box-shadow:inset 0px 0px 0px 1px black; } span.title { background-color:#666666; color:#FFFFFF; font-size:35px; display: inline-block; padding: 10px 10px; margin: 5px; font-weight: bold; border-radius: 5px; } span.timertext { background-color:green; color:#FFFFFF; font-size:70px; display: inline-block; padding: 10px 10px; margin: 5px; font-weight: bold; border-radius: 5px; } span.playstate { background-color:green; color:#FFFFFF; font-size:12px; display: inline-block; padding: 10px 10px; margin: 5px; font-weight: bold; border-radius: 5px; } span.temptext { background-color:green; color:#FFFFFF; font-size:40px; display: inline-block; padding: 10px 10px; margin: 5px; font-weight: bold; border-radius: 5px; } span.roomname { background-color:green; color:#FFFFFF; font-size:40px; display: inline-block; padding: 10px 10px; margin: 5px; font-weight: bold; border-radius: 5px; } span.lightroom { background-color:green; color:#FFFFFF; font-size:30px; display: inline-block; padding: 10px 10px; margin: 5px; font-weight: bold; border-radius: 5px; } span.device { background-color:#666666; color:#FFFFFF; font-size:20px; display: inline-block; padding: 3px 10px; margin: 5px; font-weight: bold; border-radius: 5px; } span.sensor { background-color:#666666; color:#FFFFFF; font-size:18px; display: inline-block; padding: 3px 10px; margin: 0px 5px; font-weight: bold; border-radius: 5px; } span.screentitle { background-color:#666666; font-family: 'Baloo', sans-serif; color:#FFFFFF; font-size:30px; display: inline-block; padding: 10px 10px; margin: 5px; border-radius: 5px; } span.menutext { background-color:#0066FF; color:#FFFFFF; font-size:24px; display: inline-block; padding: 10px 10px; margin: 5px; font-weight: bold; border-radius: 5px; } span.datetime { background-color:#009900; color:#FFFFFF; font-size:16px; display: inline-block; padding: 5px 5px; margin: 0px 5px; border-radius: 5px; } div.audioroomcontainer { background-color:#66FF99; float:left; height:200px; padding-top:10px; overflow:auto; font-size: 25px; margin: 0 0 10px 10px; } div.nightmodecontainer { background-color:#666666; width:150px; float:left; height:200px; padding-top:10px; overflow:auto; font-size: 25px; margin: 0 0 10px 10px; } div.notificationpane { background-color:#FFFFCC; width:100%; font-size: 2.5vw; box-shadow:inset 0px 0px 0px 1px black; } div.nightnotificationpane { background-color:grey; width:100%; font-size: 2.5vw; box-shadow:inset 0px 0px 0px 1px black; } div.toplinks { background-color:#66CCFF; top:0px; left:0px; width:100%; font-size: 30px; box-shadow:inset 0px 0px 0px 1px black; } div.popuppanel { background-color:grey; width:100vw; float:left; padding-top:10px; overflow:auto; color:white; font-size: 3vw; box-shadow:inset 0px 0px 0px 1px black; }
and here’s the portrait.css file
body { background-color:#000000; font-family: 'Comfortaa', sans-serif; margin: 0 0 0 0; padding: 0 0 0 0 ; } .back-to-top { background: none; margin: 0; position: fixed; bottom: 50px; right: 20px; width: 150px; height: 50px; z-index: 100; display: none; text-decoration: none; } .back-to-top i { font-size: 60px; } .timerselecttext { font-size: 60px; color:#FFFFFF; } .clockdisplay { font-size: 200px; color:#000000; } div.clocktext { font-size: 100px; color:#000000; } div.weathertext { font-size: 70px; color:#000000; } div.nightclock { background-color:#333333; position:relative; width:100%; float:left; padding-top:10px; overflow:auto; color:#666666; font-size: 5vw; text-align:center; box-shadow:inset 0px 0px 0px 1px black; } div.doublecontainer { background-color:grey; position:relative; width:100%; height:25vh; float:left; padding-top:10px; overflow:auto; color:white; font-size: 4vw; text-align:center; box-shadow:inset 0px 0px 0px 1px black; } div.standardcontainer { background-color:grey; position:relative; width:50%; height:15vh; float:left; padding-top:10px; overflow:auto; color:white; font-size: 2vw; text-align:center; box-shadow:inset 0px 0px 0px 1px black; } div.nightclock { background-color:#333333; position:relative; width:100%; float:left; padding-top:10px; overflow:auto; color:#666666; font-size: 5vw; text-align:center; box-shadow:inset 0px 0px 0px 1px black; } div.devicecontainer { background-color:grey; width:50%; height:10vh; float:left; padding-top:10px; overflow:auto; color:white; font-size: 2vw; box-shadow:inset 0px 0px 0px 1px black; } div.albumcontainer { background-color:grey; width:100vw; height:100vw; float:left; padding-top:0px; color:white; font-size: 3vw; box-shadow:inset 0px 0px 0px 1px black; } div.trackcontainer { background-color:grey; width:100vw; float:left; padding-top:0px; color:white; font-size: 3vw; box-shadow:inset 0px 0px 0px 1px black; } div.audiocontrols { background-color:grey; width:100vw; float:left; padding-top:0px; color:white; font-size: 3vw; box-shadow:inset 0px 0px 0px 1px black; } div.audioactivitycontainer { background-color:grey; width:100vw; float:left; padding-top:0px; color:white; font-size: 3vw; box-shadow:inset 0px 0px 0px 1px black; } div.camera { background-color:grey; width:100vw; float:left; padding-top:0px; color:white; font-size: 3vw; box-shadow:inset 0px 0px 0px 1px black; } div.camcontrols { background-color:green; width:100vw; float:right; padding-top:0px; color:white; font-size: 3vw; box-shadow:inset 0px 0px 0px 1px black; } span.menutext { background-color: #ffffff; color: #75099b; font-size:3vw; display: inline-block; padding: 3px 10px; margin: 5px; font-weight: bold; border-radius: 5px; } span { background-color: #ffffff; color: #75099b; font-size:18px; display: inline-block; padding: 3px 10px; margin: 5px; font-weight: bold; border-radius: 5px; } span.title { background-color:#666666; color:#FFFFFF; font-size:18px; display: inline-block; padding: 3px 10px; margin: 5px; font-weight: bold; border-radius: 5px; } span.timertext { background-color:green; color:#FFFFFF; font-size:20px; display: inline-block; padding: 10px 10px; margin: 5px; font-weight: bold; border-radius: 5px; } span.lightroom { background-color:green; color:#FFFFFF; font-size:16px; display: inline-block; padding: 10px 10px; margin: 5px; font-weight: bold; border-radius: 5px; } span.temptext { background-color:green; color:#FFFFFF; font-size:20px; display: inline-block; padding: 10px 10px; margin: 5px; font-weight: bold; border-radius: 5px; } span.device { background-color:#666666; color:#FFFFFF; font-size:18px; display: inline-block; padding: 3px 10px; margin: 5px; font-weight: bold; border-radius: 5px; } span.screentitle { background-color:#666666; font-family: 'Baloo', sans-serif; color:#FFFFFF; font-size:18px; display: inline-block; padding: 5px 5px; margin: 5px; border-radius: 5px; } span.menutext { background-color:#0066FF; color:#FFFFFF; font-size:24px; display: inline-block; padding: 3px 10px; margin: 5px; font-weight: bold; border-radius: 5px; } span.datetime { background-color:#009900; color:#FFFFFF; font-size:12px; display: inline-block; padding: 5px 5px; margin: 0px 5px; border-radius: 5px; } span.sensor { background-color:#666666; color:#FFFFFF; font-size:14px; display: inline-block; padding: 3px 10px; margin: 0px 5px; font-weight: bold; border-radius: 5px; } div.audioroomcontainer { background-color:#66FF99; float:left; height:200px; padding-top:10px; overflow:auto; font-size: 25px; margin: 0 0 10px 10px; } div.nightmodecontainer { background-color:#666666; width:150px; float:left; height:200px; padding-top:10px; overflow:auto; font-size: 25px; margin: 0 0 10px 10px; } div.notificationpane { background-color:#FFFFCC; width:100%; font-size: 16px; box-shadow:inset 0px 0px 0px 1px black; } div.toplinks { background-color:#66CCFF; top:0px; left:0px; width:100%; font-size: 30px; overflow:scroll; box-shadow:inset 0px 0px 0px 1px black; } div.popuppanel { background-color:grey; width:100vw; float:left; padding-top:10px; overflow:auto; color:white; font-size: 2vw; box-shadow:inset 0px 0px 0px 1px black; }