Toggle Dimmer Rule is Not Firing

I am having some issues getting my first rule to run. My thought is that the first click of the switch “ON” will set the dimmer to 20% and the second click will set the dimmer to 100%. There is nothing in the log about the rule firing and there are no errors. I am sure there is something that I am missing here. Any help is much appreciated!

  • Platform information:
    • Hardware: AMD/4GB RAM/20TB Storage
    • OS: Ubuntu 18.04
    • Java Runtime Environment: openJDK 11.0.4
    • openHAB version: 2.0.0
  • Issue of the topic: please be detailed explaining your issue
  • Things: Zooz Z-wave switch/dimmer
  • Items: Switch and dimmer
  • No sitemap

Rule:

var int count1=0
var int count2=0
rule LivingRoom
	when
		Item LRSwitch received update ON
	then
		logDebug("count1", "Rule Fired")
		if (count1=0){
			LRDimmer.sendCommand(20)
			count1=1
			logDebug("count1", "DIM")
			}
		else {
			LRDimmer.sendCommand(100)
			count1=0
			logDebug("count1", "Bright")
			}
	end

The switch does not receive an update.

when
    Item LRSwitch changed from OFF to ON
then
...
1 Like

Remember, unless you setup persistence. the Item will default to UNDEF at system start (or after the item file is reloaded) so the rule may not trigger the first time.

Thanks for the reply Bruce. I edited the rule as you have above. It seemed as if the rule was not entering the if statement and the lights would just go to 100% (event log below). The second click of the switch would do nothing. I waited about a second between the first and second click up.

2019-08-22 19:49:39.016 [vent.ItemStateChangedEvent] - LRSwitch changed from OFF to ON
2019-08-22 19:49:39.017 [vent.ItemStateChangedEvent] - LRDimmer changed from 0 to 100

So I updated the rule as below. The first click turns the lights on at 20%. If I double click the switch after that the lights go to 100%. I think the switch naturally puts the lights to 100% with a double click so I don’t think the second rule is doing anything, but this is essentially achieving what I wanted. I would like to understand why the if statements aren’t working. Any thoughts?

var int count1=0
var int count2=0
rule LivingRoom
	when
		Item LRSwitch changed from OFF to ON
	then 
		count1=1
		logDebug("count1", "Rule Fired")
		LRDimmer.sendCommand(20)

	end
	
rule LivingRoom100
	when 
		Item LRSwitch received command
	then
		if (count1=1){
			LRDimmer.sendCommand(100)
			logDebug("count1", "Bright")
			count1=0
		}
	end

Item will default to UNDEF at system start

Ah, this may be a problem too. Thanks for that. I will take a look to see if this is set up properly. Thanks for the input!

This rule won’t trigger unless you see commands in your event log.
You don’t usually get commands from incoming status from devices (there are odd exceptions).
You get commands from interacting with Items in your UI, or explicitly sent by rules.

Ok. Thanks. So it seems that the only way to trigger the rule for a switch is changed from OFF to ON as Bruce said. Is there any way to trigger something from a second “on click” if the switch is already on?

That would be the update
Updates from e.g. ON to ON aren’t logged in events.log at a normal logging level, but they can and do happen, and can trigger rules.

The problem you might be having is that your device doesn’t transmit any update to openHAB when you push the button a second time.

The question is really, does your openHAB Switch Item represent the ON/OFF state of the dimmer or the pressed/not-pressed state of the button? I don’t know.

Thanks! I will give that a try and see what happens. Worst case is that it is working now but the implementation isn’t as clean as I would like. I have a lot to learn. Appreciate all of the info!

1 Like

So it’s only about changing the behavior of the switch. instead getting OFF->ON and ON->OFF, you want an OFF->20%; 20%->100%; 100%->OFF right?

Your first rule should have worked, but your if() is wrong.

count = 0 -> set count to the value 0
if(count==0) -> if count is equal to 0

So, when comparing values, you have to use == for equality instead of =. When comparing to null (which is special) you should use === instead, as this will check if the value is identical to null.

There is another question: Is your wall switch independent from the dimmer? This is essential to avoid interference.

So given the Items

Switch LRSwitch "Wall switch [%s]" { some link to addon }
Dimmer LRDimmer "My Dimmer [%d %%]" { another link to addon }

and given that openHAB receives commands from a pressed wall switch, a rule would be like this:

// global var and val are defined at the top of the file
var Integer count1 = 0

rule "light living room"
when
    Item LRSwitch received command ON 
then
    count1 = count1 + 1               // count up
    var Number nBright = 0            // initialize var
    switch (count1) {                 // like if, but more than two options
        case 1: { nBright = 20 }      // if 1 set to 20
        case 2: { nBright = 100 }     // if 2 set to 100
        default : {                   // otherwise
            nBright = 0               // set to 0 and
            count1 = 0                // set count1 to 0
        }
    }
    LRDimmer.sendCommand(nBright)     // send the command
end
2 Likes

Thanks Udo. This is very helpful. I am going to try the if statement again. My ultimate goal is to have this rule only fire if it is a certain time window in the morning. I will give it a shot before I ask more questions. Thanks for the inputs!