Changing item label

Hi,
I’m using the following code to change the item label dynamically in my DSL rules:

n_Markise_Proxy.label = "Markise " + nInvert + "%"

I’m wondering how to achieve the same with jsr223/Jython code. I tried the following but get an error that label is not an valid attribute

items["n_Markise_Proxy"].label = "Markise " + nInvert + "%"

**AttributeError: 'org.eclipse.smarthome.core.library.types.PercentTy' object has no attribute 'label'**

Thanks /Stefan

items["n_Markise_Proxy"] provides the state of the n_Markise_Proxy Item. To get the Item itself, use…

my_item = itemRegistry.getItem("n_Markise_Proxy")

To then change the Item’s label, use…

my_item.setLabel("Markise {}%".format(nInvert))

From your example though, I question the need for this, since it looks like you are adding state information into the label. I’ve provided the how, maybe you could fill in the why?

1 Like

Hi Scott - many thanks, you saved my day :slight_smile:

I fully agree with you - this is not the “standard” approach. I prefer the GUI design when I’m adding the state value into the label for this particular case.
I also use it in order to add the warning symbol as in the picture below dynamically.

1 Like

I have done something similar by having 2 sitemap elements with a different label or icon each and display only one based on whatever threshold or state is required. This only works for 2 or 3 states before it gets unmanageable, but I’ve never needed more than that.

1 Like