Move From dsl Rule to Blockly

I am still have some old rules in dsl format in .rules files and slowly trying to alter them to the new blockly. but this one is an old one that i have found before years in the forum and i am wonder can someone make it for me as my Knowledges are limited for so difficult rule.

var Timer tShutters = null
// val java.util.concurrent.ThreadLocalRandom random = (new java.util.concurrent.ThreadLocalRandom)

rule "close Shutters"
  		 Channel 'astro:sun:local:set#event' triggered START
	   //Time cron "0 * * * * ?"
		tShutters?.cancel                                                       // cancel existing timer, if there is one
		val java.util.Random random = new java.util.Random
		val int randomTimes = random.nextInt(60) + 20                            // minimum 20 Seconds, maximum 80 Seconds
		tShutters = createTimer(now.plusSeconds(randomTimes),[ |
			val iGroup = gBlindsAstro.members.filter[r|(r.state as Number) < 100]    // all open shutters
			if(iGroup !== null && iGroup.size > 0) {                                               // any open Shutters left?
				logInfo("loggerName", iGroup.toString)
				val int randomTime = random.nextInt(60) + 20                    // minimum 20 Seconds, maximum 80 Seconds
				val int randomShutter = random.nextInt(iGroup.size)    		// from 1 to iGroup.size
				//val int randomShutter = random.nextInt(iGroup.size -1) +1       // from 1 to iGroup.size, to exclude an item from group
				iGroup.get(randomShutter).sendCommand(DOWN)                     // or 100 for absolute position
				iGroup.get(randomShutter).postUpdate(100)                       // ensure state is set to 100 immediately
				tShutters.reschedule(now.plusSeconds(randomTime))               // next close @ 

Thanks Nikos

I haven’t used Blockly enough to be able to help you, but there are some cases when you need to instead use a scripted action for advanced functionality. This may be one of them.

Instead of Blockly, choose “Rule DSL” and then paste everything between then and end into the script window.


Of course, by saying “this may not work,” I’m virtually guaranteeing that someone else will chime in to say, “here’s how to convert that rule to Blockly.” That’s actually what I’m aiming for. :wink:

This DSL rule relies on a “global variable”, tshutters.
Global variables are not available in any GUI entered rules.
But if the variable is not actually shared with other separate rules, there is a different technique available to ensure the variable persists between runs of the same rule.

1 Like