more G-Labs products

Author Topic: Script to change switch based sensor state  (Read 1497 times)

March 02, 2015, 12:02:05 AM
Read 1497 times

ccesario

  • *
  • Information
  • Newbie
  • Posts: 9
Hello all,

To solve my question regarding my post (GPIO STATE - 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

March 02, 2015, 09:42:23 AM
Reply #1

dani

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

March 02, 2015, 11:45:41 AM
Reply #2

ccesario

  • *
  • Information
  • Newbie
  • Posts: 9
Yes yes, but is it possible to do this using script... right ?

Have you any example!?

Best regards

Carlos

March 02, 2015, 02:56:39 PM
Reply #3

dani

  • *****
  • Information
  • Global Moderator
  • Posts: 535
Which kind of script do you want to use ?
Wizars Script, JavaScript, Python, Ruby ?

March 02, 2015, 04:57:35 PM
Reply #4

ccesario

  • *
  • Information
  • Newbie
  • Posts: 9
JavaScript, Python, C#, either one :)

Carlos

March 02, 2015, 05:10:33 PM
Reply #5

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
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.
« Last Edit: March 02, 2015, 06:24:51 PM by bkenobi »

March 03, 2015, 12:39:00 PM
Reply #6

ccesario

  • *
  • Information
  • Newbie
  • Posts: 9
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

March 03, 2015, 02:27:41 PM
Reply #7

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
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

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_overview.php

Cheers,
g.