I have it defined in the props section (the item). This item contains a list of options that I want to display in a list of options when I press.
If I put the item directly, the list is filled in and the options appear. However, when I try to do it using props, I don’t succeed.
Definition:
props:
parameters:
- context: item
description: Plant for the query
label: Plants
name: item_plants
required: true
type: TEXT
I have moved this question over to the category for questions. The Add-on Marketplace category is reserved for publishing complete widgets, rules, or bindings that other users can download.
To your question: This is a known limitation. You cannot use expressions in the elements of a YAML array (the - followed by title:, icon:, and item:). Without knowing why you need this to be a dynamic property it is difficult to advise you on what the best workaround might be. For now, I would suggest just setting item to the text item_plants:
@JustinG First of all, thank you very much for your good answer. I would like to ask you if you could guide me so that I can save the two selected values in two different variables and also have the selected value / name appear in the arrows.
Excuse me for asking you such a direct question, I have had little success for a long time.
If I understand you correctly, you want the state of your item_plants item to show up just like your item_measures state. That goes back to this line:
after: =items[loop.selection.item].displayState
and the only problem with that line is that it looks like displayState is not defined for the item_plants item. If it is not defined for an item then the widget just uses an empty string "" so nothing is displayed.
Because of this, often when you are using a displayState you want to check if it exists first and then use the regular state if it does not (similar to your checked property in the popup).
If you are on the right track but I forgot to comment that my code has undergone an update.
Context: The items (“item_plants” and “item_measures”) have a series of options with their label = value.
I would like to be able to save the value of “item_plants” and “item_measures” in different variables for each when pressed and without having to change the state of the item (that is, without having to do action: command … somewhere) .
And the label of the options that appears as a value in after after having been clicked.
I am attaching code right now.
Thanks for everything and sorry for the inconvenience.
OK, that’s not too difficult. You can use expressions to build variable names just like you have many other strings throughout the widget (e.g., your popup class). In this case, it sounds like you just want one variable for item_plants and one for item_measures, so you can just:
Functions beyond that however, get a little more difficult. For two reasons:
There is no way to initialize a key in the vars object with default values. It will remain undefined until some component sets that key. So you will not see anything in the after slot of the plant and measures components until a selection is made. This can be solved in the exact same way as the solution above to displayState. You’ll need that everywhere the variable appears in a context where it must have a value.
There is now nothing in your script that actually passes those option values to items for openHAB to work with. You will have to add another component with an action that sends a command to an item. If you want that to be the new information from both plant and measurements selections then that probably will have to be a proxy_item that triggers a rule and the rule will have to parse all the information out of the proxy_item value and pass the commands onto the items from there.
Following your good indications I have managed to have the two dynamic variables. Regarding the title of the label, I have also managed to show it after having clicked on it. The latter, I have achieved it in a way that technically I know is not very correct but I have not been able to do anything better. The chosen solution has consisted of introducing the label in the variable with a separator in the middle, in this case an equal and later using a split, I attach lines in case someone is useful in the future.
The only problem I have is that the first time before selecting any value from the list the value undefined appears and I want a blank space to come out. I have tried in various ways to control it with logic but I can’t know how to match the value of the variable … I have tried with undefined but without success.
The results of this are undefined because before the repeater runs, of course, loop itself is undefined. You can check if a variable is undefined, but you cannot check if an undefined variable has a property. So whenever you have situation where you refer to a property of a variable that may be undefined, you usually have to check if the variable itself exists first. The final result looks something like this:
Here !! is a double negation that give you true if the variable exists (and isn’t false) and , gives you false if the variable is any falsy value (undefined is a falsy value). So if loop doesn’t exist the condition is already false and doesn’t even evaluate the second half of the test so there’s no error about checking for the property of an undefined variable.
This approach is interesting but for some reason I am not understanding why it does not work. It keeps showing “udefined” instead of a space or something else.
You don’t have to apologize for anything. A small advance, now undefined disappears but nothing ever appears and in the case of “measure” it keeps appearing, it is rare.
is undefined because before you’ve have clicked on anything, the variable vars["option_select_" + loop.selection_idx] doesn’t exist yet. That by itself is not an issue the widget would just render that as a null or in this case an empty after slot. However, you try to call the split() method on that non-existent variable and this results in an undefined error because the parser doesn’t even know what type of variable that is (because it doesn’t exist) let alone if it has a split() method. So the test you need to run isn’t on whether vars["option_select_" + loop.selection_idx]).split("=")[1] is undefined the test is whether or not vars["option_select_" + loop.selection_idx] exists yet.
Well maybe that was my problem and I didn’t explain it well, sorry.
This last thing that you indicate to me was one of my attempts when you initially guided me. This still doesn’t work surely there’s something missing … I just started a week ago with custom widgets … this is frustrating me.
The result is this now and I have selected both values and later they are not reflected either. How can I fix this OMG. Could it be a bug or maybe I am doing or understanding something wrong?
You were right, I’m going to leave my widget here in case someone gets this far and is useful. It has the purpose of making the query of item measurements visually. This is just the visual principle, then I intend to add a run button to run a rule or an exec command … I don’t know, I have to investigate / think.
I think it may have interesting things for people like me who are starting with custom widget since there is a lot of variety of examples and things used in the code … oh-repeater, dates … radio list selects … etc.
Greetings Justin, thanks for all your help, you have made my development much easier.
I am writing to know if you, with your wisdom, would know how to define that in the “Floor Plan” and “Measure” list, before selecting a value, the first one in the list should be the default.
Thanks for your time as always.