[Solved] State from Channel object?

channels
Tags: #<Tag:0x00007fe051b3ac48>

(Michele) #1

For a binding, Java no rules, how can I read the status of a channel?

In this way I read ChannelType

if( channel.getAcceptedItemType().equals("Number:Temperature") ) {
                        // do stuff
}

State/value ?


(Hilbrand Bouwkamp) #2

Do you mean state or status? Those are different things. I think we can better help you if you explain what you want to achieve.


(Michele) #3

Hello,
Thanks for the reply.
I attach a piece of code.
After 500ms scheduler.schedule I update the status with updateState(channel.getUID(), new StringType(PressureState.RELEASED.toString()));

case PRESSED_EXT:
                    // TODO send more EXT PRESSURE messages every 500ms untile RELEASE_EXT command
                    if (isCENPlus) {
                         updateState(channel.getUID(), new StringType(PressureState.RELEASED.toString()));
                    } else {
                        // Alfa 4 bridgeHandler.gateway.send(CENScenario.virtualStartPressure(deviceWhere,
                        // buttonNumber));
                        scheduler.schedule(() -> { // let's schedule a CEN virtual ext pressure OWN message
                            logger.debug("==OWN:ScenarioHandler== # " + deviceWhere
                                    + " sending CEN virtual ext pressure...");
                            updateState(channel.getUID(), new StringType(PressureState.RELEASED.toString()));
                        }, EXT_PRESS_INTERVAL, TimeUnit.MILLISECONDS);
                    }
                    break;

I have to add the if condition, because in the meantime the value could be changed.
channel.getState() does not work

                        scheduler.schedule(() -> { // let's schedule a CEN virtual ext pressure OWN message
                            if (channel.getState() == PressureState.PRESSED) {
                                logger.debug(
                                        "==OWN:ScenarioHandler== # " + deviceWhere + " sending CEN virtual release...");
                                updateState(channel.getUID(), new StringType(PressureState.RELEASED.toString()));
                            }
                        }, EXT_PRESS_INTERVAL, TimeUnit.MILLISECONDS);

(David Graeff) #4

openHAB does not cache the state for you. You must create your own Map<String,State> for channelID->State caching in your thing handler. There are some cache related classes in the core to assist you.

Cheers, David