Promote mqtt2 to top of the article

Michael Ingraham 2019-07-06 12:41:25 -04:00
parent 5f87d12234
commit 94f3465eaa
1 changed files with 70 additions and 71 deletions

@ -28,31 +28,90 @@ The screenshot of an openHAB Sitemap below features a few Sonoff modules for lig
**Highly recommended:** If you are new to openHAB + MQTT, go through this tutorial: <br> **Highly recommended:** If you are new to openHAB + MQTT, go through this tutorial: <br>
⇨ [MQTT Binding - Getting Started 101](https://community.openhab.org/t/mqtt-binding-v1-11-getting-started-101/33958) ⇨ [MQTT Binding - Getting Started 101](https://community.openhab.org/t/mqtt-binding-v1-11-getting-started-101/33958)
Before continuing, please make sure you assigned **unique MQTT "Topics"** in the Sonoff-Tasmota configuration interface of each pf your devices. The default MQTT topic is "sonoff", in the examples below we will use names like "sonoff-A00EEA".
![Example Sonoff-Tasmota MQTT settings](https://community-openhab-org.s3-eu-central-1.amazonaws.com/original/2X/8/8fe9008fb24b0b70e6eddf7cf0f0c70c8ac21b92.png "Example Sonoff-Tasmota MQTT settings")
---- ----
If not done yet, you first need to **install and activate** the [MQTTv1](https://www.openhab.org/addons/bindings/mqtt1/)/[MQTTv2](https://www.openhab.org/addons/bindings/mqtt/)&ast;, the [MQTT action](https://www.openhab.org/addons/actions/mqtt/) and the [JsonPath transformation](https://www.openhab.org/addons/transformations/jsonpath/), e.g. via the openHAB Paper UI Add-ons section. If not done yet, you first need to **install and activate** the [MQTTv1](https://www.openhab.org/addons/bindings/mqtt1/)/[MQTTv2](https://www.openhab.org/addons/bindings/mqtt/)&ast;, the [MQTT action](https://www.openhab.org/addons/actions/mqtt/) and the [JsonPath transformation](https://www.openhab.org/addons/transformations/jsonpath/), e.g. via the openHAB Paper UI Add-ons section.
**&ast; MQTTv1 vs. MQTTv2 Binding Info:** **MQTTv1 vs. MQTTv2 Binding Information**
This article currently applies to the openHAB 1 version of the MQTT Binding (`mqtt1`). The openHAB community has just release a new native openHAB 2 MQTT Binding, which complies with enhancements and significantly changes. The openHAB community has released a new native openHAB 2 MQTT Binding, which complies with enhancements and significantly changes. Be aware that if you update your openHAB instance, the new MQTT binding will be installed and `mqtt1` will be uninstalled! This means that any MQTT openHAB automations in your openHAB environment will exhibit odd behavior or not operate at all. If you are using `mqtt1`, jump to the `mqtt1` section [below](#examples-for-integration-with-mqttv1).
Be aware that if you update your openHAB instance, the new MQTT binding will be installed and `mqtt1` will be uninstalled! This means that any MQTT openHAB automations in your openHAB environment will exhibit odd behavior or not operate at all. ## Examples for integration with MQTTv2 (`mqtt2`)
Fortunately, the `mqtt1` Binding can be reinstalled. To do so, turn on "Include Legacy 1.x Bindings" via PaperUI (Configuration > System) or set `legacy = true` in `addons.cfg`. Then reinstall the mqttv1 Binding. Please note that now that mqttv1 is a legacy binding, it will no longer receive updates or fixes. Installing both versions of the binding will allow you to migrate over time to be ready for the eventuality of mqttv1 end of life. **.things File:**
For users that intend to migrate to the new MQTT Binding some examples for the integration have been added [at the end of this article](#examples-for-integration-with-mqttv2). ```java
Bridge mqtt:broker:myMQTTBroker [ host="IPofBroker", secure=false, username="myUser", password="myPassword" , clientID="myMQTTClient" ]
{
Thing topic sonoff_TH_Thing "Light_TH" {
Channels:
Type switch : PowerSwitch [ stateTopic="stat/sonoff_TH/POWER" , transformationPattern="JSONPATH:$.POWER" , commandTopic="cmnd/sonoff_TH/POWER", on="ON", off="OFF" ]
Type string : Version [stateTopic="stat/sonoff_TH/STATUS2", transformationPattern="JSONPATH:$.StatusFWR.Version"]
Type string : Temperature [stateTopic="tele/sonoff_TH/SENSOR", transformationPattern="JSONPATH:$.AM2301.Temperature"]
}
}
```
**.items File:**
```java
Switch Switch_TH "Switch_TH" { channel="mqtt:topic:myMQTTBroker:sonoff_TH_Thing:PowerSwitch" }
String Switch_TH_Temperatur "Temperatur [%s °C]" <temperature> {channel="mqtt:topic:myMQTTBroker:sonoff_TH_Thing:Temperature"}
String Sonoff_Version "Sonoff Version: [%s]" <sonoff_basic> { channel="mqtt:topic:myMQTTBroker:sonoff_6_Thing:Version"}
```
**.rules File for the Maintenance Action:**
```java
// Work with a list of selected Sonoff modules
val sonoff_device_ids = newArrayList(
"sonoff-A00EEA",
//… add all your modules here!
"sonoff-E8A6E4"
)
// OR
// Work with the grouptopic, addressing ALL modules at once
//val sonoff_device_ids = newArrayList("sonoffs")
rule "Sonoff Maintenance"
when
Item Sonoff_Action received command
then
logInfo("sonoff.rules", "Sonoff Maintenance on all devices: " + receivedCommand)
val actionsBroker = getActions("mqtt","mqtt:broker:MyMQTTBroker") // change to your broker name!
for (String device_id : sonoff_device_ids) {
switch (receivedCommand) {
case "restart" :
actionsBroker.publishMQTT( "cmnd/" + device_id + "/restart", "1")
case "queryFW" :
actionsBroker.publishMQTT( "cmnd/" + device_id + "/status", "2")
case "upgrade" : {
actionsBroker.publishMQTT( "cmnd/" + device_id + "/otaurl", "http://sonoff.maddox.co.uk/tasmota/sonoff-DE.bin") // Replace DE with your country code or use sonoff.bin or other tasmota specialized firmware
actionsBroker.publishMQTT( "cmnd/" + device_id + "/upgrade", "1")
}
}
}
Sonoff_Action.postUpdate(NULL)
end
```
## Examples for integration with MQTTv1 (`mqtt1`)
Please note that now that `mqtt1` is a legacy binding, it will no longer receive updates or fixes. If you update your openHAB instance, the new MQTT binding will be installed and `mqtt1` will be uninstalled! This means that any MQTT openHAB automations in your openHAB environment will exhibit odd behavior or not operate at all. Fortunately, the `mqtt1` Binding can be reinstalled. To do so, turn on "Include Legacy 1.x Bindings" via PaperUI (Configuration > System) or set `legacy = true` in `addons.cfg`. Then reinstall the `mqtt1` Binding. Installing both `mqtt2` and `mqtt1` bindings will allow you to migrate over time to be ready for the eventuality of `mqtt1` end of life.
For users that intend to migrate to the new MQTT Binding some examples for the integration have been added [above](#examples-for-integration-with-mqttv2).
---- ----
Before continuing, please make sure you assigned **unique MQTT "Topics"** in the Sonoff-Tasmota configuration interface of each Sonoff module. The default MQTT topic is "sonoff", in the examples below we will use names like "sonoff-A00EEA".
![Example Sonoff-Tasmota MQTT settings](https://community-openhab-org.s3-eu-central-1.amazonaws.com/original/2X/8/8fe9008fb24b0b70e6eddf7cf0f0c70c8ac21b92.png "Example Sonoff-Tasmota MQTT settings")
In the example configuration you can see a non-default **Full Topic** definition, which is **not** used in the following examples (but which can be recommended). In the example configuration you can see a non-default **Full Topic** definition, which is **not** used in the following examples (but which can be recommended).
### Integration
Simply **set up items** for all Sonoff-Tasmota [MQTT topics](https://github.com/arendst/Sonoff-Tasmota/wiki/MQTT-Features) you are interested in. Examples for most needed topics are given below. Some Sonoff-Tasmota topics are JSON encoded, the `JSONPATH` transformation can be used to extract this data. Simply **set up items** for all Sonoff-Tasmota [MQTT topics](https://github.com/arendst/Sonoff-Tasmota/wiki/MQTT-Features) you are interested in. Examples for most needed topics are given below. Some Sonoff-Tasmota topics are JSON encoded, the `JSONPATH` transformation can be used to extract this data.
Additional or further interesting topics are easily identified by reading up on the Sonoff-Tasmota wiki and by subscribing to the modules topics. Subscribe to all topics of one module with the [MQTT wildcard](http://www.hivemq.com/blog/mqtt-essentials-part-5-mqtt-topics-best-practices) topic string `+/sonoff-XYZ/#` (String depends on your user-configured Topic/FullTopic). Configure items for the identified topics similar to the ones below. Additional or further interesting topics are easily identified by reading up on the Sonoff-Tasmota wiki and by subscribing to the modules topics. Subscribe to all topics of one module with the [MQTT wildcard](http://www.hivemq.com/blog/mqtt-essentials-part-5-mqtt-topics-best-practices) topic string `+/sonoff-XYZ/#` (String depends on your user-configured Topic/FullTopic). Configure items for the identified topics similar to the ones below.
@ -223,66 +282,6 @@ String Sonoff_Current_FW_Available "Current Release [%s]" <sonoff_basic> (Sonoff
With the item in your sitemap, you will now see the latest release/tag from the tasmota repository. With the item in your sitemap, you will now see the latest release/tag from the tasmota repository.
## Examples for integration with MQTTv2
**.things File:**
```java
Bridge mqtt:broker:myMQTTBroker [ host="IPofBroker", secure=false, username="myUser", password="myPassword" , clientID="myMQTTClient" ]
{
Thing topic sonoff_TH_Thing "Light_TH" {
Channels:
Type switch : PowerSwitch [ stateTopic="stat/sonoff_TH/POWER" , transformationPattern="JSONPATH:$.POWER" , commandTopic="cmnd/sonoff_TH/POWER", on="ON", off="OFF" ]
Type string : Version [stateTopic="stat/sonoff_TH/STATUS2", transformationPattern="JSONPATH:$.StatusFWR.Version"]
Type string : Temperature [stateTopic="tele/sonoff_TH/SENSOR", transformationPattern="JSONPATH:$.AM2301.Temperature"]
}
}
```
**.items File:**
```java
Switch Switch_TH "Switch_TH" { channel="mqtt:topic:myMQTTBroker:sonoff_TH_Thing:PowerSwitch" }
String Switch_TH_Temperatur "Temperatur [%s °C]" <temperature> {channel="mqtt:topic:myMQTTBroker:sonoff_TH_Thing:Temperature"}
String Sonoff_Version "Sonoff Version: [%s]" <sonoff_basic> { channel="mqtt:topic:myMQTTBroker:sonoff_6_Thing:Version"}
```
**.rules File for the Maintenance Action:**
```java
// Work with a list of selected Sonoff modules
val sonoff_device_ids = newArrayList(
"sonoff-A00EEA",
//… add all your modules here!
"sonoff-E8A6E4"
)
// OR
// Work with the grouptopic, addressing ALL modules at once
//val sonoff_device_ids = newArrayList("sonoffs")
rule "Sonoff Maintenance"
when
Item Sonoff_Action received command
then
logInfo("sonoff.rules", "Sonoff Maintenance on all devices: " + receivedCommand)
val actionsBroker = getActions("mqtt","mqtt:broker:MyMQTTBroker") // change to your broker name!
for (String device_id : sonoff_device_ids) {
switch (receivedCommand) {
case "restart" :
actionsBroker.publishMQTT( "cmnd/" + device_id + "/restart", "1")
case "queryFW" :
actionsBroker.publishMQTT( "cmnd/" + device_id + "/status", "2")
case "upgrade" : {
actionsBroker.publishMQTT( "cmnd/" + device_id + "/otaurl", "http://sonoff.maddox.co.uk/tasmota/sonoff-DE.bin") // Replace DE with your country code or use sonoff.bin or other tasmota specialized firmware
actionsBroker.publishMQTT( "cmnd/" + device_id + "/upgrade", "1")
}
}
}
Sonoff_Action.postUpdate(NULL)
end
```
## Community Forum ## Community Forum
For more openHAB related details and questions, please visit the [openHAB community forum thread on Sonoff and Sonoff-Tasmota](https://community.openhab.org/t/itead-sonoff-switches-and-sockets-cheap-esp8266-wifi-mqtt-hardware/15024/1). For more openHAB related details and questions, please visit the [openHAB community forum thread on Sonoff and Sonoff-Tasmota](https://community.openhab.org/t/itead-sonoff-switches-and-sockets-cheap-esp8266-wifi-mqtt-hardware/15024/1).