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.