HomeGenie Forum

General Category => General Discussion => Topic started by: ccesario on March 02, 2015, 12:02:05 AM

Title: Script to change switch based sensor state
Post by: ccesario on March 02, 2015, 12:02:05 AM
Hello all,

To solve my question regarding my post (GPIO STATE - http://www.homegenie.it/forum/index.php?topic=774.0 (http://www.homegenie.it/forum/index.php?topic=774.0)), as sugested by Wibo I've configured GPIO as INPUT to use a voltage sensor. It is working as expected.

My doubt now is how can I script it to change my switch based on sensor state?

Eg.

When Sensor1 is ON Switch1 is changed to ON
and
When Sensor1 is OFF Switch1 is changed to OFF

I've tried search some script examples, but no success.

Could someone please inform me how to do it? Is is the correct way to do it?

Best regards

Carlos
Title: Re: Script to change switch based sensor state
Post by: dani on March 02, 2015, 09:42:23 AM
You can use 2 wizard scripts to do that, it's depend on which value return your sensor ?
You have to specify the conditions and the actions in new automations programs.

Cheers
Dani
Title: Re: Script to change switch based sensor state
Post by: ccesario on March 02, 2015, 11:45:41 AM
Yes yes, but is it possible to do this using script... right ?

Have you any example!?

Best regards

Carlos
Title: Re: Script to change switch based sensor state
Post by: dani on March 02, 2015, 02:56:39 PM
Which kind of script do you want to use ?
Wizars Script, JavaScript, Python, Ruby ?
Title: Re: Script to change switch based sensor state
Post by: ccesario on March 02, 2015, 04:57:35 PM
JavaScript, Python, C#, either one :)

Carlos
Title: Re: Script to change switch based sensor state
Post by: bkenobi on March 02, 2015, 05:10:33 PM
Wizard script is easiest to describe, but I always opt for C#.  The first step is to set your GPIO input and output pins to modules so you can reference them.

Code: [Select]
Trigger:
Navigate to the correct GPIO and select ON
Run code:
Navigate to the Switch1 and select ON

For OFF, just do the same with the opposite command.

For C#:
Trigger: run when evaluates true
Code: [Select]
return true;Run:
Code: [Select]
When.ModuleParameterIsChanging((module, parameter) =>
{
  if (module.Instance.Name=="GPIO_sensor" && parameter.Name=="Status.Level" && parameter.Value=="1")
  {
    Modules.WithName("GPIO_switch").On();
  }
  else if (module.Instance.Name=="GPIO_sensor" && parameter.Name=="Status.Level" && parameter.Value=="0")
  {
    Modules.WithName("GPIO_switch").Off();
  }
  return true;
});
Program.GoBackground();

When I perform this type of task, I usually use the "When.ModuleParameterIsChanging" code.  However, since you are looking for a specific GPIO state change, you should be able to add that to the trigger side.  I do not know how the 3 approaches compare as far as computing overhead.
Title: Re: Script to change switch based sensor state
Post by: ccesario on March 03, 2015, 12:39:00 PM
Hello bkenobi,

The script it worked as expected, thanks by information!!!

One more question, this code is writed in C#, is there any documentation or code example   this code using python, js ?  Can I use

When.ModuleParameterIsChanging((module, parameter) => {
   js or python code ?
}

Or this class can be write only in C# ?


Thanks

Carlos
Title: Re: Script to change switch based sensor state
Post by: Gene on March 03, 2015, 02:27:41 PM
This is an example using the Program.Setup from Python, but will also work with any other helper class:

http://www.homegenie.it/forum/index.php?topic=135.0 (http://www.homegenie.it/forum/index.php?topic=135.0)

as for Javascript you can see following code:

Code: [Select]
hg.When.ModuleParameterChange(function(module, parameter){
  if (module.Instance.Domain == 'RaspberryPi.Gpio' && parameter.Is("Status.Level") && parameter.DecimalValue > 0)
  {
    // do something here
  }
  return true;
});

the code above was taken from the RaspberryPi->Eden OLED menu app included in HG.
More info at:

http://www.homegenie.it/docs/automation_getstarted.php (http://www.homegenie.it/docs/automation_getstarted.php)
http://www.homegenie.it/docs/automation_overview.php (http://www.homegenie.it/docs/automation_overview.php)

Cheers,
g.