- Platform information:
- Hardware: Raspberry Pi 3 B+ / Pi 4 8Go
- OS: openHABian 3.0.1
- Java Runtime Environment: OpenJDK Runtime Environment Zulu11.45+27-CA (build 11.0.10+9-LTS)
- openHAB version: 3.0.1
- Issue of the topic: From OH3 rule, using ECMAScript, I want to retrieve the type of the data of the item that triggerred the rule
Hi everybody,
When writing a script for a rule, I want to get the type of the data from the triggeringItem. Here the code I wrote for testing purpose. It shows various information for the item, the channel and the thing from the triggeringItem. I have some problems with the last lines (mainItemType and itemTypeExtension values). When using RFLink items, the channelTypeUID is ‘rflink:temperature’ or ‘rflink:humidity’. But when I use Z-Wave items, the channelTypeUID is ‘zwave:sensor_temperature’, ‘zwave:sensor_luminance’, etc. So, for temperature, I’m getting ‘temperature’ in the first case and ‘sensor_temperature’ in the second case
Question: Is there a way to get the type in a ‘canonical form’, i.e. the same ‘type’ for all temperature sensors ? I think it’s possible because without doing anything special, the UI is able to append “°C” when displaying temperature values for both items.
Thanks for your help,
Manuel
–
// Get native logger
var logger = Java.type(“org.slf4j.LoggerFactory”).getLogger(“org.openhab.model.script.Rules.Experiments”);// Get infos on the Item that caused the event
logger.info("*** triggeringItem = "+triggeringItem);
var itemName = triggeringItem.name;
var itemState = triggeringItem.state;// -----
// Want reference for
var ItemChannelLinkRegistry = Java.type(‘org.openhab.core.thing.link.ItemChannelLinkRegistry’);
var ThingRegistry = Java.type(‘org.openhab.core.thing.ThingRegistry’);// OSGI
var Bundle = Java.type(‘org.osgi.framework.Bundle’);
var FrameworkUtil = Java.type(‘org.osgi.framework.FrameworkUtil’);// Since ItemChannelLinkRegistry & ThingRegistry are in the same bundle we can use either of them
var thingBundle = FrameworkUtil.getBundle(ItemChannelLinkRegistry.class);
var bundleContext = thingBundle.getBundleContext();// Get Service
var thingRegistryRef = bundleContext.getServiceReference(ThingRegistry.class);
var thingRegistryImpl = bundleContext.getService(thingRegistryRef);// Get Service
var itemChannelLinkRegistryRef = bundleContext.getServiceReference(ItemChannelLinkRegistry.class);
var itemChannelLinkRegistryImpl = bundleContext.getService(itemChannelLinkRegistryRef); // Instance of ItemChannelLinkRegistry// Get linked Things from given Item via ItemChannelLinkRegistry
var channelUID = itemChannelLinkRegistryImpl.getBoundChannels(itemName).toArray()[0];
var thing = itemChannelLinkRegistryImpl.getBoundThings(itemName).toArray()[0];
logger.info(" - Thing ‘" + thing.getLabel() + "’ with UID ‘" + thing.getUID() + "’ is ‘" + thing.getStatus() + "’");
logger.info(" - ChannelUID ‘"+channelUID+"’ with ThingUID ‘"+channelUID.getThingUID()+"’");var channelTypeUID = thing.getChannel(channelUID).getChannelTypeUID();
logger.info(" - ChannelTypeUID ‘"+channelTypeUID+"’");var ChannelTypeRegistry = Java.type(‘org.openhab.core.thing.type.ChannelTypeRegistry’);
var channelTypeRegistryRef = bundleContext.getServiceReference(ChannelTypeRegistry.class);
var channelTypeRegistryImpl = bundleContext.getService(channelTypeRegistryRef);var channelType = channelTypeRegistryImpl.getChannelType(channelTypeUID);
logger.info(" - ChannelType ‘"+channelType+"’ with ItemType ‘"+channelType.getItemType()+"’ and Kind ‘"+channelType.getKind()+"’ and Category ‘"+channelType.getCategory()+"’");var mainItemType = org.openhab.core.items.ItemUtil.getMainItemType(channelTypeUID);
var itemTypeExtension = org.openhab.core.items.ItemUtil.getItemTypeExtension(channelTypeUID);
logger.info(" - ChannelType → main ‘"+mainItemType+"’, extension ‘"+itemTypeExtension+"’");