I’ve implemented the logic discussed above for both States and Commands, so that they are unquoted if they are pure numbers (with an optional sign and decimal separator) or a “known enum keyword”. Everything else will be quoted in triggers. In the action script, no reformatting is done, it’s uses as-is.
I’ve now also handled formatting when there are multiple triggers. This file:
rule "test"
when
Item test1 changed or
Item test2 changed or
Item test3 changed or
Item test4 changed or
Item test5 changed or
Item test6 changed
then
switch(triggeringItemName) {
case "test1" : if(example1.state == ON) stuff.sendCommand(ON)
case "test2" : if(example2.state == ON) stuff.sendCommand(ON)
case "test3" : if(example3.state == ON) stuff.sendCommand(ON)
case "test4" : if(example4.state == ON) stuff.sendCommand(ON)
case "test5" : if(example5.state == ON) stuff.sendCommand(ON)
case "test6" : if(example6.state == ON) stuff.sendCommand(ON)
}
end
..serializes to:
rule "test"
when
Item test1 changed or
Item test2 changed or
Item test3 changed or
Item test4 changed or
Item test5 changed or
Item test6 changed
then
switch(triggeringItemName) {
case "test1" : if(example1.state == ON) stuff.sendCommand(ON)
case "test2" : if(example2.state == ON) stuff.sendCommand(ON)
case "test3" : if(example3.state == ON) stuff.sendCommand(ON)
case "test4" : if(example4.state == ON) stuff.sendCommand(ON)
case "test5" : if(example5.state == ON) stuff.sendCommand(ON)
case "test6" : if(example6.state == ON) stuff.sendCommand(ON)
}
end
I’ve also done a test on a managed rule (that doesn’t come from a .rules file), which looks like this in “old YAML”:
configuration:
sourceItem: DemoColor
triggers:
- id: "1"
configuration:
itemName: DemoColor
previousState: "false"
state: "true"
type: core.ItemStateChangeTrigger
conditions: []
actions:
- inputs: {}
id: script
configuration:
type: application/vnd.openhab.dsl.rule
script: |
logInfo("Rule","mode TV")
// STOP Sonos salon
SonosSalonStop.sendCommand(ON)
type: script.ScriptAction
The generated DSL looks like this:
rule "Mode TV template based rule"
when
Item DemoColor changed from "false" to "true"
then
logInfo("Rule","mode TV")
// STOP Sonos salon
SonosSalonStop.sendCommand(ON)
end
“false” and “true” in the trigger are quoted, but they aren’t keywords, so I assume that it is correct?
The generated YAML for the same rule is:
version: 1
rules:
"122fc6f8bb":
template: mode-tv-yaml
templateState: INSTANTIATED
label: Mode TV template based rule
tags: []
description: This rule template is parsed using the "old" YAML parser
visibility: VISIBLE
config:
sourceItem: DemoColor
configDescriptions:
sourceItem:
context: item
description: The source Item whose state to monitor
label: Source Item
required: true
type: TEXT
readOnly: false
multiple: false
advanced: false
verify: false
limitToOptions: true
actions:
- id: script
config:
type: DSL
script: |
logInfo("Rule","mode TV")
// STOP Sonos salon
SonosSalonStop.sendCommand(ON)
type: script.ScriptAction
inputs: {}
triggers:
- id: "1"
config:
itemName: DemoColor
previousState: "false"
state: "true"
type: core.ItemStateChangeTrigger
Please note, this rule has been created from a template, which is why it contains a configuration description. The “old YAML” format don’t show those.
I should probably do some more work on the generated YAML with regard to hiding empty stuff. templateState should also be hidden, it’s a RuleEngine managed state. visibility should probably also be hidden if VISIBLE, as that is “standard” for almost every rule.
I’ll probably have to make a choice when exporting template-based rules to YAML, where you can either export the “template stub” (without triggers, conditions and actions, only containing “the basics”, the configuration and the template reference) or as a “standalone” where I strip the configuration and config description stuff. But, when viewed in the “Code” tab, it should probably be closer to this?
Since DSL doesn’t support templating, exporting to DSL will automatically mean to export it as “standalone”.