Blockly loop and timer behavior changed from 3.4 ot 4.0? Loops do not work anymore

The idea is to slowly increase the brightness of the lamp over a period of 30 minutes. My original code, that worked in OH 3.x, was based upon a “Repeat-while” loop that would have delay of few seconds between each repeat. I read somewhere that using “wait” inside the loop is not recommended. Therefore I ended up with this structure whre the “repeat while” loop wraps around a delay timer.

I am not a coder so I do not quite understand what “this creates the same time three times” means in this context. Please explain.

Ok, understood. What you must not do is to stay in a rule for 30min. Therefore you need to increase the value via a timer, which is the reoccurring way that replaces you loop. In that timer you use a value that is stored in a shared rule cache variable:

Retrieve it, increase it, use and then save it again in that “stored value”.

Here is an example that use it to remember the last color that was set

In you example you can use it similarly only that additionally also increase the value.

1 Like

Thanks for the help. I ditched the loops totally and rewrote the wakeuplight. Now it runs once, stores its states in value storages and exits. The script is run once a minute until it has completed the wakeup cycle.

var comma, hsb, brightness, shutdown_timer, hue, saturation;


comma = ',';
if ((cache.shared.exists('hue_storage') === false) == true) {
  hsb = ' ';
  hue = 1;
  cache.shared.put('hue_storage', hue);
  console.info(hue);
  saturation = 100;
  cache.shared.put('saturation_storage', saturation);
  brightness = 0;
  cache.shared.put('brightness_storage', brightness);
  hsb += String(hue);
  hsb += String(comma);
  hsb += String(saturation);
  hsb += String(comma);
  hsb += String(brightness);
  items.getItem('Timon_yovalo_colour_data').sendCommand(hsb);
  console.info('Dawn started, H, S and B reset to default values');
} else if (hue < 40) {
  hsb = ' ';
  hue = (cache.shared.get('hue_storage'));
  hue = (typeof hue === 'number' ? hue : 0) + 1;
  cache.shared.put('hue_storage', hue);
  saturation = (cache.shared.get('saturation_storage'));
  saturation = (typeof saturation === 'number' ? saturation : 0) + -1;
  cache.shared.put('saturation_storage', saturation);
  brightness = (cache.shared.get('brightness_storage'));
  brightness = (typeof brightness === 'number' ? brightness : 0) + 2;
  cache.shared.put('brightness_storage', brightness);
  hsb += String(hue);
  hsb += String(comma);
  hsb += String(saturation);
  hsb += String(comma);
  hsb += String(brightness);
  items.getItem('Timon_yovalo_colour_data').sendCommand(hsb);
} else if (hue == 40 && brightness >= 58) {
  brightness = 1;
  hue = 50;
  cache.shared.put('hue_storage', hue);
  items.getItem('Timon_yovalo_temp_value').sendCommand(brightness);
  items.getItem('Timon_yovalo_bright_value').sendCommand(brightness);
  cache.shared.put('brightness_storage', brightness);
} else if (hue >= 40 && brightness < 100) {
  brightness = (cache.shared.get('brightness_storage'));
  brightness = (typeof brightness === 'number' ? brightness : 0) + 4;
  items.getItem('Timon_yovalo_bright_value').sendCommand(brightness);
  cache.shared.put('brightness_storage', brightness);
} else if (brightness >= 100 && (cache.shared.exists('shutdown_timer_storage') === false) == true) {
  shutdown_timer = 30;
  cache.shared.put('shutdown_timer_storage', shutdown_timer);
} else if (brightness >= 100 && (cache.shared.get('shutdown_timer_storage')) > 0) {
  shutdown_timer = (cache.shared.get('shutdown_timer_storage'));
  shutdown_timer = (typeof shutdown_timer === 'number' ? shutdown_timer : 0) + -1;
  cache.shared.put('shutdown_timer_storage', shutdown_timer);
  cache.shared.put('brightness_storage', brightness);
} else {
  hsb = ' ';
  hue = 1;
  saturation = 100;
  brightness = 0;
  items.getItem('Timon_yovalo_bright_value').sendCommand(brightness);
  cache.shared.put('hue_storage', hue);
  cache.shared.put('saturation_storage', saturation);
  cache.shared.put('brightness_storage', brightness);
  hsb += String(hue);
  hsb += String(comma);
  hsb += String(saturation);
  hsb += String(comma);
  hsb += String(brightness);
  console.info('Suhrise is over, H, S  and B reset');
}

1 Like