Auto Off Automation

8 years 10 months ago #1712 by Cory
Auto Off Automation was created by Cory
I wrote an automation program to automatically turn off a light after 5 minutes.
/*
  This code is running one time when program is enabled
*/
public void Setup()  

{  
  
  EventHelper.ModuleChangedHandler((o, m, p) =>  
  {   
    // filter digital switch - change module address  
    if (m.Domain == Domains.MYSENSORS && m.Address == "N21S1" && p.Property == "Sensor.DigitalValue"){  
      DateTime t = DateTime.Now;
      
      // filter when switch on event   
      if (p.Value == "1") {  
        t = t.AddMinutes(5);
        Console.WriteLine("Starting 5 Minute Auto Off Timer");
        while (DateTime.Now < t) {
          Console.WriteLine("Sleeping 10 Seconds... {0} Minutes left", (t - DateTime.Now));
          System.Threading.Thread.Sleep(10000);  //Set an X Minute Delay
        }
        Console.WriteLine("Auto Off Entry Light");
        DriverHelper.ProcessCommad(Domains.MYSENSORS, "N21S1", "ControlOff", "");  //Entry Light
             
      }
      else {
        t = DateTime.Now;
      }
 
    }  
    return true;   
  });   

 
}  

/*
  This code is running periodically when program is enabled. 
  Cron job determine running period.
*/
public void Run()
{
}

I found that sleeping the thread for 5 minutes did not provide a very reliable automation. Has anyone else done an auto-off automation that works reliably?

Please Log in or Create an account to join the conversation.

8 years 10 months ago #1732 by EasyIoT
Replied by EasyIoT on topic Auto Off Automation

Cory wrote: I wrote an automation program to automatically turn off a light after 5 minutes.

/*
  This code is running one time when program is enabled
*/
public void Setup()  

{  
  
  EventHelper.ModuleChangedHandler((o, m, p) =>  
  {   
    // filter digital switch - change module address  
    if (m.Domain == Domains.MYSENSORS && m.Address == "N21S1" && p.Property == "Sensor.DigitalValue"){  
      DateTime t = DateTime.Now;
      
      // filter when switch on event   
      if (p.Value == "1") {  
        t = t.AddMinutes(5);
        Console.WriteLine("Starting 5 Minute Auto Off Timer");
        while (DateTime.Now < t) {
          Console.WriteLine("Sleeping 10 Seconds... {0} Minutes left", (t - DateTime.Now));
          System.Threading.Thread.Sleep(10000);  //Set an X Minute Delay
        }
        Console.WriteLine("Auto Off Entry Light");
        DriverHelper.ProcessCommad(Domains.MYSENSORS, "N21S1", "ControlOff", "");  //Entry Light
             
      }
      else {
        t = DateTime.Now;
      }
 
    }  
    return true;   
  });   

 
}  

/*
  This code is running periodically when program is enabled. 
  Cron job determine running period.
*/
public void Run()
{
}

I found that sleeping the thread for 5 minutes did not provide a very reliable automation. Has anyone else done an auto-off automation that works reliably?


Try to switch off light with cron. In event helper switch on and set time mark. Then in cron every minute check this mark if it's time to switch off.

Please Log in or Create an account to join the conversation.

Time to create page: 0.204 seconds

Forum latest

  • No posts to display.