First post here, and Openhab beginner … So please be patient!
I’m trying to set up a rule to control a Ikea Bulb
https://www.zigbee2mqtt.io/devices/LED1836G9.html
with the Ikea dimmer button
https://www.zigbee2mqtt.io/devices/E1743.html
both working with zigbee protocol via zigbee2mqtt.
The bulb is defined here:
Thing topic tradfri01 "Tradfri Bulb" {
Channels:
Type switch : switch "Switch Tradfri Bulb" [stateTopic="zigbee2mqtt/tradfri01", transformationPattern="JSONPATH:$.state", commandTopic="zigbee2mqtt/tradfri01/set", transformationPatternOut="JS:settradfristate.js"]
Type dimmer : dimmer "Dimmer Tradfri Bulb" [stateTopic="zigbee2mqtt/tradfri01", transformationPattern="JSONPATH:$.brightness", commandTopic="zigbee2mqtt/tradfri01/set", min=1, max=254, step=1, formatBeforePublish="{ \"brightness\" : %s }"]
Type number : brightness "tradfri01 brightness" [stateTopic="zigbee2mqtt/tradfri01", transformationPattern="JSONPATH:$.brightness"]
}
and the button here:
Thing topic tradfri_button "tradfri button" {
Channels:
Type string : click "click" [ stateTopic="zigbee2mqtt/tradfri_button", transformationPattern="JSONPATH:$.click"]
}
The button sends the following states:
on / off / brightness_up / brightness_down / brightness_stop
I’ve read in the forum that the appropriate way to manage the button is to write
a dedicated rule.
So thanks to contribution of many forum members ( have to mention @Udo_Hartmann, @rlkoshak, vzorglub)
I have written the following rule:
var Timer dimmingTimer = null
rule "tradfri button change"
when
Item tradfri_button changed
then
if (tradfri_button.state == "on") {
logInfo("tradfri_button.rules", "tradfri_button sends on")
sendCommand(tradfri01_dimmer,80)
}
if (tradfri_button.state == "off") {
logInfo("tradfri_button.rules", "tradfri_button sends off")
//sendCommand(tradfri01_dimmer,0)
sendCommand(tradfri01_switch,OFF)
}
if (tradfri_button.state == "brightness_up") {
logInfo("tradfri_button.rules", "tradfri_button sends brightness_up")
if (dimmingTimer === null) {
logInfo("tradfri_button.rules", "dimmingTimer is NULL")
//
dimmingTimer = createTimer(now.plusSeconds(0), [ |
var brightness = tradfri01_dimmer.state as Number + 5
if(brightness > 99) brightness = 100
tradfri01_dimmer.sendCommand(brightness)
logInfo("tradfri_button.rules", "brightness set to " + brightness)
if(brightness == 100) {
dimmingTimer = null
logInfo("tradfri_button.rules", "dimmingTimer set to NULL")
}
else {
dimmingTimer.reschedule(now.plusMillis(400))
logInfo("tradfri_button.rules", "dimmingTimer " + dimmingTimer)
}
])
}
else
if(dimmingTimer!==null) {
dimmingTimer.cancel
dimmingTimer = null
}
}
if (tradfri_button.state == "brightness_down") {
logInfo("tradfri_button.rules", "tradfri_button sends brightness_down")
if (dimmingTimer === null) {
logInfo("tradfri_button.rules", "dimmingTimer is NULL")
//
dimmingTimer = createTimer(now.plusSeconds(0), [ |
var brightness = tradfri01_dimmer.state as Number - 5
if(brightness < 1) brightness = 0
tradfri01_dimmer.sendCommand(brightness)
logInfo("tradfri_button.rules", "brightness set to " + brightness)
if(brightness == 0) {
dimmingTimer = null
logInfo("tradfri_button.rules", "dimmingTimer set to NULL")
}
else {
dimmingTimer.reschedule(now.plusMillis(400))
logInfo("tradfri_button.rules", "dimmingTimer " + dimmingTimer)
}
])
}
else
if(dimmingTimer!==null) {
dimmingTimer.cancel
dimmingTimer = null
}
}
if (tradfri_button.state == "brightness_stop") {
logInfo("tradfri_button.rules", "tradfri_button sends brightness_stop")
if(dimmingTimer!==null) {
dimmingTimer.cancel
dimmingTimer = null
logInfo("tradfri_button.rules", "dimmingTimer set to NULL")
}
}
end
Everything is ALMOST fine
The dimming action is a little erratic as you can see in the logfile:
As you want to dimmer the brightness down
the command is intercepted:
2020-03-09 17:12:49.873 [INFO ] [me.model.script.tradfri_button.rules] - tradfri_button sends brightness_down
2020-03-09 17:12:49.883 [INFO ] [me.model.script.tradfri_button.rules] - dimmingTimer is NULL
2020-03-09 17:12:49.910 [INFO ] [me.model.script.tradfri_button.rules] - brightness set to 75.0
and the brightness is set 5 points down
as you continue to keep the button pressed
2020-03-09 17:12:50.327 [INFO ] [me.model.script.tradfri_button.rules] - brightness set to 70.0
the brightness goes down
2020-03-09 17:12:50.751 [INFO ] [me.model.script.tradfri_button.rules] - brightness set to 65.0
the brightness goes down
But:
2020-03-09 17:12:50.793 [ome.event.ItemCommandEvent] - Item 'tradfri01_dimmer' received command 65.0
2020-03-09 17:12:50.797 [nt.ItemStatePredictedEvent] - tradfri01_dimmer predicted to become 65.0
2020-03-09 17:12:50.827 [vent.ItemStateChangedEvent] - tradfri01_dimmer changed from 70.0 to 65.0
2020-03-09 17:12:50.843 [vent.ItemStateChangedEvent] - tradfri01_dimmer changed from 65.0 to 75.00
2020-03-09 17:12:50.853 [vent.ItemStateChangedEvent] - tradfri01_brightness changed from 203.4 to 190.75
2020-03-09 17:12:51.170 [INFO ] [me.model.script.tradfri_button.rules] - brightness set to 70.00
here something goes wrong.
The dimmer goes down, and goes up for a moment and down again.
Any hint?
Best regards,
francis3