Dimmer Rule/Command Help needed

Hello,

I am trying to be able to dim smoothly my Yeelight LED Ceiling Lamp (JIAOYUE 650) via an HM-RC-2-PBU-FM (Wireless Transmitter 2-channel for brand switch systems)

This is how my rule looks like right now:

rule "Schlafzimmer Schalter"
when
	Channel 'homematic:HM-RC-2-PBU-FM:OEQ0609246:OEQ1232671:1#PRESS' triggered CONT
then
	if(YeelightLEDCeilingLampV4JIAOYUE650RGB_Brightness.state>0)
	{
	bright = YeelightLEDCeilingLampV4JIAOYUE650RGB_Brightness.state
	bright-1
	YeelightLEDCeilingLampV4JIAOYUE650RGB_Brightness.sendCommand(bright)
	}
end

but i get the following error in the logs, but I do not understand why it gets the value null.

Because when I add logging the state of the brightness it shows the correct value:

2018-03-01 14:34:54.709 [INFO ] [clipse.smarthome.model.script.bright] - bright equals YeelightLEDCeilingLampV4JIAOYUE650RGB_Brightness (Type=NumberItem, State=100, Label=brightness, Category=light, Groups=[gLight]) 

Where is my mistake?

Below the error log of my current rule:

2018-03-01 14:27:45.190 [vent.ChannelTriggeredEvent] - homematic:HM-RC-2-PBU-FM:OEQ0609246:OEQ1232671:1#PRESS triggered LONG

2018-03-01 14:27:46.005 [vent.ChannelTriggeredEvent] - homematic:HM-RC-2-PBU-FM:OEQ0609246:OEQ1232671:1#PRESS triggered CONT

==> /var/log/openhab2/openhab.log <==

2018-03-01 14:27:46.229 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Schlafzimmer Schalter': An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.IntegerExtensions.operator_minus(int,int) on instance: null

==> /var/log/openhab2/events.log <==

2018-03-01 14:27:46.273 [vent.ChannelTriggeredEvent] - homematic:HM-RC-2-PBU-FM:OEQ0609246:OEQ1232671:1#PRESS triggered CONT

==> /var/log/openhab2/openhab.log <==

2018-03-01 14:27:46.283 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Schlafzimmer Schalter': An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.IntegerExtensions.operator_minus(int,int) on instance: null

==> /var/log/openhab2/events.log <==

2018-03-01 14:27:46.544 [vent.ChannelTriggeredEvent] - homematic:HM-RC-2-PBU-FM:OEQ0609246:OEQ1232671:1#PRESS triggered CONT

==> /var/log/openhab2/openhab.log <==

2018-03-01 14:27:46.554 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Schlafzimmer Schalter': An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.IntegerExtensions.operator_minus(int,int) on instance: null

==> /var/log/openhab2/events.log <==

2018-03-01 14:27:46.817 [vent.ChannelTriggeredEvent] - homematic:HM-RC-2-PBU-FM:OEQ0609246:OEQ1232671:1#PRESS triggered CONT

==> /var/log/openhab2/openhab.log <==

2018-03-01 14:27:46.828 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Schlafzimmer Schalter': An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.IntegerExtensions.operator_minus(int,int) on instance: null

==> /var/log/openhab2/events.log <==

2018-03-01 14:27:47.087 [vent.ChannelTriggeredEvent] - homematic:HM-RC-2-PBU-FM:OEQ0609246:OEQ1232671:1#PRESS triggered CONT

==> /var/log/openhab2/openhab.log <==

2018-03-01 14:27:47.103 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Schlafzimmer Schalter': An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.IntegerExtensions.operator_minus(int,int) on instance: null

I see two mistakes.

  1. You need to cast the state to a Number
  2. When you do arithmetic, you need to assign the result to a variable. bright-1 essentially does nothing

Try:

    bright = YeelightLEDCeilingLampV4JIAOYUE650RGB_Brightness.state as Number
    bright = bright - 1
    YeelightLEDCeilingLampV4JIAOYUE650RGB_Brightness.sendCommand(bright)

Thanks a lot for the answer! I just tried it and it sends the commands but the brightness does not change. Any idea why that could be ?

val Number bright

rule "Schlafzimmer Dimming"
when
	Channel 'homematic:HM-RC-2-PBU-FM:OEQ0609246:OEQ1232671:1#PRESS' triggered CONT
then
	if(YeelightLEDCeilingLampV4JIAOYUE650RGB_Brightness.state as Number >0)
	{	
    bright = YeelightLEDCeilingLampV4JIAOYUE650RGB_Brightness.state as Number
    bright = bright - 1
    YeelightLEDCeilingLampV4JIAOYUE650RGB_Brightness.sendCommand(bright)
	}
end

Log:

2018-03-01 17:08:08.560 [vent.ChannelTriggeredEvent] - homematic:HM-RC-2-PBU-FM:OEQ0609246:OEQ1232671:1#PRESS triggered LONG

