Rule Examples/en: различия между версиями

Обновление для соответствия новой версии исходной страницы.
(Обновление для соответствия новой версии исходной страницы.)
Строка 7: Строка 7:
For example, a rule can turn on a siren and a lamp if a motion sensor detects movement.  
For example, a rule can turn on a siren and a lamp if a motion sensor detects movement.  


<div class="mw-translate-fuzzy">
In the example, the motion sensor is connected to the "dry contact" input, control type "switch". The siren is connected to the built-in Wiren Board relay, and the lamp is connected to the relay box via Modbus. When the "dry contact" input (motion sensor output) is closed, "1" is supplied to the lamp and the relay, and "0" when it is off.
In the example, the motion sensor is connected to the "dry contact" input, control type "switch". The siren is connected to the built-in Wiren Board relay, and the lamp is connected to the relay box via Modbus. When the "dry contact" input (motion sensor output) is closed, "1" is supplied to the lamp and the relay, and "0" when it is off.
</div>


<div class="mw-translate-fuzzy">
The rule is triggered every time the control value "D1_IN" of the device "wb-gpio" is changed. The new value of this control is passed to the rule code as a variable newValue.
The rule is triggered every time the control value "D1_IN" of the device "wb-gpio" is changed. The new value of this control is passed to the rule code as a variable newValue.
</div>


<syntaxhighlight lang="ecmascript">
<syntaxhighlight lang="ecmascript">


<div class="mw-translate-fuzzy">
defineRule({
defineRule({
   whenChanged: "wb-gpio/D1_IN",
   whenChanged: "wb-gpio/D1_IN",
Строка 18: Строка 23:
dev["wb-gpio"]["Relay_2"] = newValue;
dev["wb-gpio"]["Relay_2"] = newValue;
dev["wb-mrm2_6"]["Relay 1"] = newValue;
dev["wb-mrm2_6"]["Relay 1"] = newValue;
</div>


   }
   }
Строка 37: Строка 43:
});
});


<div class="mw-translate-fuzzy">
defineRule("simple_switch", {
defineRule("simple_switch", {
   whenChanged: "simple_test/enabled",
   whenChanged: "simple_test/enabled",
Строка 42: Строка 49:
dev["wb-gpio"]["Relay_2"] = newValue;
dev["wb-gpio"]["Relay_2"] = newValue;
dev["wb-mrm2_6"]["Relay 1"] = newValue;
dev["wb-mrm2_6"]["Relay 1"] = newValue;
</div>


   }
   }
Строка 49: Строка 57:
=== Motion detection with timeout ===
=== Motion detection with timeout ===


<div class="mw-translate-fuzzy">
A motion detector with a "dry contact" output is connected to input D2. When motion is detected, it shorts D2 and GND and status "1" appears on the corresponding <code>wb-gpio/D2_IN</code> channel.
A motion detector with a "dry contact" output is connected to input D2. When motion is detected, it shorts D2 and GND and status "1" appears on the corresponding <code>wb-gpio/D2_IN</code> channel.
</div>


The lighting is connected via a built-in relay corresponding to the <code>wb-gpio/Relay_1</code> channel.
The lighting is connected via a built-in relay corresponding to the <code>wb-gpio/Relay_1</code> channel.


<div class="mw-translate-fuzzy">
The rule works like this:
The rule works like this:
* when movement appears, the light turns on. If a thirty-second "off" timer was previously started, this timer is disabled;
* when movement appears, the light turns on. If a thirty-second "off" timer was previously started, this timer is disabled;
* when motion is lost, a thirty second "off" timer is started. If he manages to reach the end, the light turns off.
* when motion is lost, a thirty second "off" timer is started. If he manages to reach the end, the light turns off.
</div>


<syntaxhighlight lang="ecmascript">
<syntaxhighlight lang="ecmascript">
Строка 61: Строка 73:
var motion_timer_1_id = null;
var motion_timer_1_id = null;


<div class="mw-translate-fuzzy">
defineRule("motion_detector_1", {
defineRule("motion_detector_1", {
     whenChanged: "wb-gpio/D2_IN",
     whenChanged: "wb-gpio/D2_IN",
Строка 77: Строка 90:
});
});
</syntaxhighlight>
</syntaxhighlight>
</div>


=== Creating similar rules ===
=== Creating similar rules ===
Строка 82: Строка 96:
If you need several such motion detectors, then in order not to copy the code, you can wrap the creation of the rule and variables in a function:
If you need several such motion detectors, then in order not to copy the code, you can wrap the creation of the rule and variables in a function:


<div class="mw-translate-fuzzy">
<syntaxhighlight lang="ecmascript">
<syntaxhighlight lang="ecmascript">
  function makeMotionDetector(name, timeout_ms, detector_control, relay_control) {
  function makeMotionDetector(name, timeout_ms, detector_control, relay_control) {
Строка 93: Строка 108:
                   clearTimeout(motion_timer_id);
                   clearTimeout(motion_timer_id);
               }
               }
</div>


               motion_timer_id = setTimeout(function() {
               <div class="mw-translate-fuzzy">
motion_timer_id = setTimeout(function() {
                   dev["wb-gpio"][relay_control] = false;
                   dev["wb-gpio"][relay_control] = false;
                   motion_timer_id = null;
                   motion_timer_id = null;
Строка 102: Строка 119:
   });
   });
}
}
</div>


