Перейти к содержанию

Навигация

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

Обновление для соответствия новой версии исходной страницы.
Метки: правка с мобильного устройства правка из мобильной версии
(Обновление для соответствия новой версии исходной страницы.)
 
(не показано 18 промежуточных версий 2 участников)
Строка 1: Строка 1:
{{DISPLAYTITLE: Примеры правил}}
<languages/>
<languages/>
{{DISPLAYTITLE: Rule Examples}}
=== Control tracking ===
=== Control tracking ===


Строка 7: Строка 8:
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.  


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.


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.


<syntaxhighlight lang="ecmascript">
<syntaxhighlight lang="ecmascript">
Строка 16: Строка 17:
   whenChanged: "wb-gpio/D1_IN",
   whenChanged: "wb-gpio/D1_IN",
   then: function (newValue, devName, cellName) {
   then: function (newValue, devName, cellName) {
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;


   }
   }
Строка 40: Строка 41:
   whenChanged: "simple_test/enabled",
   whenChanged: "simple_test/enabled",
   then: function (newValue, devName, cellName) {
   then: function (newValue, devName, cellName) {
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;


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


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.


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.


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.


<syntaxhighlight lang="ecmascript">
<syntaxhighlight lang="ecmascript">
Строка 65: Строка 66:
     then: function (newValue, devName, cellName) {
     then: function (newValue, devName, cellName) {
         if (newValue) {
         if (newValue) {
             dev["wb-gpio"]["Relay_1"] = true;
             dev["wb-gpio/Relay_1"] = true;
             if (motion_timer_1_id) {
             if (motion_timer_1_id) {
                 clearTimeout(motion_timer_1_id);
                 clearTimeout(motion_timer_1_id);
             }
             }
             motion_timer_1_id = setTimeout(function () {
             motion_timer_1_id = setTimeout(function () {
                 dev["wb-gpio"]["Relay_1"] = false;
                 dev["wb-gpio/Relay_1"] = false;
                 motion_timer_1_id = null;
                 motion_timer_1_id = null;
             }, motion_timer_1_timeout_ms);
             }, motion_timer_1_timeout_ms);
Строка 89: Строка 90:
       then: function(newValue, devName, cellName) {
       then: function(newValue, devName, cellName) {
           if (!newValue) {
           if (!newValue) {
               dev["wb-gpio"][relay_control] = true;
               dev["wb-gpio/relay_control"] = true;
               if (motion_timer_id) {
               if (motion_timer_id) {
                   clearTimeout(motion_timer_id);
                   clearTimeout(motion_timer_id);
Строка 95: Строка 96:


               motion_timer_id = setTimeout(function() {
               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;
               }, timeout_ms);
               }, timeout_ms);
Строка 135: Строка 136:
     if ((date > date_start) && (date < date_end)) {
     if ((date > date_start) && (date < date_end)) {
       if (newValue) {
       if (newValue) {
           dev["wb-gpio"]["EXT1_R3A1"] = 1;
           dev["wb-gpio/EXT1_R3A1"] = 1;
    
    
           if (motion_timer_1_id) {
           if (motion_timer_1_id) {
Строка 142: Строка 143:
    
    
           motion_timer_1_id = setTimeout(function () {
           motion_timer_1_id = setTimeout(function () {
             dev["wb-gpio"]["EXT1_R3A1"] = 0;             
             dev["wb-gpio/EXT1_R3A1"] = 0;             
             motion_timer_1_id = null;
             motion_timer_1_id = null;
           }, motion_timer_1_timeout_ms);               
           }, motion_timer_1_timeout_ms);               
Строка 179: Строка 180:
   defineRule( "roller_shutter_up_on" + suffix, {
   defineRule( "roller_shutter_up_on" + suffix, {
   asSoonAs: function() {
   asSoonAs: function() {
     return dev[relay_up_device][relay_up_control];
     return dev["relay_up_device/relay_up_control"];
   },
   },
     then: function () {
     then: function () {
Строка 187: Строка 188:


       relay_up_timer_id = setTimeout(function() {
       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);
     }
     }
Строка 202: Строка 203:
        
        
       relay_down_timer_id = setTimeout(function() {
       relay_down_timer_id = setTimeout(function() {
         dev[relay_down_device][relay_down_control] = 0;
         dev["relay_down_device/relay_down_control"] = 0;
       }, timeout_s * 1000);
       }, timeout_s * 1000);
     }
     }
Строка 209: Строка 210:
   defineRule("roller_shutter_both_on" + suffix, {
   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"];
     },
     },
     then: function () {
     then: function () {
Строка 222: Строка 223:
        
        
       dev[relay_up_device][relay_up_control] = 0;
       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");
     }
     }
Строка 296: Строка 297:
     then: function(newValue, devName, cellName) {
     then: function(newValue, devName, cellName) {
       if(newValue){
       if(newValue){
       dev["water_meters"]["water_meter_1"] = ((parseInt(newValue) - counterCorrection) * inpulseValue) + meterCorrection; // We multiply the value of the counter by the number of liters / pulse and add the correction value.
       dev["water_meters/water_meter_1"] = ((parseInt(newValue) - counterCorrection) * inpulseValue) + meterCorrection; // We multiply the value of the counter by the number of liters / pulse and add the correction value.
       }
       }
     }
     }
Строка 411: Строка 412:
== Sensor MSW v.3 ==
== Sensor MSW v.3 ==


When connecting the WB-MSW v.3 sensor to the Wiren Board controller, it is possible to create interesting scenarios using data from the sensor. For example, turn on the light when moving, signal with LEDs when the CO2 or VOC value is exceeded, turn on the air conditioner if it is hot or the air humidifier if the air is too dry. Rules are created individually for tasks. Here we will give some examples to understand the principle of working with the sensor. More examples of writing rules can be found in the '''[[Движок правил wb-rules]]''' documentation.
When connecting the WB-MSW v.3 sensor to the Wiren Board controller, it is possible to create interesting scenarios using data from the sensor. For example, turn on the light when moving, signal with LEDs when the CO2 or VOC value is exceeded, turn on the air conditioner if it is hot or the air humidifier if the air is too dry. Rules are created individually for tasks. Here we will give some examples to understand the principle of working with the sensor. More examples of writing rules can be found in the '''[[Движок правил wb-rules | Rules engine wb-rules]]''' documentation.


=== CO2 ===
=== CO2 ===
Строка 429: Строка 430:


         if (co2_good) {
         if (co2_good) {
             dev[devName]["Green LED"] = true;
             dev["devName/Green LED"] = true;
             dev[devName]["Red LED"] = false;
             dev["devName/Red LED"] = false;
             dev[devName]["LED Period (s)"] = 10;
             dev["devName/LED Period (s)"] = 10;
         }
         }
         if (co2_middle) {
         if (co2_middle) {
             dev[devName]["Green LED"] = true;
             dev["devName/Green LED"] = true;
             dev[devName]["Red LED"] = true;
             dev["devName/Red LED"] = true;
             dev[devName]["LED Period (s)"] = 5;
             dev["devName/LED Period (s)"] = 5;
         }
         }
         if (co2_bad) {
         if (co2_bad) {
             dev[devName]["Green LED"] = false;
             dev["devName/Green LED"] = false;
             dev[devName]["Red LED"] = true;
             dev["devName/Red LED"] = true;
             dev[devName]["LED Period (s)"] = 1;
             dev["devName/LED Period (s)"] = 1;
         }
         }
     }
     }
Строка 457: Строка 458:
     then: function(newValue, devName, cellName) {
     then: function(newValue, devName, cellName) {
         if (newValue > 50) {
         if (newValue > 50) {
             if (dev["wb-msw-v3_97"]["Illuminance"] < 50) {
             if (dev["wb-msw-v3_97/Illuminance"] < 50) {
                 dev["wb-mr3_11"]["K1"] = true;
                 dev["wb-mr3_11/K1"] = true;
             }
             }
         } else {
         } else {
             dev["wb-mr3_11"]["K1"] = false;
             dev["wb-mr3_11/K1"] = false;
         }
         }
     }
     }
Строка 581: Строка 582:
     whenChanged: "wb-adc/Vin",
     whenChanged: "wb-adc/Vin",
     then: function() {
     then: function() {
         if (dev["wb-adc"]["Vin"] < dev["wb-adc"]["BAT"] ) {
         if (dev["wb-adc/Vin"] < dev["wb-adc/BAT"] ) {
             dev["power_status"]["Vin"] = 0;
             dev["power_status/Vin"] = 0;
         } else {
         } else {
             dev["power_status"]["Vin"] = dev["wb-adc"]["Vin"] ;
             dev["power_status/Vin"] = dev["wb-adc/Vin"] ;
         }
         }
     }
     }
Строка 593: Строка 594:
defineRule("_system_dc_on", {
defineRule("_system_dc_on", {
   asSoonAs: function () {
   asSoonAs: function () {
     return  dev["wb-adc"]["Vin"] > dev["wb-adc"]["BAT"];
     return  dev["wb-adc/Vin"] > dev["wb-adc/BAT"];
   },
   },
   then: function () {
   then: function () {
     dev["power_status"]["working on battery"] = false;
     dev["power_status/working on battery"] = false;
   }
   }
});
});
Строка 602: Строка 603:
defineRule("_system_dc_off", {
defineRule("_system_dc_off", {
   asSoonAs: function () {
   asSoonAs: function () {
     return  dev["wb-adc"]["Vin"] <= dev["wb-adc"]["BAT"];
     return  dev["wb-adc/Vin"] <= dev["wb-adc/BAT"];
   },
   },
   then: function () {
   then: function () {
     dev["power_status"]["working on battery"] = true;
     dev["power_status/working on battery"] = true;
   }
   }
});
});
Строка 930: Строка 931:
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.


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.


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

правок