Движок правил wb-rules: различия между версиями

Строка 96: Строка 96:
});
});


</syntaxhighlight>
=== Активация правила только в определённое время ===
Правило как в предыдущем разделе, но выполняется только с 9:30 до 17:10 по UTC.
<syntaxhighlight lang="ecmascript">
var motion_timer_1_timeout_ms = 5 * 1000;
var motion_timer_1_id = null;
defineRule("motion_detector_1", {
  whenChanged: "wb-gpio/A1_IN",
  then: function (newValue, devName, cellName) {
    var date = new Date();
    // time point marking the beginning of the interval
    // i.e. "today, at HH:MM". All dates are in UTC!
    var date_start = new Date(date);
    date_start.setHours(9);
    date_start.setMinutes(30);
    // time point marking the end of the interval
    var date_end = new Date(date);
    date_end.setHours(17);
    date_end.setMinutes(10);
   
    // if time is between 09:30 and 17:10 UTC
    if ((date > date_start) && (date < date_end)) {
      if (newValue) {
          dev["wb-gpio"]["EXT1_R3A1"] = 1;
 
          if (motion_timer_1_id) {
            clearTimeout(motion_timer_1_id);
        }
 
          motion_timer_1_id = setTimeout(function () {
            dev["wb-gpio"]["EXT1_R3A1"] = 0;           
          }, motion_timer_1_timeout_ms);             
      }
    }
  }
});
</syntaxhighlight>
</syntaxhighlight>