External link towards OpenHAB (item)

My purpose in posting that link was primarily for you to look at the HTML page.

To implement you ModeAway.php using just an HTML file that can be served by OH through myopenhab.org it’s simple enough:

<!DOCTYPE html>
<html>
	<head>
		<title>Mode Away</title>
	</head>
<script>
// Change these values to your Item and the state you want to send when this page is loaded.

var item = 'HouseMode'
var code = "AWAY"

document.write("Preparing to update " + item + " with " + code)

var xhr = new XMLHttpRequest();
xhr.open('POST', "https://myopenhab.org/rest/items/"+item);
xhr.setRequestHeader("Content-Type", "text/plain");
xhr.setRequestHeader("Accept", "application/json");
xhr.onreadystatechange = function() {
	if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
		document.write("Received  " + code);
	}
	else if(xhr.status != 200) {
		document.write("Error in callback: " + xhr.status + " " + xhr.responseText);
	}
}
xhr.send(code)

document.write("Sent " + code + "...");

</script>

</html>

No need for a separate server. No need for PHP. No need for external scripts. And since you are not passing arguments as part of the URL, you don’t need all that JavaScript up front to parse out the arguments. If we assume this is saved to modeaway.html and placed in the html folder of your OH conf you would activate the command to your HouseMode Item by accessing https://myopenhab.org/static/modeaway.html from your browser or from an app.

And for security this goes through myopenhab.org so you have all the same security that you have now.

Seems like a win.

And you can make it smarter if you want/need to. For example, if we add back the argument parser, you can pass the Item and the state as arguments and make the HTML generic.

<!DOCTYPE html>
<html>
	<head>
		<title>OAuth2 Catcher</title>
	<script type="text/javascript">
	function getAllUrlParams(url) {

  // get query string from url (optional) or window
  var queryString = url ? url.split('?')[1] : window.location.search.slice(1);

  // we'll store the parameters here
  var obj = {};

  // if query string exists
  if (queryString) {

    // stuff after # is not part of query string, so get rid of it
    queryString = queryString.split('#')[0];

    // split our query string into its component parts
    var arr = queryString.split('&');

    for (var i=0; i<arr.length; i++) {
      // separate the keys and the values
      var a = arr[i].split('=');

      // in case params look like: list[]=thing1&list[]=thing2
      var paramNum = undefined;
      var paramName = a[0].replace(/\[\d*\]/, function(v) {
        paramNum = v.slice(1,-1);
        return '';
      });

      // set parameter value (use 'true' if empty)
      var paramValue = typeof(a[1])==='undefined' ? true : a[1];

      // (optional) keep case consistent
      paramName = paramName.toLowerCase();
      paramValue = paramValue.toLowerCase();

      // if parameter name already exists
      if (obj[paramName]) {
        // convert value to array (if still string)
        if (typeof obj[paramName] === 'string') {
          obj[paramName] = [obj[paramName]];
        }
        // if no array index number specified...
        if (typeof paramNum === 'undefined') {
          // put the value on the end of the array
          obj[paramName].push(paramValue);
        }
        // if array index number specified...
        else {
          // put the value at that index number
          obj[paramName][paramNum] = paramValue;
        }
      }
      // if param name doesn't exist yet, set it
      else {
        obj[paramName] = paramValue;
      }
    }
  }

  return obj;
}
	</script>
	</head>
<script>
var item = getAllUrlParams().item
var state = getAllUrlParams().state;
document.write("Preparing to update " + item + " with state " + state)

var xhr = new XMLHttpRequest();
xhr.open('POST', "https://myopenhab.org/rest/items/"+item);
xhr.setRequestHeader("Content-Type", "text/plain");
xhr.setRequestHeader("Accept", "application/json");
xhr.onreadystatechange = function() {
	if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
		document.write("Received  " + state);
	}
	else if(xhr.status != 200) {
		document.write("Error in callback: " + xhr.status + " " + xhr.responseText);
	}
}
document.write("Sent " + state + "...");
xhr.send(state)

</script>

</html>

If we save this to commanditem.html in the conf/html folder you can send a command to ANY Item by accessing https://myopenhab.org/static/commanditem.html?item=HouseMode&state=AWAY from you browser or app or whatever you are using. Again, you are going through myopenhab.org so there is no need for a separate web server and you have all the same security that you have now.

Or if you have ClassicUI installed, as described in the link above, you can access https://myopenhab.org/classicui/CMD?HouseMode=AWAY to issue commands to your Items. That is easy enough. Again, you are going through myopenhab.org so there is no need for a separate server and you have all the same security you have now.

Another alternative is you can use IFTTT Button Widgets to add single button icons on your phone’s screens to trigger these commands.

Under no circumstances should you stand up your own web server and expose it to the Internet. That is just asking to be attacked.

2 Likes