Hi everyone,
Renaming every proposed channel Item when adding a Thing to the semantic model can get repetitive. I’ve made a small Violentmonkey userscript that does this automatically in Main UI’s “Create Equipment from Thing” dialog.
Naming Format
The script converts proposed names to:
ThingInCamelCase_ChannelInCamelCase
For example:
| Before | After |
|---|---|
| airQ_GuestRoom_Status_of_Sensors | AirQGuestRoom_StatusOfSensors |
| airQ_GuestRoom_Show_WLAN_Status_with_LED | AirQGuestRoom_ShowWlanStatusWithLed |
Both parts begin with a capital letter, technically PascalCase. Acronyms are normalized: WLAN becomes Wlan, and LED becomes Led.
What It Does
- Uses the Equipment’s Name field as the prefix.
- Extracts the channel portion from openHAB’s proposed Item name.
- Updates proposed channel Item names as their fields appear.
- Leaves the Equipment name and channel labels unchanged.
It runs locally in your browser. No API token or openHAB server changes are required. It does not rename existing Items or create Items automatically.
Installation
- Install the Violentmonkey browser extension.
- Create a new userscript and paste in the script shared with this post.
- Adjust the
@matchlines to your openHAB address, including the protocol and port. For example:
// @match http://openhab.local:8080/settings/things/* - Save and enable the script, then reload openHAB.
- Open a Thing and choose Add to Model.
The match covers Thing settings pages so the script can load before you navigate into the dialog. Renaming runs only on the /add-to-model page.
Limitations
The current version expects English Name labels and openHAB’s default EquipmentName_Channel_Name proposals. It supports ASCII letters and digits; accented or non-Latin names may need adjustments.
Feedback and improvements are welcome, especially for other UI languages and openHAB versions!
// ==UserScript==
// @name openHAB Equipment Item Names
// @namespace openhab.local
// @version 1.0.0
// @description Use ThingInCamelCase_ChannelInCamelCase for proposed channel Item names.
// @match http://192.168.69.10:8080/settings/things/*
// @match http://openhab.local:8080/settings/things/*
// @match http://c3po:8080/settings/things/*
// @grant none
// @run-at document-idle
// ==/UserScript==
(() => {
'use strict';
const channelNames = new WeakMap();
let scheduled = false;
let updating = false;
function toCamelCase(value) {
const words = value
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2')
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
.split(/[^a-zA-Z0-9]+/)
.filter(Boolean);
return words.map(word => {
const lower = word.toLowerCase();
return lower[0].toUpperCase() + lower.slice(1);
}).join('');
}
function isNameInput(input) {
if (input.type === 'hidden' || input.disabled || input.readOnly) return false;
const row = input.closest('.item-content, .item-input, .form-group');
const label = row && row.querySelector('.item-label, label, .item-title');
if (label && label.textContent.trim() === 'Name') return true;
return Array.from(input.labels || []).some(label => label.textContent.trim() === 'Name');
}
function setValue(input, value) {
if (input.value === value) return;
const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value').set;
setter.call(input, value);
input.dispatchEvent(new Event('input', { bubbles: true }));
input.dispatchEvent(new Event('change', { bubbles: true }));
}
function updateNames() {
scheduled = false;
if (!/^\/settings\/things\/[^/]+\/add-to-model\/?$/.test(location.pathname)) return;
const inputs = Array.from(document.querySelectorAll('input')).filter(isNameInput);
const thingInput = inputs.shift();
if (!thingInput || !thingInput.value.trim()) return;
const thingName = thingInput.value.trim();
const prefix = toCamelCase(thingName);
if (!prefix) return;
updating = true;
try {
for (const input of inputs) {
let channel = channelNames.get(input);
if (!channel) {
const proposed = input.value.trim();
if (!proposed.startsWith(thingName + '_')) continue;
channel = toCamelCase(proposed.slice(thingName.length + 1));
if (!channel) continue;
channelNames.set(input, channel);
}
setValue(input, prefix + '_' + channel);
}
} finally {
updating = false;
}
}
function scheduleUpdate() {
if (updating || scheduled) return;
scheduled = true;
requestAnimationFrame(updateNames);
}
new MutationObserver(scheduleUpdate).observe(document.body, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ['value']
});
document.addEventListener('input', scheduleUpdate);
document.addEventListener('change', scheduleUpdate);
window.addEventListener('popstate', scheduleUpdate);
scheduleUpdate();
})();