How to toggle channel (type of Switch) from binding

Hi all! I develop custom binding for radio light switcher, and have a question.

Now I can handle commands ON and OFF, which produce update channel state:

updateState(SWITCHER_CHANNEL, OnOffType.ON);
or
updateState(SWITCHER_CHANNEL, OnOffType.OFF);

Also I need to handle command TOGGLE, which change current state of channel (from ON to OFF, or from OFF to ON).

How I can read current state of channel? I could not find such a method.

Thank you.

Resolved with “trigger” channel.

Channel definition:

	<channel-type id="toggle-button">
		<kind>trigger</kind>
		<label>Toggle button</label>
		<description>Toggle button pressed</description>
	</channel-type>

And rule:

rule "Trigger button"
when
    Channel 'light:radio_switch:switcher5:toggle-button' triggered
then
    if(light_hall1.state == ON) {
        sendCommand(light_hall1, OFF)
    } else {
        sendCommand(light_hall1, ON)
    }
end

How do you use this toggle channel when a toggle event comes from your device?

If you are talking about how the binding is handling it, I’d react on either command (OnOffType.ON or OnOffType.OFF) in the handleCommand routine the same way!

Hi! You can see my example on github:

@Override
    public void messageReceived(NooliteMR1132Message message) {
        LOG.debug("MR1132 message received: {}", message);
        updateStatus(ThingStatus.ONLINE);
        int channel = message.getChannel();
        Bridge bridge = getThing();
        List<Thing> things = bridge.getThings();
        for (Thing th : things) {
            ThingHandler thHandler = th.getHandler();
            if ((thHandler != null) && (thHandler instanceof NooliteSwitcherHandler)) {
                if (((NooliteSwitcherHandler) thHandler).getChannelNum() == channel) {
                    ((NooliteSwitcherHandler) thHandler).updateValue(message.getStatusTogl());
                    if (message.getCommand() == CommandCode.SWITCHER_ON) {
                        ((NooliteSwitcherHandler) thHandler).updateSwitcher(OnOffType.ON);
                    } else if (message.getCommand() == CommandCode.SWITCHER_OFF) {
                        ((NooliteSwitcherHandler) thHandler).updateSwitcher(OnOffType.OFF);
                    } else if (message.getCommand() == CommandCode.SWITCHER_TOGGLE) {
                        ((NooliteSwitcherHandler) thHandler).toggleSwitcher();
                    }
                }
            }
        }
    }

public void toggleSwitcher() {
        triggerChannel(NooliteMR1132BindingConstants.TOGGLE_BUTTON_CHANNEL);
    }

Okay so when you get the SWITCHER_TOGGLE command you trigger your channel toggle_button and this triggers your rule that toggles the state of your switch channel?
Thanks for your answers and help.

Yes. Because I could not find a method for reading current state of channel from handler.

I do not understand why you can’t react on the received command within the binding. Even without “knowing” the state, a toggle just changes the state from ON to OFF or CLOSED to OPEN etc…
Just do the same as you did in the rule in the binding.

Can you give me an example? When I receive the TOGGLE command from device how can I find out which command I need to execute on the linked channel: ON or OFF?

If you are a that line:

((NooliteSwitcherHandler) thHandler).toggleSwitcher();

of the above posted code, you have already checked if the message you received is for the channel of the thing you are presently iterating. In other words you have found a thing that should react on the message.
Receiving a command via message should be the same as receiving a command via a button, the later are handled in the handleCommand routine.

Why not exchange the line above with:
handleCommand (thHandler.getChannelUID, ToggleType.Toggle)
since I’m not on an IDE, I’m not sure sure on “thHandler.getChannelUID” (might be th.getChannelUID), the ToogleType has to be made up.

In the handleCommand of your NooliteSwitcherHandler.java you could use an if like:

   if (command instanceof ToggleType) { 
       
}

in which you could set the channel to the opposite state. However you are missing a variable to hold such information (if I’m not mistaken)

In this case handleCommand gets called in both instances. When a message is received and when we press a button in OpenHab. But in case of an incoming message i just want to toggle the state and in case of a OpenHab command i want to toggle the state and send out a message. Is this possible?