HomeGenie Forum

General Category => Troubleshooting and Support => Topic started by: kevinvinv on October 22, 2016, 10:55:36 PM

Title: Detecting night and day
Post by: kevinvinv on October 22, 2016, 10:55:36 PM
Once I switched to version 525 my night and day detection stopped working.  I might have messed it up myself though...   Does anyone care to examine the below and see if they see something wrong?  Basically it always reports  NIGHT

 if (module.Is("BackPorchMotion"))
     { 
 
        //jkUtils
         time_sunrise = DateTime.ParseExact(Program.WithName("jkUtils - Solar Altitude").Parameter("jkUtils.SolarAltitude.Morning.Sunrise.Start").Value, "H:mm", System.Globalization.CultureInfo.InvariantCulture);
         time_sunset = DateTime.ParseExact(Program.WithName("jkUtils - Solar Altitude").Parameter("jkUtils.SolarAltitude.Evening.Sunset.End").Value, "H:mm", System.Globalization.CultureInfo.InvariantCulture);
         //Program.Notify("Hello","time_sunrise");
       
        //[email protected];
        //[email protected];
     

        if (DateTime.Compare(DateTime.Now, time_sunrise)<0 || DateTime.Compare(DateTime.Now, time_sunset)>0)
        {
          Night = true;
          Program.Notify("Msg from BackPorchMotion","It is Nighttime");
          //Log("Night");
        }
        else
        {
          Night = false;
          Program.Notify("Msg from BackPorchMotion","It is Daytime");
          //Log("Day");
        }
     }
Title: Re: Detecting night and day
Post by: mvdarend on October 23, 2016, 07:34:36 AM
To make things more readable in v525 I rewrote a number of my scripts that were using similar logic to yours.

What I have now for the above:

Created a new Scheduler rule: Nighttime
Code: [Select]
@SolarAltitude.Evening.Sunset.End > @SolarAltitude.Morning.Sunrise.Start
And then you can basically replace all your above code with the following (untested)

Code: [Select]
if (module.Is("BackPorchMotion"))

    if (Scheduler.IsScheduling("Nighttime"))
    {
          Night = true;
          Program.Notify("Msg from BackPorchMotion","It is Nighttime");
          //Log("Night");
    }
    else
    {
          Night = false;
          Program.Notify("Msg from BackPorchMotion","It is Daytime");
          //Log("Day");
    }
}
Title: Re: Detecting night and day
Post by: kevinvinv on October 23, 2016, 04:30:23 PM
Oh-  I think I understand that... I'll give it a try.  Thanks!!!
Title: Re: Detecting night and day
Post by: mvdarend on October 23, 2016, 09:33:02 PM
I just edited the above code, I forgot the 'if (module.Is("BackPorchMotion"))' part...
Title: Re: Detecting night and day
Post by: bkenobi on October 24, 2016, 04:26:01 PM
That is a lot simpler than what I have in the Advanced Smart Lights code.  If/when I update to the new scheduler, it looks like I'll need to rewrite some things to simplify!
Title: Re: Detecting night and day
Post by: [email protected] on October 25, 2016, 12:10:58 PM
Is it worth editing the built in smartlights code and submitting a pull request to replace it with yours?
Title: Re: Detecting night and day
Post by: bkenobi on October 25, 2016, 04:27:57 PM
I thought about it, but Gene actually updated his built-in code recently.  I should put it in the repository so it would be easier to find though.  All of my contributions are in the ASL thread, but none are in the repository.  I never updated them to conform to the new standards (they still use both pages to define the code and the current approach is to avoid doing that) so I didn't know if it should be put up there prior to the fix.
Title: Re: Detecting night and day
Post by: kevinvinv on October 29, 2016, 06:13:11 AM
Well, I cant get it to work.

Does  Scheduler.IsScheduling()  work with the NEW schedule tool?   And if so,  what does it actually mean? 

I searched the forums and found a lot of stuff on Scheduler.IsScheduling way before this new 525 schedule tool came out...   maybe it doesn't work with the new schedule tool?

Thoughts?
Title: Re: Detecting night and day
Post by: mvdarend on October 29, 2016, 07:09:57 AM
My apologies, apparently it doesn't work here either with the new scheduling tool...

I'd rewritten a lot of scripts to something like the above, but had not actually put them in use yet. (lighting was not yet installed in the new house)

Because it compiled and 'looked' like it should work I just assumed it would be OK....  :-[

Sorry about that...

Another option is to look at the new scripting options in the Scheduler, SensorTimeout is a pretty good example of what you're trying to achieve I think.
Title: Re: Detecting night and day
Post by: kevinvinv on October 29, 2016, 07:11:14 AM
OK Thanks!  At least I feel better now!  :)

I'll keep looking! 
Title: Re: Detecting night and day
Post by: kevinvinv on October 29, 2016, 07:13:47 AM
BTW--  sorry-  can you tell me where to find the SensorTimeout thing you mentioned?

Thanks again!!!
Title: Re: Detecting night and day
Post by: mvdarend on October 29, 2016, 07:16:54 AM
Configure -> Scheduler the schedule 'Sensor.Timeout' should be at the top.

Gene has said he's working on the documentation (http://www.homegenie.it/forum/index.php?topic=1721.msg10856#msg10856), hopefully we can find out if Scheduler.IsScheduling is deprecate or not.
Title: Re: Detecting night and day
Post by: kevinvinv on October 29, 2016, 07:21:00 PM
I've heard talk about a virtual module... Maybe this is what I need to use .

Have the scheduler enable a module during the night
Query this module in my code.

Thoughts?

I am growing a little weary of this hobby... and I think my wife is too!
Title: Re: Detecting night and day
Post by: kevinvinv on October 30, 2016, 05:32:53 AM
OK-  so here is what is working as far as detecting night and day

I use the scheduler to turn on a dusk to dawn X10 channel

I detect Night by just checking the state of that module like this:

________________________
bool Night = false;

 // Query Dusk to Dawn module to see if it is night or not
       var DuskToDawn = Modules.InDomain("HomeAutomation.X10").WithAddress("A1").Get();
       var NSense = DuskToDawn.Parameter("Status.Level").DecimalValue;
       
       if(NSense==1) { 
           Night=true;
           Program.Notify("Hello", "It is Night");
       } else {
           Night=false;
           Program.Notify("Hello", "It is Day");
       }


___________________________


Thanks!!