more G-Labs products

Author Topic: Controlling modules from outside of home network  (Read 1690 times)

November 11, 2015, 09:02:26 PM
Read 1690 times

roger.wills

  • ***
  • Information
  • Full Member
  • Posts: 53
Hi all,

I have a basic requirement to be able to turn a zwave switch on and off when outside of my home network from a mobile device, the scenario is that I want to be able to turn the lights on when I approach home after sunset using Tasker, for those not familiar, this is an automation programme in Android.

How do I do control HG remotely to do this? I've used the web api within the network but is it simply a case of setting up portforwarding and a redirection service to replace the internal IP address or is there something built in to HG already?

Thanks

November 11, 2015, 09:38:12 PM
Reply #1

Maximo

  • ***
  • Information
  • Full Member
  • Posts: 84
Hi Roger,

This has been covered a couple of times now. HG has an API, here's the documentation for it. http://genielabs.github.io/HomeGenie/api/mig/overview.html

Hopefully that'll help you move in the right direction. Also lookup the program Ping.Me at home. It's recently been updated.

Cheers,

Garry

November 11, 2015, 10:07:01 PM
Reply #2

roger.wills

  • ***
  • Information
  • Full Member
  • Posts: 53

November 13, 2015, 01:07:30 PM
Reply #3

roger.wills

  • ***
  • Information
  • Full Member
  • Posts: 53
Hi Maximo/Garry,

Thanks for pointing me in the right direction. I have solved this now. For the benefit of novices and relative non-techies I'll document what I did.

Note I've also built in a check to see if the sunset has passed before turning the light on. For some reason I wasn't able to find any simple example of doing this elsewhere and it took me ages being a non-programmer, but Google saved the day!

Startup Code:
return false;

Program Code:
var sunset = Convert.ToDateTime(Program.WithName("Weather Underground").Parameter("Astronomy.Sunset").Value);
if (DateTime.Now >= sunset)
{
  Modules.WithName("Driveway Light").On();
  Pause (600);
 Modules.WithName("Driveway Light").Off();
}

Explanation of Code:
Written in C#
So as I am triggering the program from a mobile device outside of HG, that is all you need in the Startup Code section apparently, so I have been told!

In the Program Code, the first thing to note is that the first line of the code starts from "var and continues until the first ";". It should be all one line, but formatting is not working for me as I am writing this entry.

That first line is setting a variable up called "sunset" that uses a programme called "Weather Underground" to get the sunset time from the internet. I also convert the response to a format common with the system format so that I can compare the two times in the "If" condition on the next line. In order for this to work though you have to set up and enable the "Weather Underground" program that is already in the list of programs in HG. The instructions for that are fairly straight forward and I was able to find out how to configure it fairly easily so I will not replicate it here but basically, you have to get your API key and specify your location in the configuration of the "Weather Underground" program. Note that you only need to enable the program and configure it, you do not need to add a module for "Weather Underground" to any of your groups although you may want to in order to check that you have got it set up and running properly.

So the next line that contains the If statement calls a function that is standard in C# (I had to Google it to find out about it) called DateTime to get the current time. I then check this against the variable "sunset" (as created in line one and converted to the same format so that the check can actually be done). If it is sunset or if it is after sunset, i.e. it's getting dark or it is dark outside, then it turns the Driveway light on, leaves it on for 10 minutes (or in fact 600 seconds as per the code) and then switches it off again. If it is before sunset then it will not turn the light on. (Note I had already set up the driveway light and assume that you will do that as well. I called the module "Driveway Light").

Explanation of How the trigger event works:
So in my scenario I want to use a mobile phone to trigger the driveway light to switch on as I approach my home. I used a program called Tasker to do this. There are other programs out there that you can use I am sure.

Once that program has detected that I am near home, then I get it to make a Web API call (see Maximo's link to the documentation in the previous post) in the following format to run the program I created above:

https://<server address>/api/HomeAutomation.HomeGenie/Automation/Programs.Run/<program_address>

In my case the program number was 1007, so I simply replaced "<program address>" with that number. Note that you don't need the "<" or the ">" those are deleted when you add the programme number. You'll need to use your own program's number here. I also ran into a problem in that it didn't work at first. I discovered that it is case sensitive and had changed the "G" in "HomeGenie" to a lowercase "g" when typing it in. Probably obvious to most, but it wasn't to me, it is now though!

The server address is a little more complicated. For that to work I had to set up a forwarding service, I used no-ip.org because it was free (there are lots of them so choose one you like) and created a host. That service talks to my broadband router and takes care of the networking and allows me to get access when out and about. It took me a while to get it sorted out when I first did this, but I got there in the end through excessive use of Google!

What is not obvious, in order to get this to work is that you need to open up the port that HG uses on both your router's firewall and separately on the computer's firewall that you are using as the HG server. If you don't do that then the firewalls will simply block your request to turn on the driveway light and you'll sit there baffled as to why it's not working! It took me a long time to work that out when I first did it, I hope this note saves you a lot of time and frustration!

I actually changed the port number which you can find in HG's Maintenance section.

That's it. Hope it helps get you on the right track. Cheers.
« Last Edit: November 13, 2015, 01:34:54 PM by roger.wills »

November 13, 2015, 05:30:54 PM
Reply #4

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
Unless you change your startup code or add a check to see if the light is already on, your code will run constantly at and after time=sunset.  Your code will check for the time and if it's after sunset will turn the light on, wait 5 minutes, and turn the light off.  Then, it will immediately run again and turn the light on, wait 5 minutes, and turn the light off.  This will continue until time is not greater than sunset which I believe will be after midnight when WU sets sunset to the current date's sunset.

If that is your desired effect, then your code will work as is.  I suspect you do not want that (it will basically flash the light off momentarily every 5 minutes at night until midnight).

November 13, 2015, 05:41:15 PM
Reply #5

roger.wills

  • ***
  • Information
  • Full Member
  • Posts: 53
OK, thanks, what do I need to change so it only runs once?

Is it perhaps:

return true;

???

as per this post http://www.homegenie.it/forum/index.php?topic=50.0
« Last Edit: November 13, 2015, 05:52:38 PM by roger.wills »

November 13, 2015, 06:33:23 PM
Reply #6

roger.wills

  • ***
  • Information
  • Full Member
  • Posts: 53
Hi bkenobi,

You are correct in your assertion that I didn't want the light to momentarily flash off every 10 minutes once it has got past sunset!

But as I'd not seen the behaviour that you described when testing yesterday, I thought I would run the code (from the mobile device to be super belt and braces about it!) again to be sure. I've not changed a thing and it is behaving exactly as I expected it to, i.e. turning off after 10 minutes and staying off. I even waited a further 10 minute ( in fact a bit longer than that to be really sure).

I've checked the log and although it turns the light on and off as expected, it does make an entry when runs as follows:

<the time>     Runtime.Error      1007   HomeAutomation.HomeGenie.Autmotation

and then 10 minutes or so later:
<10 minutes later> Program.Status  Idle 1007 HomeAutomation.Homegenie.Automation

These entire coincide with the light being turned on then off again.

I am unsure what to make of it especially as it appears to be doing what I want it to do?

Thoughts?

November 13, 2015, 06:37:03 PM
Reply #7

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
Perhaps something has changed then in the way HG executes code.  In older versions there was a selection box for how to execute the code in the trigger section.  By default, the code would execute when true but you could also select other options like execute once etc.  I do not know the default operation at this point so perhaps you won't have issues as is.

November 13, 2015, 07:13:17 PM
Reply #8

roger.wills

  • ***
  • Information
  • Full Member
  • Posts: 53
OK, thanks, I'll keep an eye on it.