Hello. Has anybody used variables im Widget component oh-context, which contains a formula?
I use 2 variables to calc multiple used expressions in sub components in an Widget.This Widget is included in a page. When i First time call / Start this widget (I show it in the sidebar), the variables are empty (I assume null).
My oh-context:
When I for debugging purpose show the value of this variable in a label component, at first call after starting my browser I see nothing, After switching to another page and selecting this page again, I see a 1.
For me it looks like an error, but want exclude own errors first.Thanks, Siggi.
Context variables definitely are calculated initially on page load. I cannot replicate what you are describing (I am on a recent snapshot, but I don’t think there have been any updates to the context since 4.2.1).
It may well be something about exactly where in a page you have your widget. Can you either show the whole widget and page yaml configurations, or create a minimal example that shows the problem so I can test it out?
Hello Justin, find below my widget. I only deleted all other elements form my widget and kept the structure.
When I start my browser (Firefox or Edge), this is what I see. There should be two “1” at Green arrows, first is output of direct calculated sign of above number, second should be via variable.
Starting row 95 you have to change the item name to any number item (mine is a power item).
When I’m working in Widget Editor, all is visible. But when including widget in a page … load error …
Thanks for checking this! Siggi
That used to be true, but in more recent updates, the state values available in the widgets have been expanded. If you put =items.Some_Numer_Item in the developer side-bar widget tester, you’ll see that numericState can now be a viable option. For example:
Numeric Quantity items will even add the unit as a return value.
Thanks. With this I can replicate your issue, so it’s not jsut your system doing something strange.
Ok, that is very weird. I’m not sure what’s going on here. I’ll poke around for a little while and see what I can find out.
Edit: I see what’s going on now. The variables are being calculated, but in the initial calculation, for some reason, the items object isn’t being properly injected, or the expression is waiting for the return from the call to get the item state.
For now, this is a bug. I’ll see if I can put together a quick fix, or at least file an issue.
I’m not 100% sure, but would it be possible for you to change afterwards the item value and check if the variable will also be changed. I had the case one time, that the value changed to a negative value and then normally the color should have changed from green to red (in my fully Widget). But it did not change … So it could be that not only with first page call something is not triggered, it can also be with following changes.
That would be the correct behavior for a context variable, in my opinion. What you set in the context definition is just a default value for the variable. All other changes to the variable’s value after the initialization should then come from within the context of the widget, not from outside factors. If you want a value to change each time an item changes, that’s more appropriate as a function.
Constants - evaluated on widget build, cannot change
Variables - evaluated on widget build, values only changed by component variable actions
Functions - evaluated on every call and re-evaluated when dependent values change