[SOLVED] Dimmer Rule with exec Binding

OK…I get this error now:

Configuration model 'aufgang.rules' has errors, therefore ignoring it: [3,1]: mismatched input 'val' expecting 'when'

Post the whole rules file

rule "Slowly Dim Up"

val int timeoutSecs = 9 // 9 seconds for 15 minutes
var int dimLevel = 100 // Start from 100
var Timer_Up timer = null // Initialise timer to null

when

    Item GPIO4 changed to ON

then

    if (timer === null) { // If there is no timer
        logInfo("TIMER", "Starting dimmer loop")
        timer = createTimer(now.plusSeconds(0), [ |   //Starts immediately
            if (dimLevel == 0) { //If we have reached 0
                timer = null // cancel timer
            } else {
                dimLevel = dimLevel -1 // Decrease dimLevel by 1
                val int pwmValue = ((dimLevel * 1024) / 100).intValue // Scale dimLevel from 0-100 to 0-1024
                val commandString = "gpio pwm 1 " + pwmValue // Create command String
                logInfo("LOOP Command Line: ", commandString)
                executeCommandLine(commandString) // Execute command String
                timer.reschedule(now.plusSeconds(timeoutSecs)) // reschedule timer like a loop
            }
        ])
    }
end
val int timeoutSecs = 9 // 9 seconds for 15 minutes

var Timer_Up timer = null // Initialise timer to null

rule "Slowly Dim Up"
when
    Item GPIO4 changed to ON
then
    var int dimLevel = 100 // Start from 100
    if (timer === null) { // If there is no timer
        logInfo("TIMER", "Starting dimmer loop")
        timer = createTimer(now.plusSeconds(0), [ |   //Starts immediately
            if (dimLevel == 0) { //If we have reached 0
                timer = null // cancel timer
            } else {
                dimLevel = dimLevel -1 // Decrease dimLevel by 1
                val int pwmValue = ((dimLevel * 1024) / 100).intValue // Scale dimLevel from 0-100 to 0-1024
                val commandString = "gpio pwm 1 " + pwmValue // Create command String
                logInfo("LOOP Command Line: ", commandString)
                executeCommandLine(commandString) // Execute command String
                timer.reschedule(now.plusSeconds(timeoutSecs)) // reschedule timer like a loop
            }
        ])
    }
end

hmm Thank you. I’m going to try it.
Do I need to use unique names for the timer in both rules?

Not if they are in different files

I tried it this way and it looks like its working:

rule "Slowly Dim Up"
when
    Item GPIO4 changed to ON
then
    val int timeoutSecs = 9 // 9 seconds for 15 minutes
    var Timer_Up timer = null // Initialise timer to null
    var int dimLevel = 100 // Start from 100
    if (timer === null) { // If there is no timer
...

If you’re not using VSC editor to highlight syntax errors, you gotta keep an eye on your openhab.log to see if a rules file you just uploaded contains errors.

var Timer_Up timer = null
Syntax is var class-of-variable name-of-this-variable = whatever
In this case, timer class is actually Timer as well, case is important.
So that’s two errors, it would be
var Timer Timer_Up = null
or whatever.

Next, this part has become nonsense

rule "Slowly Dim Up"
...
    var Timer_Up timer = null // Initialise timer to null
    var int dimLevel = 100 // Start from 100
    if (timer === null) { // If there is no timer

We know it’s null, because we just set it that way…
It’s really worth reading and following along with the rule, and asking about things that are unclear if needed. You’re not going to get people posting you complete working error-free rules that do exactly what you want, you’ll need to understand enough to fix and modify them.

Having said all that, here is a suggestion. Assuming you never want to have dim-up and dim-down happening at the same time. Put BOTH your rules into one rules file as follows (and delete the spare file)

// Globals
var Timer dimTimer = null // Initialise timer to null
              // shared timer so that only one rule runs at a time 
val int timeoutSecs = 9 // 9 seconds for 15 minutes
              // convenient to control both rules with same setting
var int dimLevel = 0 // don't care what value it is
              // global so that it can be accessed inside timer

rule "Slowly Dim Up"
when
    Item GPIO4 changed to ON
then
    if (dimTimer === null) { // If there is no timer
        logInfo("TIMER", "Starting dimup loop")
        dimLevel = 100    // initial value
        dimTimer = createTimer(now.plusSeconds(0), [ |   //Starts immediately
            if (dimLevel == 0) { //If we have reached 0
                dimTimer = null // cancel timer
            } else {
                dimLevel = dimLevel -1 // Decrease dimLevel by 1
                val int pwmValue = ((dimLevel * 1024) / 100).intValue // Scale dimLevel from 0-100 to 0-1024
                val commandString = "gpio pwm 1 " + pwmValue // Create command String
                logInfo("LOOP Command Line: ", commandString)
                executeCommandLine(commandString) // Execute command String
                dimTimer.reschedule(now.plusSeconds(timeoutSecs)) // reschedule timer like a loop
            }
        ])
    }
end

rule "Slowly Dim Down"
when
    Item GPIO4 changed to OFF
then
    if (dimTimer === null) { // If there is no timer
        logInfo("TIMER", "Starting dim down loop")
        dimLevel = 0   // initialise
        dimTimer = createTimer(now.plusSeconds(0), [ |   //Starts immediately
            if (dimLevel == 100) { //If we have reached 100
                dimTimer = null // cancel timer
            } else {
                dimLevel = dimLevel +1 // Increase dimLevel by 1
                val int pwmValue = ((dimLevel * 1024) / 100).intValue // Scale dimLevel from 0-100 to 0-1024
                val commandString = "gpio pwm 1 " + pwmValue // Create command String
                logInfo("LOOP Command Line: ", commandString)
                executeCommandLine(commandString) // Execute command String
                dimTimer.reschedule(now.plusSeconds(timeoutSecs)) // reschedule timer like a loop
            }
        ])
    }
end

You need to check the rule triggers, and see if that is what you want.