i try really hard to dig into all this widget stuff. There are many examples, but often an example does not give any explanation. Actual i have two questions:
1.) If i add a property to a widget the is the type key. What types are possible. Until now i found only TEXT
2.) How do i implement loops. The Sonos widget uses loops, but from the example it is hard to understand the general idea. Is there somewhere a documentation about this.
You’re right, all those new cool features seem to still lack proper documentation, I also had a hard time when I started digging into OH3.
Although I’m not to deep into this yet, I’ll try to help you by trying to read the code
1.) there seem to be: TEXT, INTEGER, BOOLEAN and DECIMAL
2.) hm, more complicated than I thought. Even though I probably shouldn’t do this, I’ll try to explain what I read from the code (and to learn about it myself ):
The general idea is to display components in a “foreach” manner, i.e. iterate over a source and display a component(-tree) for each element. For this you need to set a component “oh-repeater” and set its config:
sourcetype, possible values:
range: iterate over numbers, additional config for this:
rangeStart: number to start from
rangeStop: number to stop at
rangeStep: number to add with each iteration
itemsWithTags: iterate over all items with certain tags, additional config:
itemTags (required): comma separated list of tags (to be confirmed)
fetchMetadata: comma separated list of metadata (to be confirmed)
itemsInGroup: iterate over items in a specific group
groupItem: group to get the items from
itemTags: comma separated list of tags (to be confirmed)
fetchMetadata: comma separated list of metadata (to be confirmed)
itemStateOptions: iterate over predefined states an item can be set to
itemOptions: item to get states from
itemCommandOptions: iterate over predefined commands an item can be set to
itemOptions: item to get commands from
unset: iterate over a provided array of string
in: array of string to iterate over
for: name of the variable to assign current iteration value to (see below) (though from the code it seems there’s more to it, perhaps an array of variables is possible, didn’t quite get that yet)
filter: boolean expression (without preceding “=“) to filter for
map: expression returning value to use instead of original array’s value
Styling options:
listContainer (boolean): if true, wrap each resulting component(-tree) in a <ul> tag
fragment (boolean): if true, do not wrap component(-tree) in any container tag (ignored of listContainer is true)
none of the above present: wrap in <div>
containerClasses: classes to apply to <ul> or <div>
containerStyle: styles to apply to <ul> or <div>
Then, In child components the following variables are available to be used in a expression:
loop.<name_of_for_value_in_config>: iteration’s current value
loop.<name_of_for_value_in_config>_idx: iteration’s current index
While writing this up I found a bunch of examples as part of the original pull request: here
Though please note that “vars” has been replaced by “loop”.
Wow, this ended up to as quite some cheatsheet… Though no limit or warranty to the information given here
BUT: Need to mention, that changes of the filter expression in the widget editor does not always trigger an update on the preview. This confused me at first.
So I had right expressions multiple times already without noticing it
Then I started playing with:
filter: true and filter: false
to convince myself, that the filter feature does affect the iteration at all.
Once this has worked; I’ve started with the “complications” …
Hint:
If the filter expression seems not working properly: Save the widget code and reload it …
BTW: Any idea how to access the “parent equipment group” of an item found by:
sourceType: itemsWithTags
itemTags: <tag>
If the found items have all the same label (“Temperature”, “Battery low”, etc.) it would be nice to determine, what equipment group in the model is belongs to.
EDIT: @ysc, may I ask you directly if there is an option deriving and finally accessing the equipment (and/or location) parent group item a certain tagged-item belongs to?
Obvious use case is selecting items with specific state having a certain semantic class attached to it:
LowBatteryand*.state == "ON"
Alarmand*.state == "ON"
Statusand*.state == "OFFLINE"
OpenState …
… and then display them on UI elements enriched by information (mainly the Label) taken from the parent semantic group(s) (equipment, location) of the items found by class and filter…