jq is notorious for problem with quote and parsing from one liners like this without a full shell. jq solves this by allowing you to define your arguments and variables before wrapping them in your query.
some examples here
you can look up the --arg and --argjson functions of jq. I’ve battled this myself in the past.
I begin to wonder if you can get hold of this info directly in DSL.
I don’t have a suitable Item to try it out on, with real stateDescription options … but try this and see what you get
var sd = LG_TV0_Channel.getStateDescription
logInfo("test", "object {}", sd)
logInfo("test", "options list {}", sd.getOptions)
I thought you were asking for an NGRE example . Here is how you could get an option using the rules DSL…
rule "Test DSL rule"
when
System started
then
logWarn("Rules", "Test: Start")
var current_label
val state_description_options = Lock_EntranceFront.stateDescription.options
state_description_options.forEach[option|
if (option.value == Lock_EntranceFront.state.toString()) {
current_label = option.label
}
]
logWarn("Rules", "current_label: '{}'", current_label)
logWarn("Rules", "Test: End")
end
Though I would still recommend using a shell script like you have done already. As mentioned, getting the quotes right and all the escaping becomes challenging. But I wanted to mention that it is possible to run commands in a shell so you can get pipes and redirects and such.
NOTE: There is no reason to sudo -u openhab, it’ll already be running as user openhab.
rule "Test DSL rule"
when
System started
then
logWarn("Rules", "Test: Start")
var current_label
val state_description_options = WebOS.stateDescription.options
state_description_options.forEach[option|
if (option.value == WebOS_state.state.toString()) {
current_label = option.label
}
]
logWarn("Rules", "current_label: '{}'", current_label)
logWarn("Rules", "Test: End")
end
where WebOS is like this, but with a lot more value/ label lines
That’s a bit curious; in a logInfo() curly braces get replaced with the object following. Importantly, the curly braces do not themselves appear in the output.
Looks like that doesn’t work with logWarn.
Let’s spell it out
logInfo(“test”, "result " + current_label.toString)
openhab> smarthome:status WebOS ["http://209.12.145.41:8080/rest/items/LG_TV0_Channel", "3_3_93_93_233_401_1", "{pattern=%s, readOnly=false, options=[{"value":"0_2_1_0_2_0_0","label":"1 - S 02"},{"value":"0_4_3_0_4_0_0","label":"3 - S 03"},{"value":"0_6_5_0_6_0_0","label":"5 - S 04"},{"value":"0_8_7_0_8_0_0","label":"7 - S 05"},{"value":"0_10_9_0_10_0_0","label":"9 - S 06"},{"value":"0_12_11_0_12_0_0","label":"11 - S 08"},{"value":"0_14_13_0_14_0_0","label":"13 - S 09"},{"value":"0_16_15_0_16_0_0","label":"15 - S 10"}......................
WebOS_state and LG_TV0_Channel output the same string. I will stop using WebOS_state item because is confusing
One more question how do I post the result to an item?
I tried WebOS_label.postUpdate( current_label )
and I get an error 19:23:34.562 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'Test DSL rule': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.postUpdate(org.eclipse.smarthome.core.items.Item,org.eclipse.smarthome.core.types.State) on instance: null
Also when savind the rule there is a validation message 19:24:45.829 [INFO ] [del.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model '(7)_webos.rules', using it anyway: Type cannot be derived