[SOLVED] Starting scenes (or other rules) with Rule DSL for e.g. Scene Management

Hi,

I wanted to test scenes in OpenHAB. I found a way to start scenes with JavaScript using the following code:

rules.runRule('UID of scene', {})

The problem is that my complete scene management works with Rule DSL, but I believe there’s a workaround. However, I need some assistance, and I kindly ask for a hint.

The idea is to send the UID of the scene to a string item. This command will trigger the JavaScript rule to start the scene. In Rule DSL, it would look like this:

rule "Start a scene"
when 
	Item LightSceneDSL received command //String-item
then 
	rules.runRule(receivedCommand, {})
end

Can someone help me to translate this to JS, so that rules.runRule works.

This would be a great help, and I would be grateful.

BR Peter

In JRuby:

received_command(LightSceneDSL) { |event| rules[event.command].run }

Or if you prefer a more traditional syntax

rule "Start a scene" do
  received_command LightSceneDSL
  run { |event| rules[event.command].run }
end

Or in a UI rule, setup the trigger and put this inside the script action

rules[event.command].run
1 Like

The short answer is that you can’t run another rule using Rules DSL. You will have to use any of the other rules languages. @JimT showed how it works in jRuby. You already know how it works in JS Scripting.

I apologize for my English - I probably couldn’t describe my idea well.

I’m aware that I can’t reference other rules in Rule DSL. My idea is to create a “workaround” for this.

I found the method in JavaScript, but I wasn’t clear on how to use receivedCommand or how JavaScript works in general. The only thing I knew was that one line from the online help on how to start scenes.

Since I now have the rule from @JimT, I don’t need this anymore. Any change in the String Item StartRule now triggers the corresponding scene from Rule DSL. This “new rule” is, so to speak, the interface.

I’m putting this together so that someone looking for it understands what I was looking for in the first post.

For those who also don’t know JRuby: I created the following rule via the UI:

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: StartRule
    type: core.ItemCommandTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    label: löse die Szene aus
    configuration:
      type: application/x-ruby
      script: rules[event.command].run
    type: script.ScriptAction

In Rule DSL I now only have to use

StartRule.sendCommand('UID of the scene or rule')

Thank you very much
Best regards
PeterK

Hi,

just for completeness an example how to use this:

item:

String StartRule	"receives a string and starts this rule or scene"

Group:Switch:OR(ON,OFF)	Szenenschalter		"Group with one switch for each scene e.g. for Alexa"
Switch StartScene_UID1	"Trigges the scene with the uid: UID1"
Switch StartScene_UID2	"Trigges the scene with the uid: UID2"

rule

rule "Szenenschalter"
when
	Member of Szenenschalter received command ON
then
	// Stringmanipulation in order to get the uid out of the item name
	val String CommandString = triggeringItemName.toString.split('_').get(1)
	//Starting the scene 
	StartRule.sendCommand(CommandString)
end

The one additional JRuby-rule made in the UI as proposed by @JimT from here is still required, but appart from that, I can now start scenes from Rule DSL:

Best regards
Peter

It that’s all you are after, other alternative approaches include:

  • You can set the action of a widget in MainUI to execute a rule directly. There is an issue open to add something similar for Sitemaps but I don’t think any work has started on that yet.

  • If you want to be sneaky, you can click on the “Code” tab of the Scene and manually insert a trigger. You’d still have your StartRule Item but you wouldn’t need the separate rule.

  • You can create separate rules for each scene and not have to write any code at all. Set the StartRule trigger and possibly the condition and the use the call rule action.

  • As shown above, use a single rule to call the Scenes. This approach is pretty simple if the command is the rule UID of the scene to call. If can become more complicated if not. In Blockly it would look like this.

In JS Scripting:

rules.runRule(event.itemCommand.toString(), {});
1 Like

Nice… I did not expect that this will work. Thank you.