openhab / openhab-js

openHAB JavaScript Library for JavaScript Scripting Automation
https://www.openhab.org/addons/automation/jsscripting/
Eclipse Public License 2.0
38 stars 31 forks source link

[items] state of an item created by replaceItem() not visible in BasicUI. #118

Closed JacekKac closed 2 years ago

JacekKac commented 2 years ago

items.replaceItem({type: 'String', name: 'RadiatorACx_RunningState', label: 'Bieżący stan uruchomienia', tags: ['Roomx'], category: '', groups: ['gRoomx_termostat'],});
console.log("Running state:" + items.getItem('RadiatorACx_RunningState').state);
==> /var/log/openhab/openhab.log <==
2022-05-21 09:13:43.914 [INFO ] [utomation.script.file.ItemCreator.js] - Running state:idle

basicUI image html:


<div class="mdl-form__row mdl-cell mdl-cell--6-col mdl-cell--8-col-tablet ">
    <span class="mdl-form__icon">
        <img data-icon="" src="/icon/none.png">
    </span>
    <span class="mdl-form__label">
        Bieżący stan uruchomienia
    </span>
    <div class="mdl-form__control mdl-form__text" data-control-type="text" data-item="RadiatorACx_RunningState" data-has-value="false" data-widget-id="RadiatorACx_RunningState">

    </div>
</div>

generated html shows "data-has-value="false" which is not true. It acts the same for number and string items, only switch items show "switch slider". Is it neccesary to add metadata with statedescription ?

openhab-js installed and updated manually.

JacekKac commented 2 years ago

adding a value: " " config: pattern: "%.1f °C" into a metadata statedescriptions is solving the problem, but is there a way to skip the metadata and redender the value as default like it is in normal item configuration (without obligatory statedescription)

florian-h05 commented 2 years ago

I can reproduce that behaviour, but I don't have a solution in my mind for this. Maybe @digitaldan has any idea?

digitaldan commented 2 years ago

It looks like items defined in files that have a label with a state pattern ( like [%s]) get a default StateDescription assigned to them based on type, but using the rest API or the API here to create items does not auto generate this, so indeed you need to add that yourself with metadata. The basic UI code is dependent on this for rendering.

florian-h05 commented 2 years ago

Okay, should we consider setting a default StateDescription depending on the Item type for the case that the user does not define one?

digitaldan commented 2 years ago

Okay, should we consider setting a default StateDescription depending on the Item type for the case that the user does not define one?

I'm a little on the fence on this one since the REST API does not provide this convenience, and our main WebUI does not do this automatically either ( i just tested it) . It seems like its just the older file implementation for items which does this, and i can't find where that code lives in core, so i'm not sure what the logic is. My vote is we follow the REST API and WebUI and not try and do this automatically for now.

florian-h05 commented 2 years ago

I'm a little on the fence on this one since the REST API does not provide this convenience, and our main WebUI does not do this automatically either ( i just tested it).

Okay, that's good to know.

Maybe we should add a note about this to the README and update examples to show how to set stateDescription?

digitaldan commented 2 years ago

Maybe we should add a note about this to the README and update examples to show how to set stateDescription?

Yeah, i think an example would be awesome. 👍

florian-h05 commented 2 years ago

Closing as #129 mentions the required stateDescription in the docs.