2018-03-01 17:08:08.830 [vent.ChannelTriggeredEvent] - homematic:HM-RC-2-PBU-FM:OEQ0609246:OEQ1232671:1#PRESS triggered CONT

2018-03-01 17:08:08.841 [ome.event.ItemCommandEvent] - Item 'YeelightLEDCeilingLampV4JIAOYUE650RGB_Brightness' received command 94

2018-03-01 17:08:08.850 [vent.ItemStateChangedEvent] - YeelightLEDCeilingLampV4JIAOYUE650RGB_Brightness changed from 95 to 94

2018-03-01 17:08:09.101 [vent.ChannelTriggeredEvent] - homematic:HM-RC-2-PBU-FM:OEQ0609246:OEQ1232671:1#PRESS triggered CONT

2018-03-01 17:08:09.115 [ome.event.ItemCommandEvent] - Item 'YeelightLEDCeilingLampV4JIAOYUE650RGB_Brightness' received command 93

2018-03-01 17:08:09.121 [vent.ItemStateChangedEvent] - YeelightLEDCeilingLampV4JIAOYUE650RGB_Brightness changed from 94 to 93

2018-03-01 17:08:09.374 [vent.ChannelTriggeredEvent] - homematic:HM-RC-2-PBU-FM:OEQ0609246:OEQ1232671:1#PRESS triggered CONT

According to the events.log you posted it is working. Whenever the channel gets a CONT the Item gets a command one down from where it was.

I think the problem is the channel only gets the CONT once a second and 1% drop in brightness is not that much.

Thank you!

I am talking with @Anthrax about this lamp in particular and there seems to be another light mode.
We will continue our talk/solution finding here in the thread so everybody can learn from this.

@Anthrax
So what you are saying is that if I want to be able to really dim from 100 (ON) to 0 (OFF) I will probably need to first dim the normal brightness down as far as possible and then switch to night mode and dim it from 100 to 0 aswell, correct ?

If yes how do I switch to night mode programmatically ?

I have a question, I need to get the right brightness value for my lamp with:

YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_.sendCommand('get_prop["nl_br"]')

Which returns in the logs:

YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_ changed from get_prop["nl_br"] to {"result":["80"],"id":12465}

How can I now just get the value in this case 80 and use it in my dimming rule.?

You could install the JSONPATH transformation service and try this…

val String result = transform("JSONPATH","$.result[0]",YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_.state.toString)

Thank you for your answer. It is kind of working but sometimes the result show the wrong thing.

See the log here:

2018-03-02 19:10:56.208 [vent.ChannelTriggeredEvent] - homematic:HM-RC-2-PBU-FM:OEQ0609246:OEQ1232671:2#PRESS triggered CONT

2018-03-02 19:10:56.217 [ome.event.ItemCommandEvent] - Item 'YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_' received command get_prop["nl_br"]

2018-03-02 19:10:56.249 [vent.ItemStateChangedEvent] - YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_ changed from {"result":["ok"],"id":12796} to get_prop["nl_br"]

2018-03-02 19:10:56.315 [vent.ItemStateChangedEvent] - YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_ changed from get_prop["nl_br"] to {"result":["1"],"id":12799}

2018-03-02 19:10:56.491 [vent.ChannelTriggeredEvent] - homematic:HM-RC-2-PBU-FM:OEQ0609246:OEQ1232671:2#PRESS triggered CONT

2018-03-02 19:10:56.503 [ome.event.ItemCommandEvent] - Item 'YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_' received command get_prop["nl_br"]

2018-03-02 19:10:56.531 [vent.ItemStateChangedEvent] - YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_ changed from {"result":["1"],"id":12799} to get_prop["nl_br"]

==> /var/log/openhab2/openhab.log <==

2018-03-02 19:10:56.549 [INFO ] [clipse.smarthome.model.script.result] - result Flag ist ok

2018-03-02 19:10:56.549 [INFO ] [clipse.smarthome.model.script.result] - result Flag ist ok

==> /var/log/openhab2/events.log <==

2018-03-02 19:10:56.586 [vent.ItemStateChangedEvent] - YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_ changed from get_prop["nl_br"] to {"result":["1"],"id":12802}

2018-03-02 19:10:56.750 [vent.ChannelTriggeredEvent] - homematic:HM-RC-2-PBU-FM:OEQ0609246:OEQ1232671:2#PRESS triggered CONT

2018-03-02 19:10:56.776 [ome.event.ItemCommandEvent] - Item 'YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_' received command get_prop["nl_br"]

2018-03-02 19:10:56.784 [vent.ItemStateChangedEvent] - YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_ changed from {"result":["1"],"id":12802} to get_prop["nl_br"]

==> /var/log/openhab2/openhab.log <==

2018-03-02 19:10:56.791 [ERROR] [ore.transform.actions.Transformation] - Error executing the transformation 'JSONPATH': Invalid path '$.result[0]' in 'get_prop["nl_br"]'

2018-03-02 19:10:56.799 [INFO ] [clipse.smarthome.model.script.result] - result Flag ist get_prop["nl_br"]

==> /var/log/openhab2/events.log <==

