Openhab 4 can we change dynamically items enabled/disabled in OpenHab UI with rules?

Hi,
I have a very simple scenario:
When the window is open (contact sensor), then the slider of the roller shutter should be disabled (why? well it will unroll over the window and “kill it”)

image

I was looking for a rule to update some metadata of items:

Generated code

var itemMetadata;


if (items.getItem('Contact_sensor_living_room_window_Window_Contact_State').state != 'CLOSED') {
  console.info('DISABLE UI');
  itemMetadata = items.metadata.getMetadata('Roller_blind_living_room_Position', 'UI');
  itemMetadata = (itemMetadata === null) ? { value: '', configuration: {} } : itemMetadata;
  itemMetadata.value = 'readonly';
  items.metadata.replaceMetadata('Roller_blind_living_room_Position', 'UI', itemMetadata.value, itemMetadata.configuration);
} else {
  console.info('ENABLE UI');
  itemMetadata = items.metadata.getMetadata('Roller_blind_living_room_Position', 'UI');
  itemMetadata = (itemMetadata === null) ? { value: '', configuration: {} } : itemMetadata;
  itemMetadata.value = '';
  items.metadata.replaceMetadata('Roller_blind_living_room_Position', 'UI', itemMetadata.value, itemMetadata.configuration);
}

However seems it doesn’t work as expected, the slider stays active.

Did some researches and found this but I am not sure it’s really the same topic.

I could find the following topic: How to change a Channel to Read-Only dynamically?

Can someone help me there?

(Note: I want to keep using the openhab default UI auto generated based on the model, so no sitemap or whatever possible here)

Thank you

For any item you can define custom widgets that will be displayed as the default widget in auto-generated OH lists. This value is set in the item metadata under Default List Item Widget. In this case there is a very simple solution and you don’t even really need to create your own widget. You can use nearly all the default settings that are in place in that metadata. However, if you scroll down to the bottom of that page, there is a Visible field that takes a widget expression. The expression just needs to return a boolean result with true meaning the item will be visible and false meaning it will not. So in this case you want an expression based on the state of your window contact. Something like this:

=items.Contact_sensor_living_room_window_Window_Contact_State.state == 'CLOSED'
2 Likes

Hi @JustinG thank you for your help it works like a charm!

I share here the steps with some screenshots as it might help future readers (was not easy for me)



@JustinG is there a way to simply “disable” (make it read only) ? As a workaround I could define another item “Text” that display the value when the slider is disabled but well it’s a workaround :slight_smile:

Yes there is, just not through the UI fields on the metadata page. You’ll have to go back to that metadata page and then switch over to the the code tab. There you’ll see the yaml for the visible property you configured. But instead of visible you want to use disabled. So replace ‘visible’ with ‘disabled’ manually. Then you’ll need to switch around your expression because for this property, true is the disabled state and false is the enabled state. In this case just changing == to != should be sufficient.

1 Like