[SOLVED] Using JSON for Dimming in a Rule

Hi All

I have written a rule but it seems not to function. I believe because of the json string, it appears as

"direction":"INCREASE"

So my guess is I need to some how remove all of the string other than INCREASE

My item is a string

17:15:22.808 [INFO ] [smarthome.event.ItemStateChangedEvent] - BedRoom1_AeoButton_Slide1 changed from {"direction":"DECREASE"} to {"direction":"INCREASE"}

My goal is to basically increase the light dimming by 10% if the json string returns ‘increase’

I thought I had it right :stuck_out_tongue:

No errors! JSON path is installed.

Rule:

var Number percent = 0

rule "WallMote Dim Control"
when
	Item BedRoom1_AeoButton_Slide1
then
	val String json = (BedRoom1_AeoButton_Slide1.state as StringType).toString
		val String type = transform("JSONPATH", "$._type", json)
	if (type == "INCREASE") {
		percent = LivingRoomDim1.state as Number
			percent = percent + 10 // increase brightness by 10%
				LivingRoomDim1.sendCommand(percent)
}
logInfo('myrule', json)
end

thanks

Seems I need to split but this doesnt work very well because I need to store either the INCREASE or DECREASE values.


var Number percent = 0

rule "WallMote Dim Control"
when
        Item BedRoom1_AeoButton_Slide1 received update
then
        val String json = (BedRoom1_AeoButton_Slide1.state as StringType).toString.split("\\W+").get(2)
                val String type = transform("JSONPATH", "$._type", json)
        if (type == "INCREASE") {
                percent = LivingRoomDim1.state as Number
                        percent = percent + 10 // increase brightness by 10%
                                LivingRoomDim1.sendCommand(percent)
}
logInfo('myrule', json)
end

Are you looking for the if (someString.contains("target")) method?

Well, my json string contains direction: increase and direction: decrease

I want to act on either of those…Im not familiar with if (someString.contains("target"))

The above rule works, but I need to a) limit the increases to 100% (it went over 100) and then incorporate the decrease but the transform only splits the increase.

Think this

val String type = transform("JSONPATH", "$._type", json)

should be this?

val String type = transform("JSONPATH", "$.direction", json)
1 Like

Thanks Mark that got it working!!!

However

It seems that there 0 or 100 of the percent are not limits, it will try and go to below 0 and above 100%

06:49:54.048 [WARN ] [arthome.model.script.actions.BusEvent] - Cannot convert '-10' to a command type which item 'LivingRoomDim1' accepts: [PercentType, OnOffType, IncreaseDecreaseType, RefreshType].

Is there a way to define percent as being only between 0 and 100?

Yep, it’s called a Dimmer type. It only accepts 0-100. The problem is your rule sending e.g. -10, which isn’t allowed.
Solution: only send commands 0-100

Write code with the effect of “if >100 make it 100”, etc.

You could do something like this.

import java.lang.Math
...
percent = Math.min(percent + 10, 100)

and

percent = Math.max(percent - 10, 0)
1 Like

@dastrix80 So, I forgot to ask… It sounds like you got the Wallmote slide functionality working. Was there another change needed to the binding to get it working?

Cool, ill try this Mark! Thanks

Yes, it works, you need to grab the latest snapshot binding and delete the Wallmote things for the channels to pickup the changes.

So this is what I’ve ended up with, Thanks Guys

Ill test this tonight. Unfortunately I need one of these rules for each button , alot of rules if you have alot of buttons! I’m sure Mark youll be able to consolidate them down using smarter logic given you’ve a few


rule "WallMote Dim Control Kitchen"
when
        Item Kitchen_AeoButton_Slide1 received update
then
        val String json = (Kitchen_AeoButton_Slide1.state as StringType).toString.split("\\W+").get(2)
                val String type = transform("JSONPATH", "$.direction", json)
        if (type == "INCREASE") {
                percent_kit = KitchenDim1.state as Number
                        percent_kit = Math.min(percent_kit + 10,100) // increase brightness by 10%
                                KitchenDim1.sendCommand(percent_kit)
}
        if (type == "DECREASE") {
                percent_kit = KitchenDim1.state as Number
                        percent_kit = Math.max(percent_kit - 10,0) // decrease brightness by 10%
                                KitchenDim1.sendCommand(percent_kit)
}
logInfo('myrule', json)
end
1 Like

Hi Mark

Ive started to test it but I get this error. I assume this is because the item is null.


17:36:57.034 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'WallMote Dim Control Kitchen': An error occurred during the script execution: Could not invoke method: java.lang.Math.min(int,int) on instance: null
17:36:59.398 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'WallMote Dim Control Kitchen': An error occurred during the script execution: Could not invoke method: java.lang.Math.min(int,int) on instance: null

Ah, how is percent_kit defined?

If var int percent_kit, I think you need this

percent_kit = (KitchenDim1.state as Number).intValue
percent_kit = Math.min(percent_kit + 10,100) // increase brightness by 10%
KitchenDim1.sendCommand(percent_kit)

If var Number percent_kit, I think you need this

