Dimmer rule

Hello,

I have an issue that I can’t figure out. Probably its something simple.

I wan’t to dim up en down some tradfi bulbs using a zwave.me wall controller.

For the dimming part I use this rule ( Its only the part for dimming, as the rule is a bit bigger with all the button functions. The Wallcontroller sends 1.2 when long pressed, en 1.1 when released.

rule "Schakelaar begane grond"

when
        Item ZWaveNode4ZME_KFOBSSecure4ButtonKeyChainController_SceneNumber received update

then

        var boolean Switch12 = false
        var boolean Switch52 = false
        var int brightness = 0
        var int brightnessTarget = 0

         if (ZWaveNode4ZME_KFOBSSecure4ButtonKeyChainController_SceneNumber.state == 1.2) {
                Switch12 = true
                brightness = (Brightness_Hal_BG_1.state as DecimalType).intValue
                brightnessTarget = brightness + 5

                      if (brightnessTarget > 99)
                      brightnessTarget = 100

                sendCommand(Brightness_Hal_BG_1 , brightnessTarget)

                Thread::sleep(400)
            }
                 if (ZWaveNode4ZME_KFOBSSecure4ButtonKeyChainController_SceneNumber.state == 1.1) {
                Switch12 = false }

end

The light goes on, but the rule doesn’t sleep proper as can been seen in the log below.

2018-05-30 21:35:20.359 [vent.ItemStateChangedEvent] - ZWaveNode4ZME_KFOBSSecure4ButtonKeyChainController_SceneNumber changed from 1.1 to 1.2

2018-05-30 21:35:21.282 [ome.event.ItemCommandEvent] - Item 'Brightness_Hal_BG_1' received command 17

2018-05-30 21:35:21.299 [ome.event.ItemCommandEvent] - Item 'Brightness_Hal_BG_1' received command 17

2018-05-30 21:35:21.302 [ome.event.ItemCommandEvent] - Item 'Brightness_Hal_BG_1' received command 17

2018-05-30 21:35:21.307 [ome.event.ItemCommandEvent] - Item 'Brightness_Hal_BG_1' received command 17

2018-05-30 21:35:21.321 [ome.event.ItemCommandEvent] - Item 'Brightness_Hal_BG_1' received command 17

2018-05-30 21:35:21.326 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 12 to 17

2018-05-30 21:35:21.743 [ome.event.ItemCommandEvent] - Item 'Brightness_Hal_BG_1' received command 22

2018-05-30 21:35:21.763 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 17 to 22

2018-05-30 21:35:21.785 [ome.event.ItemCommandEvent] - Item 'Brightness_Hal_BG_1' received command 27

2018-05-30 21:35:21.792 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 22 to 27

2018-05-30 21:35:21.809 [ome.event.ItemCommandEvent] - Item 'Brightness_Hal_BG_1' received command 32

2018-05-30 21:35:21.823 [ome.event.ItemCommandEvent] - Item 'Brightness_Hal_BG_1' received command 22

2018-05-30 21:35:21.830 [ome.event.ItemCommandEvent] - Item 'Brightness_Hal_BG_1' received command 32

2018-05-30 21:35:21.834 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 27 to 32

2018-05-30 21:35:21.848 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 32 to 22

2018-05-30 21:35:21.853 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 22 to 32

2018-05-30 21:35:21.896 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 32 to 17

2018-05-30 21:35:22.171 [ome.event.ItemCommandEvent] - Item 'Brightness_Hal_BG_1' received command 22

2018-05-30 21:35:22.178 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 17 to 22

2018-05-30 21:35:22.228 [ome.event.ItemCommandEvent] - Item 'Brightness_Hal_BG_1' received command 27

2018-05-30 21:35:22.232 [ome.event.ItemCommandEvent] - Item 'Brightness_Hal_BG_1' received command 27

2018-05-30 21:35:22.261 [ome.event.ItemCommandEvent] - Item 'Brightness_Hal_BG_1' received command 27

2018-05-30 21:35:22.265 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 22 to 27

2018-05-30 21:35:22.274 [ome.event.ItemCommandEvent] - Item 'Brightness_Hal_BG_1' received command 27

2018-05-30 21:35:22.308 [vent.ItemStateChangedEvent] - ZWaveNode4ZME_KFOBSSecure4ButtonKeyChainController_SceneNumber changed from 1.2 to 1.1

2018-05-30 21:35:22.512 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 27 to 17

2018-05-30 21:35:24.308 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 17 to 23

2018-05-30 21:35:24.916 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 23 to 28

2018-05-30 21:35:25.508 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 28 to 32

2018-05-30 21:35:25.853 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 32 to 28

2018-05-30 21:35:26.120 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 28 to 23

2018-05-30 21:35:26.721 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 23 to 32

2018-05-30 21:35:26.861 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 32 to 23

2018-05-30 21:35:27.923 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 23 to 28

I hope someone can help. I have read a lot of examples. ( this is a build of examples ) But can;t find what I’m doing wrong.
Expected the rule would run once when 1.2 triggers, and wait 400ms and run again.
The Boolean is to stop the loop.

Best Regards,
Olaf

Probably because as long as you are holding the button you are receiving updates and each update triggers a separate instance of this rule running. You probably have two or three copies of this rule running at the same time.

Additional problems I see is your Switch12 and Switch52 are pointless. They don’t exist outside this Rule and you never check them for anything anyway.

There really is no good way to do this using your current approach.

Assuming you get an OFF command or update when you stop pressing the switch then you can do something like the following:

val Timer dimmingTimer = null

rule "Schakelaar begane grond"
when
    Item ZWaveNode4ZME_KFOBSSecure4ButtonKeyChainController_SceneNumber changed 
then

    // switch is being pressed
    if(ZWaveNode4ZME_KFOBSSecure4ButtonKeyChainController_SceneNumber.state == 1.2) {
        if(dimmingTimer === null){
            dimmingTimer = createTimer(now.plusSeconds(0), [ |
                var brightness = Brightness_Hal_BG_1.state as Number + 5
                if(brightness > 99) brightness = 100
                Brightness_Hal_BG_1.sendCommand(brightness)
                if(brightness == 100) {
                    dimmingTimer = null
                }
                else {
                    dimmingTimer.reschedule(now.plusMillis(400)
                }
            ])
        }
    }
    // Switch is not being pressed
    else if(ZWaveNode4ZME_KFOBSSecure4ButtonKeyChainController_SceneNumber.state == 1.1) {
        dimmingTimer?.cancel
        dimmingTimer = null
    }
end
1 Like

That’s making complete sense. You are right. It’s running up to 5x at the same time ( max threads = 5 I read ).

Will try this asap. Let you know.
Thanks for the clear answer.

Best regards,
Olaf

This is working a lot better. Only I get 3 times the answer from the tradfri hub with the brightness. Is also adds 1 more ?
Maybe better play with the timer’s time and or brightness steps ?

2018-05-30 22:43:47.224 [ome.event.ItemCommandEvent] - Item 'Brightness_Hal_BG_1' received command 5

2018-05-30 22:43:47.241 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 0 to 5

2018-05-30 22:43:47.251 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 5 to 0

2018-05-30 22:43:47.277 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 0 to 6

2018-05-30 22:43:48.027 [ome.event.ItemCommandEvent] - Item 'Brightness_Hal_BG_1' received command 11

2018-05-30 22:43:48.039 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 6 to 11

2018-05-30 22:43:48.052 [vent.ItemStateChangedEvent] - Brightness_Hal_BG_1 changed from 11 to 12

2018-05-30 22:43:48.844 [ome.event.ItemCommandEvent] - Item 'Brightness_Hal_BG_1' received command 17

Best Regards,
Olaf

Hi @rlkoshak
I use the rule that you have suggested. I have differianated it a bit so as to be able to do up/down bimmer light with one shoot. I am facing a problem though : when the light goes from up to down the change happens too fast while when it goes from down to up it goes very smooth. What shall I change so as it goes smooth the up to down light as well?

val Timer dimmingTimer = null

rule "Living Room Dimmer"

when

    Item Node_012_Scene_number received update

then

    // switch is being pressed

    if (Node_012_Scene_number.state == 1.0 ){

        SLiving_Room.sendCommand(if(SLiving_Room.state != ON) ON else OFF)

        logInfo(filename, "Node 012 Switch Button : (1.0) Αριστερό Πανω 1 click  : ΣΑΛΟΝΙ ON-OFF")

    }

    if(Node_012_Scene_number.state == 1.2 && Living_Room.state <= 50) {

        if(dimmingTimer === null){

            dimmingTimer = createTimer(now.plusSeconds(0), [ |

                var brightness = Living_Room.state as Number + 5

                if(brightness > 99) brightness = 100

                Living_Room.sendCommand(brightness)

                if(brightness == 100) {

                    dimmingTimer = null

                }

                else {

                    dimmingTimer.reschedule(now.plusMillis(400))

                }

            ])

        }

    }

    if(Node_012_Scene_number.state == 1.2 && Living_Room.state >= 50) {

        if(dimmingTimer === null){

            dimmingTimer = createTimer(now.plusSeconds(0), [ |

                var brightness = Living_Room.state as Number - 5

                if(brightness < 99) brightness = 0

                Living_Room.sendCommand(brightness)

                if(brightness == 0) {

                    dimmingTimer = null

                }

                else {

                    dimmingTimer.reschedule(now.plusMillis(400))

                }

            ])

        }

    }

    // Switch is not being pressed

    else if(Node_012_Scene_number.state == 1.1) {

        dimmingTimer?.cancel

        dimmingTimer = null

    }

end

Your script works perfectly, the thing I was searching for :slight_smile:
About your question I just removed in the script for down:
Yours:
if(brightness < 99) brightness = 0
My:
if(brightness < 99)

And to really get bulb off in case I manually adjusted the brightness (I also have +/- 3 instead 5 for smoother dimming) I changed this:
Yours:
if (brightness == 0) {dimmingTimer = null}

Me:
if (brightness <=2)
{
G_WohnzimmerlampenBrightness.sendCommand(0)
dimmingTimer?.cancel
dimmingTimer = null
}