makeMotionDetector("motion_detector_1", 20000, "EXT1_DR1", "EXT2_R3A1");
makeMotionDetector("motion_detector_1", 20000, "EXT1_DR1", "EXT2_R3A1");
Строка 127: Строка 145:
     date_start.setMinutes(30);
     date_start.setMinutes(30);


     // time point marking the end of the interval
     <div class="mw-translate-fuzzy">
// time point marking the end of the interval
     var date_end = new Date(date);
     var date_end = new Date(date);
     date_end.setHours(17);
     date_end.setHours(17);
Строка 150: Строка 169:
});
});
</syntaxhighlight>
</syntaxhighlight>
</div>


=== Rolling shutters ===
=== Rolling shutters ===
Строка 169: Строка 189:
   var relay_down_control = "Relay 2";
   var relay_down_control = "Relay 2";


   var timeout_s = 15;
   <div class="mw-translate-fuzzy">
var timeout_s = 15;
    
    
   // End of settings
   // End of settings
Строка 185: Строка 206:
         relay_up_timer_id = clearTimeout(relay_up_timer_id);  
         relay_up_timer_id = clearTimeout(relay_up_timer_id);  
       };
       };
</div>


       relay_up_timer_id = setTimeout(function() {
       <div class="mw-translate-fuzzy">
relay_up_timer_id = setTimeout(function() {
         return dev[relay_up_device][relay_up_control] = 0;
         return dev[relay_up_device][relay_up_control] = 0;
       }, timeout_s * 1000);
       }, timeout_s * 1000);
     }
     }
   });
   });
</div>


   defineRule("roller_shutter_down_on" + suffix, {
   <div class="mw-translate-fuzzy">
defineRule("roller_shutter_down_on" + suffix, {
     asSoonAs: function() {
     asSoonAs: function() {
       return dev[relay_down_device][relay_down_control];
       return dev[relay_down_device][relay_down_control];
Строка 206: Строка 231:
     }
     }
   });
   });
</div>


   defineRule("roller_shutter_both_on" + suffix, {
   <div class="mw-translate-fuzzy">
defineRule("roller_shutter_both_on" + suffix, {
     asSoonAs: function() {
     asSoonAs: function() {
       return dev[relay_up_device][relay_up_control] && dev[relay_down_device][relay_down_control];
       return dev[relay_up_device][relay_up_control] && dev[relay_down_device][relay_down_control];
Строка 215: Строка 242:
         relay_up_timer_id = clearTimeout(relay_up_timer_id);  
         relay_up_timer_id = clearTimeout(relay_up_timer_id);  
       };
       };
</div>


       if (relay_down_timer_id) {
       if (relay_down_timer_id) {
Строка 221: Строка 249:


        
        
       dev[relay_up_device][relay_up_control] = 0;
       <div class="mw-translate-fuzzy">
dev[relay_up_device][relay_up_control] = 0;
       dev[relay_down_device][relay_down_control] = 0;
       dev[relay_down_device][relay_down_control] = 0;
       log("Both roller shutter relays on, switching them off");
       log("Both roller shutter relays on, switching them off");
Строка 228: Строка 257:
})();
})();
</syntaxhighlight>
</syntaxhighlight>
</div>


An older version of the same script demonstrates the use of aliases:
An older version of the same script demonstrates the use of aliases:
Строка 292: Строка 322:
});
});