percent_kit = KitchenDim1.state as Number
percent_kit = Math.min(percent_kit.intValue + 10,100) // increase brightness by 10%
KitchenDim1.sendCommand(percent_kit)

Yes, but ive got them as Numbers

var Number percent_kit = 0
var Number percent_liv = 0
var Number percent_eav = 0

Updated it and now:

23:00:54.363 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'WallMote Dim Control Kitchen': 'IntValue' is not a member of 'java.lang.Number'; line 34, column 48, length 20

I tried to import java.lang.Number, same issue

Rules are case sensitive.

1 Like

Lovely work gents!

:07:06.426 [INFO ] [smarthome.event.ItemStateChangedEvent] - Kitchen_AeoButton_Slide3 changed from {"direction":"DECREASE"} to {"direction":"INCREASE"}
23:07:06.431 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'LivingRoomDim1' received command 10
23:07:06.433 [INFO ] [arthome.event.ItemStatePredictedEvent] - LivingRoomDim1 predicted to become 10
23:07:06.436 [INFO ] [smarthome.event.ItemStateChangedEvent] - LivingRoomDim1 changed from 0 to 10
23:07:06.439 [INFO ] [home.event.GroupItemStateChangedEvent] - gAllDimmers changed from 0 to UNDEF through LivingRoomDim1
23:07:06.654 [INFO ] [home.event.GroupItemStateChangedEvent] - gAllLights changed from OFF to ON through LivingRoomSw1
23:07:06.656 [INFO ] [smarthome.event.ItemStateChangedEvent] - LivingRoomSw1 changed from OFF to ON
23:07:06.659 [INFO ] [home.event.GroupItemStateChangedEvent] - gInsideLights changed from OFF to ON through LivingRoomSw1
23:07:07.698 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'LivingRoomDim1' received command 20
23:07:07.700 [INFO ] [arthome.event.ItemStatePredictedEvent] - LivingRoomDim1 predicted to become 20
23:07:07.704 [INFO ] [smarthome.event.ItemStateChangedEvent] - LivingRoomDim1 changed from 10 to 20
23:07:09.425 [INFO ] [smarthome.event.ItemStateChangedEvent] - LivingRoomNum1 changed from 0 to 5.8
23:07:09.428 [INFO ] [home.event.GroupItemStateChangedEvent] - gAllLightsUsage changed from 60.16 to 65.96 through LivingRoomNum1
23:07:15.245 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'LivingRoomDim1' received command 30
23:07:15.248 [INFO ] [arthome.event.ItemStatePredictedEvent] - LivingRoomDim1 predicted to become 30
23:07:15.250 [INFO ] [smarthome.event.ItemStateChangedEvent] - LivingRoomDim1 changed from 20 to 30
23:07:16.975 [INFO ] [smarthome.event.ItemStateChangedEvent] - LivingRoomNum1 changed from 5.8 to 23.7
23:07:16.977 [INFO ] [home.event.GroupItemStateChangedEvent] - gAllLightsUsage changed from 65.96 to 83.86 through LivingRoomNum1
23:07:19.199 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'LivingRoomDim1' received command 40
23:07:19.202 [INFO ] [arthome.event.ItemStatePredictedEvent] - LivingRoomDim1 predicted to become 40
23:07:19.206 [INFO ] [smarthome.event.ItemStateChangedEvent] - LivingRoomDim1 changed from 30 to 40
23:07:23.523 [INFO ] [smarthome.event.ItemStateChangedEvent] - Kitchen_AeoButton_Slide3 changed from {"direction":"INCREASE"} to {"direction":"DECREASE"}
23:07:23.528 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'LivingRoomDim1' received command 30
23:07:23.529 [INFO ] [arthome.event.ItemStatePredictedEvent] - LivingRoomDim1 predicted to become 30
23:07:23.531 [INFO ] [smarthome.event.ItemStateChangedEvent] - LivingRoomDim1 changed from 40 to 30
23:07:23.838 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'LivingRoomDim1' received command 20
23:07:23.840 [INFO ] [arthome.event.ItemStatePredictedEvent] - LivingRoomDim1 predicted to become 20
23:07:23.842 [INFO ] [smarthome.event.ItemStateChangedEvent] - LivingRoomDim1 changed from 30 to 20
23:07:23.885 [INFO ] [smarthome.event.ItemStateChangedEvent] - LivingRoomDim1 changed from 20 to 30
23:07:24.118 [INFO ] [smarthome.event.ItemStateChangedEvent] - LivingRoomDim1 changed from 30 to 20
23:07:25.553 [INFO ] [smarthome.event.ItemStateChangedEvent] - LivingRoomNum1 changed from 23.7 to 40.4
23:07:25.555 [INFO ] [home.event.GroupItemStateChangedEvent] - gAllLightsUsage changed from 83.86 to 100.56 through LivingRoomNum1
23:07:28.237 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'LivingRoomDim1' received command 10
23:07:28.239 [INFO ] [arthome.event.ItemStatePredictedEvent] - LivingRoomDim1 predicted to become 10
23:07:28.242 [INFO ] [smarthome.event.ItemStateChangedEvent] - LivingRoomDim1 changed from 20 to 10

1 Like