Setting variable timer using a slider (dimmer)

I have this rule to automatically turn off a switch after turn on. I’m looking for a way to set up a slider to dynamically set the minutes to turn off for every on. I want the slider to be between 30 and 180 minutes.
Any ideas?

my rule:

var Timer timer = null
rule "Timer Demo"
when
    Item The_blue_Led received command
then
    if (receivedCommand == ON) {
        if (timer === null) {
            // first ON command, so create a timer to turn the light off again
            timer = createTimer(now.plusMinutes(30)) [|
                sendCommand(The_blue_Led, OFF)
            ]
        } else {
            // subsequent ON command, so reschedule the existing timer
            timer.reschedule(now.plusSeconds(30))
        }
    } else if (receivedCommand == OFF) {
        // remove any previously scheduled timer
        if (timer !== null) {
            timer.cancel
            timer = null
            
        }
    }

Hi

That sounds simple enough.

You just need a number Item to store the valve and allow you to change it in a UI.

(Don’t forget to use some kind of persistent to restore the value after a reboot)

Then poll that item during the rule.

New Item might be called “LED_Duration”

var Timer timer = null
rule "Timer Demo"
when
    Item The_blue_Led received command
then

var duration = (LED_Duration.state as Number).intValue // so the value is available to all timers

    if (receivedCommand == ON) {
        if (timer === null) {
            // first ON command, so create a timer to turn the light off again

            timer = createTimer(now.plusMinutes(duration)) [|
                sendCommand(The_blue_Led, OFF)
            ]
        } else {
            // subsequent ON command, so reschedule the existing timer
            timer.reschedule(now.plusSeconds(duration))
        }
    } else if (receivedCommand == OFF) {
        // remove any previously scheduled timer
        if (timer !== null) {
            timer.cancel
            timer = null
            
        }
    }
end

Updated rule

@rossko57

Thanks for the advice, as always :smile:

1 Like

Note that the now() functions require integers, Items states are not by default integers.
Example

Thank you very much. it’s working.
The slider only sets between 0-100 where i need 30 to 120. I added 30 to the var duration and I get 30 to 130 which is close enough. all I need now is to figure out how to display the calculated number…

Are you using a sitemap based UI? Recent versions allow setting of minValue maxValue in the Slider widget

1 Like

@Tboul
I was going to ask something similar about HabPanel, as you can set minimum & maximum values, as well as Steps

Or you could use a Selection widget and offer presets

Yes I am. I’ll try that.
Thank you all. I’m just starting so I guess my mistakes are elementary.
you are very kind :sweat_smile:

1 Like