wb_editors
14 355
правок
Matveevrj (обсуждение | вклад) |
FuzzyBot (обсуждение | вклад) (Обновление для соответствия новой версии исходной страницы.) |
||
(не показано 14 промежуточных версий 1 участника) | |||
Строка 1: | Строка 1: | ||
<languages/> | <languages/> | ||
{{DISPLAYTITLE: Rule Examples}} | |||
=== Control tracking === | === Control tracking === | ||
Строка 13: | Строка 14: | ||
<syntaxhighlight lang="ecmascript"> | <syntaxhighlight lang="ecmascript"> | ||
defineRule({ | defineRule({ | ||
whenChanged: "wb-gpio/D1_IN", | whenChanged: "wb-gpio/D1_IN", | ||
then: function (newValue, devName, cellName) { | then: function (newValue, devName, cellName) { | ||
dev["wb-gpio | dev["wb-gpio/Relay_2"] = newValue; | ||
dev["wb-mrm2_6 | dev["wb-mrm2_6/Relay 1"] = newValue; | ||
} | } | ||
Строка 39: | Строка 38: | ||
}); | }); | ||
defineRule("simple_switch", { | defineRule("simple_switch", { | ||
whenChanged: "simple_test/enabled", | whenChanged: "simple_test/enabled", | ||
then: function (newValue, devName, cellName) { | then: function (newValue, devName, cellName) { | ||
dev["wb-gpio | dev["wb-gpio/Relay_2"] = newValue; | ||
dev["wb-mrm2_6 | dev["wb-mrm2_6/Relay 1"] = newValue; | ||
} | } | ||
Строка 53: | Строка 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 | |||
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 | * 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 | * 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"> | ||
Строка 69: | Строка 62: | ||
var motion_timer_1_id = null; | var motion_timer_1_id = null; | ||
defineRule("motion_detector_1", { | defineRule("motion_detector_1", { | ||
whenChanged: "wb-gpio/D2_IN", | whenChanged: "wb-gpio/D2_IN", | ||
then: function (newValue, devName, cellName) { | then: function (newValue, devName, cellName) { | ||
if (newValue) { | if (newValue) { | ||
dev["wb-gpio | 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 | 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); | ||
Строка 86: | Строка 78: | ||
}); | }); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Creating similar rules === | === Creating similar rules === | ||
Строка 92: | Строка 83: | ||
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: | ||
<syntaxhighlight lang="ecmascript"> | <syntaxhighlight lang="ecmascript"> | ||
function makeMotionDetector(name, timeout_ms, detector_control, relay_control) { | function makeMotionDetector(name, timeout_ms, detector_control, relay_control) { | ||
Строка 100: | Строка 90: | ||
then: function(newValue, devName, cellName) { | then: function(newValue, devName, cellName) { | ||
if (!newValue) { | if (!newValue) { | ||
dev["wb-gpio" | dev["wb-gpio/relay_control"] = true; | ||
if (motion_timer_id) { | if (motion_timer_id) { | ||
clearTimeout(motion_timer_id); | clearTimeout(motion_timer_id); | ||
} | } | ||
motion_timer_id = setTimeout(function() { | |||
motion_timer_id = setTimeout(function() { | dev["wb-gpio/relay_control"] = false; | ||
dev["wb-gpio" | |||
motion_timer_id = null; | motion_timer_id = null; | ||
}, timeout_ms); | }, timeout_ms); | ||
Строка 115: | Строка 103: | ||
}); | }); | ||
} | } | ||
makeMotionDetector("motion_detector_1", 20000, "EXT1_DR1", "EXT2_R3A1"); | makeMotionDetector("motion_detector_1", 20000, "EXT1_DR1", "EXT2_R3A1"); | ||
Строка 141: | Строка 128: | ||
date_start.setMinutes(30); | date_start.setMinutes(30); | ||
// time point marking the end of the interval | |||
// 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: | Строка 136: | ||
if ((date > date_start) && (date < date_end)) { | if ((date > date_start) && (date < date_end)) { | ||
if (newValue) { | if (newValue) { | ||
dev["wb-gpio | dev["wb-gpio/EXT1_R3A1"] = 1; | ||
if (motion_timer_1_id) { | if (motion_timer_1_id) { | ||
Строка 157: | Строка 143: | ||
motion_timer_1_id = setTimeout(function () { | motion_timer_1_id = setTimeout(function () { | ||
dev["wb-gpio | 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); | ||
Строка 165: | Строка 151: | ||
}); | }); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Rolling shutters === | === Rolling shutters === | ||
Строка 185: | Строка 170: | ||
var relay_down_control = "Relay 2"; | var relay_down_control = "Relay 2"; | ||
var timeout_s = 15; | |||
var timeout_s = 15; | |||
// End of settings | // End of settings | ||
Строка 196: | Строка 180: | ||
defineRule( "roller_shutter_up_on" + suffix, { | defineRule( "roller_shutter_up_on" + suffix, { | ||
asSoonAs: function() { | asSoonAs: function() { | ||
return dev[relay_up_device | return dev["relay_up_device/relay_up_control"]; | ||
}, | }, | ||
then: function () { | then: function () { | ||
Строка 202: | Строка 186: | ||
relay_up_timer_id = clearTimeout(relay_up_timer_id); | relay_up_timer_id = clearTimeout(relay_up_timer_id); | ||
}; | }; | ||
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 | |||
}, timeout_s * 1000); | }, timeout_s * 1000); | ||
} | } | ||
}); | }); | ||
defineRule("roller_shutter_down_on" + suffix, { | |||
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]; | ||
Строка 223: | Строка 203: | ||
relay_down_timer_id = setTimeout(function() { | relay_down_timer_id = setTimeout(function() { | ||
dev[relay_down_device | dev["relay_down_device/relay_down_control"] = 0; | ||
}, timeout_s * 1000); | }, timeout_s * 1000); | ||
} | } | ||
}); | }); | ||
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 | return dev[relay_up_device][relay_up_control] && dev["relay_down_device/relay_down_control"]; | ||
}, | }, | ||
then: function () { | then: function () { | ||
Строка 238: | Строка 216: | ||
relay_up_timer_id = clearTimeout(relay_up_timer_id); | relay_up_timer_id = clearTimeout(relay_up_timer_id); | ||
}; | }; | ||
if (relay_down_timer_id) { | if (relay_down_timer_id) { | ||
Строка 245: | Строка 222: | ||
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 | |||
log("Both roller shutter relays on, switching them off"); | log("Both roller shutter relays on, switching them off"); | ||
} | } | ||
Строка 253: | Строка 229: | ||
})(); | })(); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
An older version of the same script demonstrates the use of aliases: | An older version of the same script demonstrates the use of aliases: | ||
Строка 318: | Строка 293: | ||
}); | }); | ||
defineRule("water_meter_1", { | defineRule("water_meter_1", { | ||
whenChanged: "wb-mcm8_29/Input 1 counter", | whenChanged: "wb-mcm8_29/Input 1 counter", | ||
then: function(newValue, devName, cellName) { | then: function(newValue, devName, cellName) { | ||
if(newValue){ | if(newValue){ | ||
dev["water_meters | 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. | ||
} | } | ||
} | } | ||
}); | }); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Handling click counters == | == Handling click counters == | ||
Строка 456: | Строка 429: | ||
var co2_bad = newValue > 1001; | var co2_bad = newValue > 1001; | ||
if (co2_good) { | |||
if (co2_good) { | dev["devName/Green LED"] = true; | ||
dev[devName | dev["devName/Red LED"] = false; | ||
dev[devName | dev["devName/LED Period (s)"] = 10; | ||
dev[devName | |||
} | } | ||
if (co2_middle) { | if (co2_middle) { | ||
dev[devName | dev["devName/Green LED"] = true; | ||
dev[devName | dev["devName/Red LED"] = true; | ||
dev[devName | dev["devName/LED Period (s)"] = 5; | ||
} | } | ||
if (co2_bad) { | if (co2_bad) { | ||
dev[devName | dev["devName/Green LED"] = false; | ||
dev[devName | dev["devName/Red LED"] = true; | ||
dev[devName | dev["devName/LED Period (s)"] = 1; | ||
} | } | ||
} | } | ||
Строка 478: | Строка 450: | ||
=== 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 class="NavFrame"> | <div class="NavFrame"> | ||
<div class="NavContent"> | <div class="NavContent"> | ||
Строка 488: | Строка 458: | ||
then: function(newValue, devName, cellName) { | then: function(newValue, devName, cellName) { | ||
if (newValue > 50) { | if (newValue > 50) { | ||
if (dev["wb-msw-v3_97 | if (dev["wb-msw-v3_97/Illuminance"] < 50) { | ||
dev["wb-mr3_11 | dev["wb-mr3_11/K1"] = true; | ||
} | } | ||
} else { | } else { | ||
dev["wb-mr3_11 | dev["wb-mr3_11/K1"] = false; | ||
} | } | ||
} | } | ||
}); | }); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</div> | </div> | ||
</div> | </div> | ||
Строка 610: | Строка 579: | ||
defineRule("_system_track_vin", { | defineRule("_system_track_vin", { | ||
whenChanged: "wb-adc/Vin", | whenChanged: "wb-adc/Vin", | ||
then: function() { | then: function() { | ||
if (dev["wb-adc | if (dev["wb-adc/Vin"] < dev["wb-adc/BAT"] ) { | ||
dev["power_status | dev["power_status/Vin"] = 0; | ||
} else { | } else { | ||
dev["power_status | dev["power_status/Vin"] = dev["wb-adc/Vin"] ; | ||
} | } | ||
} | } | ||
}); | }); | ||
defineRule("_system_dc_on", { | defineRule("_system_dc_on", { | ||
asSoonAs: function () { | asSoonAs: function () { | ||
return dev["wb-adc | return dev["wb-adc/Vin"] > dev["wb-adc/BAT"]; | ||
}, | }, | ||
then: function () { | then: function () { | ||
dev["power_status | dev["power_status/working on battery"] = false; | ||
} | } | ||
}); | }); | ||
defineRule("_system_dc_off", { | defineRule("_system_dc_off", { | ||
asSoonAs: function () { | asSoonAs: function () { | ||
return dev["wb-adc | return dev["wb-adc/Vin"] <= dev["wb-adc/BAT"]; | ||
}, | }, | ||
then: function () { | then: function () { | ||
dev["power_status | dev["power_status/working on battery"] = true; | ||
} | } | ||
}); | }); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Строка 968: | Строка 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 | |||
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. |