2018-03-02 19:10:56.884 [vent.ItemStateChangedEvent] - YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_ changed from get_prop["nl_br"] to {"result":["1"],"id":12805}

2018-03-02 19:10:57.022 [vent.ChannelTriggeredEvent] - homematic:HM-RC-2-PBU-FM:OEQ0609246:OEQ1232671:2#PRESS triggered CONT

2018-03-02 19:10:57.032 [ome.event.ItemCommandEvent] - Item 'YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_' received command get_prop["nl_br"]

2018-03-02 19:10:57.039 [vent.ItemStateChangedEvent] - YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_ changed from {"result":["1"],"id":12805} to get_prop["nl_br"]

==> /var/log/openhab2/openhab.log <==

2018-03-02 19:10:57.039 [INFO ] [clipse.smarthome.model.script.result] - result Flag ist 1

This is how my rule looks like right now:

rule "Schlafzimmer Dimming"
when
	Channel 'homematic:HM-RC-2-PBU-FM:OEQ0609246:OEQ1232671:2#PRESS' triggered LONG
then
	YeelightLEDCeilingLampV4JIAOYUE650RGB_Power.sendCommand(ON)
	YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_.sendCommand('set_scene["nightlight", 1]')
	
end

rule "Schlafzimmer Dimming"
when
	Channel 'homematic:HM-RC-2-PBU-FM:OEQ0609246:OEQ1232671:2#PRESS' triggered CONT
then
YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_.sendCommand('get_prop["nl_br"]')
result = transform("JSONPATH","$.result[0]",YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_.state.toString)
logInfo("result","result Flag ist {}", result)
end

Is there any way to only get the actual correct value ?

Lol… I was going to mention that it looked like your rule would need some handling for this :wink:. What binding are you using? It seems really odd that the Channel the YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_ Item is linked to would be used for both sending commands and receiving states. If the results were only dimmer states, it would be easy, but it looks like there are some OKs coming back too. Posting all of your related items might help. I’m sure there is some discussion somewhere about how to use the binding, so that would probably be the best place to go next. But off the top of my head, I’d setup another rule triggered on YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_, like this…

rule "YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_ received update"
when
    Item YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_ received update
then
    if (YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_.state.toString.contains("result")) {
        val String result = transform("JSONPATH","$.result[0]",json)
        if (result.isNumeric) {
            // do something
        }
        logInfo("result","result Flag ist {}", result)
    }
end

You could put something like this in your existing rule, but you’d need to put in a Thread::sleep to wait for the result to come back in. Best to avoid sleeps because it ties up threads, which are a limited resource.

Hello right now i sloved it this way. (almost solved it)

rule "Schlafzimmer Dimming"
when
	Channel 'homematic:HM-RC-2-PBU-FM:OEQ0609246:OEQ1232671:2#PRESS' triggered LONG
then
	YeelightLEDCeilingLampV4JIAOYUE650RGB_Power.sendCommand(ON)
	YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_.sendCommand('get_prop["nl_br"]')
	result = transform("JSONPATH","$.result[0]",YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_.state.toString)
	logInfo("result","result Flag ist {}", result)
	nl_bright = Integer::parseInt(result)
	logInfo("nl_bright","nl_bright Flag ist {}", nl_bright)
	if(nl_bright < 100)
	{
		switchlightmodeflag == false
	} 
	YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_.sendCommand('set_power["on","smooth", 500, 5]')
	YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_.sendCommand('set_bright[[nl_bright%],"smooth", 500]')
	bright = 1

		
end

rule "Schlafzimmer Dimming 2"
when
	Channel 'homematic:HM-RC-2-PBU-FM:OEQ0609246:OEQ1232671:2#PRESS' triggered CONT
then
	if(nl_bright < 100 && switchlightmodeflag == false)
	{	
		nl_bright = nl_bright + 4
		YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_.sendCommand('set_bright[[nl_bright],"smooth", 500]')
	} else if(nl_bright >= 100 && switchlightmodeflag == false)
	{
		switchlightmodeflag =true
		YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_.sendCommand('set_power["on","smooth",500,1]')
		logInfo("Reached","Reached second if clause")
		YeelightLEDCeilingLampV4JIAOYUE650RGB_Brightness.sendCommand(1)
	} else if(switchlightmodeflag == true && bright < 100)
	{	
		logInfo("Reached","Reached last if clause")
		bright = YeelightLEDCeilingLampV4JIAOYUE650RGB_Brightness.state as Number
		bright = bright + 4
		YeelightLEDCeilingLampV4JIAOYUE650RGB_Brightness.sendCommand(bright)
	}
end

My problem right now is that it sends instead of the nl_bright value e.g. 1 just the word nl_bright

YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_.sendCommand('set_bright["[nl_bright]","smooth", 500]')

You would need to send it’s value then…

YeelightLEDCeilingLampV4JIAOYUE650RGB_Actions_.sendCommand('set_bright["[' + nl_bright.toString + ']","smooth", 500]')