Rule to control items via MQTT? Is it necessary?

This is a pain I just can’t figure out.
I have my OH2 up and running with things, items and so on.
Now I want to use MQTT to control my items. So I have MQTT running, I have a PUBSUB programme running (MQTTFx) and all is working great.
I have rules to control my items, from a MQTT topic, but is this really neccessary?
I mean, can’t I somehow control my items directly with MQTT topics?

You may configure your items directly with a switch connected to a channel like stated in the docs:

http://docs.openhab.org/addons/bindings/mqtt1/readme.html

Item itemName { mqtt="<direction>[<broker>:<topic>:<type>:<trigger>:<transformation>]" }

For example to switch a relay connected to an ESP8266 with ESP Easy firmware you would configure a switch item like:

Switch Relay <switch> {mqtt=">[mosquitto:/esp8266two/gpio/4:command:ON:1],>[mosquitto:/esp8266two/gpio/4:command:OFF:0]"}

Then put that item on a sitemap

Switch item=Relay label="my Relay"

and happy switching :grinning:

No need for any rules in this case.

Thanks for the info @sihui
But if you have an existing item, with a Elko/IHC binding, and you want to control that same item but from an MQTT topic, what then?
Can I code one item to be controlled from both “IHC binding” and from “MQTT topic” ?
Example:

	Switch office_ceiling "Office ceiling" <light> (Office, Lights) [ "Switchable" ]  {ihc="59995" }

Controlled from MQTT

// **** Office MQTT ****
rule "Office MQTT loft"
	when
		Item office_light changed from OFF to ON
	then
		sendCommand(office_ceiling, ON)
	end
// **** Office MQTT ****
rule "Office MQTT off"
	when
		Item office_light changed from ON to OFF
	then
		sendCommand(office_ceiling, OFF)
	end

Where office_light is my MQTT item, and office_ceiling is my IHC binding item.

You could do it with a rule or just map the item to two bindings, one example is here:

There are also examples in this forum how to do that with MQTT, I don’t have time right now to search for it, please use the search button :grinning::sunglasses:

Thanks @sihui
You have done more than enogh, the post you found, is the answer to my prayer.
Guess I didn’t use the search bar correct :slight_smile:

1 Like