<div class="mw-translate-fuzzy">
defineRule("water_meter_1", {
defineRule("water_meter_1", {
     whenChanged: "wb-mcm8_29/Input 1 counter",
     whenChanged: "wb-mcm8_29/Input 1 counter",
Строка 301: Строка 332:
});
});
</syntaxhighlight>
</syntaxhighlight>
</div>


== Handling click counters ==
== Handling click counters ==
Строка 428: Строка 460:
         var co2_bad = newValue > 1001;
         var co2_bad = newValue > 1001;


         if (co2_good) {
         <div class="mw-translate-fuzzy">
if (co2_good) {
             dev[devName]["Green LED"] = true;
             dev[devName]["Green LED"] = true;
             dev[devName]["Red LED"] = false;
             dev[devName]["Red LED"] = false;
Строка 449: Строка 482:
=== Max Motion ===
=== Max Motion ===
"Max Motion" - the maximum value of the motion sensor for N time. Time from 1 to 60 seconds can be set in register 282. The default is 10 seconds. When the Max Motion value reaches 50, we check whether the room is sufficiently lit, if not, turn on the light. As soon as the Max Motion value drops below 50, turn off the light.
"Max Motion" - the maximum value of the motion sensor for N time. Time from 1 to 60 seconds can be set in register 282. The default is 10 seconds. When the Max Motion value reaches 50, we check whether the room is sufficiently lit, if not, turn on the light. As soon as the Max Motion value drops below 50, turn off the light.
</div>


<div class="mw-translate-fuzzy">
<div class="NavFrame">
<div class="NavFrame">
   <div class="NavContent">
   <div class="NavContent">
Строка 466: Строка 501:
});
});
</syntaxhighlight>
</syntaxhighlight>
</div>
</div>
</div>
</div>
</div>
Строка 578: Строка 614:




<div class="mw-translate-fuzzy">
defineRule("_system_track_vin", {
defineRule("_system_track_vin", {
     whenChanged: "wb-adc/Vin",
     whenChanged: "wb-adc/Vin",
Строка 588: Строка 625:
     }
     }
});
});
</div>






<div class="mw-translate-fuzzy">
defineRule("_system_dc_on", {
defineRule("_system_dc_on", {
   asSoonAs: function () {
   asSoonAs: function () {
Строка 599: Строка 638:
   }
   }
});
});
</div>


<div class="mw-translate-fuzzy">
defineRule("_system_dc_off", {
defineRule("_system_dc_off", {
   asSoonAs: function () {
   asSoonAs: function () {
Строка 608: Строка 649:
   }
   }
});
});
</div>


</syntaxhighlight>
</syntaxhighlight>
Строка 930: Строка 972:
Not cron-rules are used for schedules, but  the libschedule. The libschedule enables and disables rules, which, unlike cron rules, are executed continuously when enabled.
Not cron-rules are used for schedules, but  the libschedule. The libschedule enables and disables rules, which, unlike cron rules, are executed continuously when enabled.


<div class="mw-translate-fuzzy">
For example, we want the lighting to be on from 10 to 17h. The libschedule will follow the "turn on the lights" rule once a minute from 10 am to 17 PM.
For example, we want the lighting to be on from 10 to 17h. The libschedule will follow the "turn on the lights" rule once a minute from 10 am to 17 PM.
</div>


This means that even if the controller is running intermittently and missed the transition time between schedules (10 am), the controller will still turn on the lights at the first opportunity.
This means that even if the controller is running intermittently and missed the transition time between schedules (10 am), the controller will still turn on the lights at the first opportunity.
wb_editors
14 355

правок