is there any way to set “item.label” in JS code (OH4.3)?
If you want to create a new item and are asking how to set the label:
if (items.MyNewItem === null) {
items.addItem({
type: 'Switch',
name: 'MyNewItem',
label: 'My Label of MyNewItem',
groups: ['gMyGroup'],
category: 'error',
tags: ['Point']
});
Thanks - but I want to change the label of an existing item by JS code
e.g. items.MyItem.label=“xxx”, what does not work - at least in my installation
I am not sure, but try to use the code for existing items. It might work that the item gets updated.
At least, openhab‘s rest API works in this way. So there is a chance that it works by code, too.
Please use an unlinked test item.
I tried this:
rules.JSRule({
name: "JS DEBUG1",
triggers: [
triggers.ItemStateChangeTrigger ('set_a_value','OFF','ON'),
],
execute: function(event) {
items.getItem('set_a_value').label='xxx'
}
});
But label text was not changed. I think, I’ve seen somewhere in the doc, that JS does not support setting the label. I though someone know a trick to do it anyway.
No, it doesn‘t work - I tested it. You need to call openhab‘s rest API. See the API explorer:
PUT
/items/{itemname}
Adds a new item to the registry or updates the existing item.
My current solution is using a small rules file which does only change the label text.
If your items are not managed by UI you cannot change the item by rule
The openhab-js library does a lot of work to make sure that you only ever deal with native js objects instead of of the underlying java objects. Usually this much more convenient for the user. In this case, however, it means that you don’t have direct access to the label of the underlying item. JS lines like this:
items.getItem('set_a_value').label='xxx'
only change the javascript object; this does not propagate back to the java object.
What you are looking for is the items
object replaceItem()
method:
This will take a js item configuration and make an OH item from it. If an item with that name already exists, it will delete the initial item and recreate it.
So, your steps would be:
- capture the item config of the item you want to modify
- make the modifications
- use the modified config to create the new version of the item with
replaceItem()
var itemToChange = items.getItem('MyItem')
itemToChange.label = "New Label"
items.replaceItem(itemToChange)
Thank for your code. However tried it, but the label did not change. What could go wrong?
items.getItem('MyItem').rawItem.setLabel('New_Label');
Ah, sorry. I thought the replaceItem method automatically converted a js item class into an item config object, but looking at the code, I see that it doesn’t.
In that case, you have to actually work with the setLabel()
method of the direct Java Item object, which you can access using the rawItem
property:
var itemToChange = items.getItem('MyItem').rawItem
itemToChange.setLabel('New Label")
var itemToChange =items.getItem('set_a_value').rawItem
itemToChange.setLabel='yyy'
items.replaceItem(itemToChange )
Now the Item “set_a_value” is deleted in the items collection - not replaced
Yes. Do not add the extra line with the replaceItem()
method when you are using the raw java item. Use only the lines
Thanks - this results in a “blank” label. But the item is not deleted
Perfect - that worked. Many thanks to all
Sorry - that work also, after I corrected my typo