more G-Labs products

Author Topic: Detecting night and day  (Read 1246 times)

October 22, 2016, 10:55:36 PM
Read 1246 times

kevinvinv

  • ****
  • Information
  • Sr. Member
  • Posts: 196
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");
        }
     }

October 23, 2016, 07:34:36 AM
Reply #1

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
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");
    }
}
« Last Edit: October 23, 2016, 09:32:36 PM by mvdarend »

October 23, 2016, 04:30:23 PM
Reply #2

kevinvinv

  • ****
  • Information
  • Sr. Member
  • Posts: 196
Oh-  I think I understand that... I'll give it a try.  Thanks!!!

October 23, 2016, 09:33:02 PM
Reply #3

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
I just edited the above code, I forgot the 'if (module.Is("BackPorchMotion"))' part...

October 24, 2016, 04:26:01 PM
Reply #4

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
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!

October 25, 2016, 12:10:58 PM
Reply #5

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
Is it worth editing the built in smartlights code and submitting a pull request to replace it with yours?

October 25, 2016, 04:27:57 PM
Reply #6

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
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.

October 29, 2016, 06:13:11 AM
Reply #7

kevinvinv

  • ****
  • Information
  • Sr. Member
  • Posts: 196
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?

October 29, 2016, 07:09:57 AM
Reply #8

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
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.

October 29, 2016, 07:11:14 AM
Reply #9

kevinvinv

  • ****
  • Information
  • Sr. Member
  • Posts: 196
OK Thanks!  At least I feel better now!  :)

I'll keep looking! 

October 29, 2016, 07:13:47 AM
Reply #10

kevinvinv

  • ****
  • Information
  • Sr. Member
  • Posts: 196
BTW--  sorry-  can you tell me where to find the SensorTimeout thing you mentioned?

Thanks again!!!

October 29, 2016, 07:16:54 AM
Reply #11

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Configure -> Scheduler the schedule 'Sensor.Timeout' should be at the top.

Gene has said he's working on the documentation, hopefully we can find out if Scheduler.IsScheduling is deprecate or not.
« Last Edit: October 29, 2016, 07:18:33 AM by mvdarend »

October 29, 2016, 07:21:00 PM
Reply #12

kevinvinv

  • ****
  • Information
  • Sr. Member
  • Posts: 196
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!

October 30, 2016, 05:32:53 AM
Reply #13

kevinvinv

  • ****
  • Information
  • Sr. Member
  • Posts: 196
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!!