Simulate Sunrise

logo

This rule template will create a rule that gradually increase a Dimmer or Color item to the target brightness and time over a configurable period. The target time can be set either with an item (such as one linked to the Sunrise End Time of the Astro binding), or a fixed time. Once the rule is created, you can add conditions manually to e.g. run it only on weekdays.

The rules will run every minute and calculate the brightness based on the current time, target time, desired duration & brightness so that no stateful timer is necessary. A limitation is that the sunrise period cannot span multiple days or cross midnight.

Configuration

Parameter Type Description
Target Time (DateTime Item) Item DateTime Item that holds the target time (for instance, linked to the Sunrise End Time channel of an Astro Sun Thing). Set either this or a fixed target time below.
Fixed Target Time Text Set a fixed target time - ignored if Target Time (DateTime Item) is set above.
Target Brightness Integer Brightness to reach at the target time (default 100).
Sunrise Duration Integer Duration of the sunrise in minutes (The brightness will be set to 0 at the start of the period and gradually every minute to the target brightness until the end). Default 60.
Brightness Item Item Dimmer or Color Item to use to control the brightness.
Color Prefix Text In case of a Color Item set above, prefix the command with the comma-separated Hue,Saturation components to send to the item (a separator comma and the brightness will be appended).

Changelog

Version 0.1

  • initial release

Resources

uid: ysc:simulate_sunrise
label: Simulate Sunrise
description: This rule will gradually increase a Dimmer or Color item to the target brightness and time over a configurable period.
configDescriptions:
 - name: itemTargetTime
   type: TEXT
   context: item
   label: Target Time (DateTime Item)
   defaultValue: ""
   required: false
   description: DateTime Item that holds the target time (for instance, linked to the Sunrise End Time channel of an Astro Sun Thing). Set either this or a fixed target time below.
 - name: fixedTargetTime
   type: TEXT
   context: time
   label: Fixed Target Time
   required: false
   description: Set a fixed target time - ignored if Target Time (DateTime Item) is set above.
 - name: targetBrightness
   type: INTEGER
   label: Target Brightness
   required: true
   defaultValue: 100
   description: Brightness to reach at the target time.
 - name: sunriseDuration
   type: INTEGER
   label: Sunrise Duration
   required: true
   defaultValue: 60
   description: Duration of the sunrise in minutes (The brightness will be set to 0 at the start of the period and gradually every minute to the target brightness until the end).
 - name: brightnessItem
   type: TEXT
   context: item
   label: Brightness Item
   required: true
   description: Dimmer or Color Item to use to control the brightness.
 - name: colorPrefix
   type: TEXT
   label: Color Prefix
   required: false
   defaultValue: ""
   description: In case of a Color Item set above, prefix the command with the comma-separated Hue,Saturation components to send to the item (a separator comma and the brightness will be appended).
triggers:
  - id: "1"
    configuration:
      cronExpression: 0 * * * * ? *
    type: timer.GenericCronTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    label: Calculate & set the target brightness
    description: Sets the brightness appropriately or do nothing if outside the sunrise time
    configuration:
      type: application/javascript
      script: >
        // set by the rule template

        var itemTargetTime = "${itemTargetTime}";

        var fixedTargetTime = "${fixedTargetTime}";

        var sunriseDuration = ${sunriseDuration};

        var targetBrightness = ${targetBrightness};

        var brightnessItem = "${brightnessItem}";

        var colorPrefix = "${colorPrefix}";


        var openhab = (typeof(require) === "function") ? require("@runtime") : {
          ir: ir, events: events
        };


        var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.rule." + this.ctx.ruleUID);


        // returns the number of minutes past midnight for a Date object

        function getMinutesPastMidnight(date) {
          return date.getHours() * 60 + date.getMinutes();
        }



        // returns the brightness to set at the current time (Date), given the target time (Date),

        // target brightness (int) & desired sunrise duration (int)


        function getBrightnessAtTime(currentTime, targetTime, targetBrightness, sunriseDuration) {
          var currentMinutes = getMinutesPastMidnight(now);
          var targetMinutes = getMinutesPastMidnight(targetTime);
          if (currentMinutes > targetMinutes) return null;
          if (currentMinutes < targetMinutes - sunriseDuration) return null;
          var minutesToGo = targetMinutes - currentMinutes;
          return parseInt(parseInt(targetBrightness) * ((sunriseDuration - minutesToGo) / sunriseDuration));
        }


        var now = new Date();

        var targetTime = null;


        if (itemTargetTime) {
          targetTime = new Date(openhab.ir.getItem(itemTargetTime).getState());
        } else if (fixedTargetTime.match(/\d\d:\d\d/)) {
          targetTime = new Date();
          targetTime.setHours(parseInt(fixedTargetTime.split(":")[0]));
          targetTime.setMinutes(parseInt(fixedTargetTime.split(":")[1]));
          targetTime.setSeconds(0);
        } else {
          logger.warn("Invalid target time");
        }


        if (targetTime != null) {
          var brightness = getBrightnessAtTime(now, targetTime, targetBrightness, sunriseDuration);
          if (brightness != null) {
            openhab.events.sendCommand(brightnessItem, (colorPrefix ? colorPrefix + "," : "") + brightness.toString());
          }
        }
    type: script.ScriptAction
3 Likes