*Issue of the topic:
I need help creating a rule for sending a play command to MPD when I press the first button of my hue dimmer switch (setting up a second one for stop should be easy once this works).
I created this rule using a blockly script for the action:
A Thing will never have a status of “1000.0”. It will be ONLINE, OFFLINE, ERROR, INITIALIZING, etc.
Do you mean to check the event that triggered the rule to see if it’s “1000.0”?
It occurs to me that I don’t know how to do that in Blockly. In Rules DSL event will have the value from the channel trigger event that caused the rule to run. In other languages it would be event.event or something like that.
I had a look at Rules DSL and i guess i have a script that can work (with the help of ChatGPT).
I tested the dimmer switch and the MPD-remote separately. The switch works but the MPD-remote does only work in the GUI:
My problem is the command that has to be send to the remote-player: ChatGPT told me to use “mpc play” or “play”. In the documentation they use
actions.sendCommand("play");
the first part probably due to java. I didn’t manage to use one of them. So how to use the remote?
For simplicity I also tested using the “coding-GUI”. Setting the volume worked.
The rule written by ChatGPT(you don´t have to check it, if it doesn't work I will write my own)
import org.eclipse.xtext.xbase.lib.Functions
val Functions$Function1<HueDimmerSwitch, Boolean> heldLongEnough = [ switch |
val longPressDuration = 1000
val buttonPressedSince = switch.lastButtonPressTime - now
buttonPressedSince >= longPressDuration
]
val Functions$Function1<HueDimmerSwitch, Boolean> wasDimUp = [ switch |
switch.getEvent().getDirection() == "INCREASE"
]
val Functions$Function1<HueDimmerSwitch, Boolean> wasDimDown = [ switch |
switch.getEvent().getDirection() == "DECREASE"
]
rule "MPD player control with Philips Hue Dimmer Switch"
when
Channel "hue:0406:00112233:2:dimmer_switch#buttonevent" triggered
then
val dimmerSwitch = HueDimmerSwitch.lastEvent
if (wasDimUp.apply(dimmerSwitch) && heldLongEnough.apply(dimmerSwitch)) {
logInfo("MPD Control", "Starting MPD player")
executeCommandLine("mpc play", 1000)
} else if (wasDimDown.apply(dimmerSwitch) && heldLongEnough.apply(dimmerSwitch)) {
logInfo("MPD Control", "Stopping MPD player")
executeCommandLine("mpc stop", 1000)
}
end
I’ve yet to see ChatGPT produce a working openHAB rule. Always review every line of code against the docs.
Beyond being way more complicated than necessary, the code it generated isn’t sending a command to an Item, it’s using executeCommandLine to call an external program called mpc. I’t also probably a problem to use the reserved word “switch” as a variable name. There’s no such thing as “lastEvent” and if there were, it wouldn’t be a “dimmerSwitch”.
While this rule is less bogus than most, it does not break ChatGPT’s perfect zero when it comes to generating working openHAB rules. You’d have to know enough about coding OH rules to correct this code as you would need to write it in the first place without ChatGPT.
What you need is ultra simple though. First, please please please don’t skip the Getting Started Tutorial and the docs. Please do refer to the many thousands of example rules here on the forum.
Keep your original rule with it’s trigger. Remove the Script Action and replace it with a new one. Your choice. The contents will be very simple:
If you want to stick with Blockly, use an Inline Script block with the following: