Slightly more advanced configurations
This post assumes that the Things, Items and Sitemap files are already configured as in the previous post.
Topics
- Device online/offline status
- The STATE message
- Device Wi-Fi signal quality
- Status on startup
Device online/offline status
In order to know whether our Tasmota device is actually reachable we can interrogate the Last Will and Testament (LWT) topic.
Click here for a simplified description of how this works
- When Tasmota connects to the MQTT broker, it sends a message
Online
to the device LWT topic. - At the same time, Tasmota also tells the MQTT broker what message to use if it goes offline, namely
Offline
. - The MQTT broker is now in charge, and when it no longer has a connection to the Tasmota device it will broadcast the
Offline
message on the device LWT topic.
a. Things
i. mqttdevices.things
Add a new switch
type Channel called reachable
to the existing thing, as below:
Type switch : reachable "Reachable" [
stateTopic = "tele/swBedroomSideLight/LWT",
on="Online",
off="Offline"
]
Click here to see the resultant things file.
Thing mqtt:topic:swBedroomSideLight "Switch Bedroom Side Light" (mqtt:broker:MosquittoMqttBroker) {
Channels:
Type switch : power "Power Switch" [
commandTopic="cmnd/swBedroomSideLight/POWER",
stateTopic="stat/swBedroomSideLight/POWER",
on="ON",
off="OFF"
]
Type switch : reachable "Reachable" [
stateTopic = "tele/swBedroomSideLight/LWT",
on="Online",
off="Offline"
]
}
b. Item
Inside mqtt.items
, add the following line:
Switch sBedroomSideLightReachable "Bedroom Side Light" { channel="mqtt:topic:swBedroomSideLight:reachable" }
c. Sitemap
We have some options when presenting this online/offline information.
Switch
i. Switch
A simple display method is to use a Switch element, which will show ON
when the device is online, or OFF
when it is offline.
Add the following into myHome.sitemap
, underneath the original Switch Item:
Switch item=sBedroomSideLightReachable label="Bedroom side reachable [%s]"
Note that you are able to toggle the switch but nothing will happen, so this perhaps is not the most ideal method of displaying whether the Tasmota device is reachable.
Text
ii. Text
We can use a Text element to display a read-only status.
Add the following into myHome.sitemap
:
Text item=sBedroomSideLightReachable label="Bedroom side reachable [%s]"
But showing ON
or OFF
isn’t quite right.
Text with mapping
iii. Text with mapping
We can use a Text element to display a read-only status, and display either ONLINE
or OFFLINE
instead of ON
or OFF
. This is achieved using a MAP transformation, which requires the installation of another add-on, and a new file!
Install MAP transformation service
- Navigate to PaperUI, usually at http://<openhab-ip-address>:8080/paperui/
- Go to Add-ons -> Transformations -> Map Transformation and click Install
Create MAP file
Create a new file called switch2online.map
and save within the transform folder in the openHAB Site configuration folder.
Inside switch2online.map
copy and paste the following, and save.
OFF=OFFLINE
ON=ONLINE
NULL=UNDEFINED
-=-
Create Sitemap element
Add the following into myHome.sitemap
:
Text item=sBedroomSideLightReachable label="Bedroom side reachable [MAP(switch2online.map):%s]"
The label now includes a link to the MAP transformation service, with our recently created file. We send the word ON
or OFF
(via the %s
) to the MAP transformation service, and it uses the data in the file to return either ONLINE
or OFFLINE
.
For more information on transformations (MAP isn’t the only one!), see the documentation.
Text with mapping and colours
iv. Text with mapping and colours
Depending on the status, we can colour the ONLINE
and OFFLINE
differently.
Ensure the MAP transformation service is installed - see above.
Add the following into myHome.sitemap
:
Text item=sBedroomSideLightReachable label="Bedroom side reachable [MAP(switch2online.map):%s]" valuecolor=["ON"="green", "OFF"="red", "NULL"="red", "-"="red"]
Note that the colours are based on the original value of the Item (i.e. ON
and OFF
, not the value after it has been transformed.
For more information on dynamic colouring of labels, see the documentation.
The STATE message
In addition to the RESULT
topic, to which Tasmota publishes a message every time it performs a command, a default configuration of Tasmota will send out a message on the STATE
topic every 5 minutes (300 seconds). Simply, this message contains information about the current state of the device.
For our device, Tasmota will publish a message to the following STATE
topic:
tele/swBedroomSideLight/STATE
and a normal message published to this topic looks as follows:
{"Time":"2020-09-06T19:14:24","Uptime":"12T23:00:11","UptimeSec":1119611,"Heap":26,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"MqttCount":1,"POWER":"OFF","Wifi":{"AP":1,"SSId":"ACPRO1","BSSId":"E0:63:DA:65:EA:4A","Channel":6,"RSSI":84,"Signal":-58,"LinkCount":1,"Downtime":"0T00:00:07"}}
This mesage is in a JSON format.
Click to see this message with slightly more friendly formatting
{
"Time":"2020-09-06T19:14:24",
"Uptime":"12T23:00:11",
"UptimeSec":1119611,
"Heap":26,
"SleepMode":"Dynamic",
"Sleep":50,
"LoadAvg":19,
"MqttCount":1,
"POWER":"OFF",
"Wifi":{
"AP":1,
"SSId":"ACPRO1",
"BSSId":"E0:63:DA:65:EA:4A",
"Channel":6,
"RSSI":84,
"Signal":-58,
"LinkCount":1,
"Downtime":"0T00:00:07"
}
}
This message contains a number of details, including:
- How long the device has been on for (
Uptime
andUptimeSec
) - The current state of the relay (
POWER
) - Wi-Fi signal quality/strength (
RSSI
andSignal
)
Depending on the device, Tasmota might provide more details in the STATE
message. For example, the message below comes from a MagicHome RGB LED controller, flashed with Tasmota:
{"Time":"2020-09-06T19:20:21","Uptime":"62T08:16:02","UptimeSec":5386562,"Heap":26,"SleepMode":"Dynamic","Sleep":10,"LoadAvg":99,"MqttCount":5,"POWER":"ON","Dimmer":81,"Color":"06CF00","HSBColor":"118,100,81","Channel":[2,81,0],"Scheme":0,"Fade":"ON","Speed":5,"LedTable":"ON","Wifi":{"AP":1,"SSId":"ACPRO1","BSSId":"E0:63:DA:65:EA:4A","Channel":6,"RSSI":66,"Signal":-67,"LinkCount":2,"Downtime":"0T00:00:44"}}
Click to see this message with slightly more friendly formatting
{
"Time":"2020-09-06T19:20:21",
"Uptime":"62T08:16:02",
"UptimeSec":5386562,
"Heap":26,
"SleepMode":"Dynamic",
"Sleep":10,
"LoadAvg":99,
"MqttCount":5,
"POWER":"ON",
"Dimmer":81,
"Color":"06CF00",
"HSBColor":"118,100,81",
"Channel":[
2,
81,
0
],
"Scheme":0,
"Fade":"ON",
"Speed":5,
"LedTable":"ON",
"Wifi":{
"AP":1,
"SSId":"ACPRO1",
"BSSId":"E0:63:DA:65:EA:4A",
"Channel":6,
"RSSI":66,
"Signal":-67,
"LinkCount":2,
"Downtime":"0T00:00:44"
}
}
In addition to the previously highlighted details, this STATE message also includes:
- Current dimmer level (
Dimmer
) - Current colour (
Color
andHSBColor
) - Fade status, and speed during fade (
Fade
andSpeed
respectively)
The following couple of examples use this STATE message.
Device Wi-Fi signal quality
Within the STATE message (see above) there is a Wifi
group, within which is the RSSI
key/value pair. RSSI in this case is a measure of the quality of the signal: a signal strength lower than -100dBm is 0%, anything greater than -50dBm is 100%, and anything in-between is, well, inbetween %!
For this example, we are going to have to extract the value of RSSI
from the STATE message. Because the message is in a JSON format, we can use openHAB’s JSONPath Transformation Service.
Install JSONPath transformation service
- Navigate to PaperUI, usually at http://<openhab-ip-address>:8080/paperui/
- Go to Add-ons -> Transformations -> JSONPath Transformation and click Install
a. Things
i. mqttdevices.things
Add a new number
type Channel called rssi
to the existing thing, as below:
Type number:rssi "RSSI" [
stateTopic="tele/swBedroomSideLight/STATE",
transformationPattern="JSONPATH:$.Wifi.RSSI"
]
We use the transformationPattern
configuration to extract the RSSI
value from the Wifi
group.
Click here to see the resultant things file.
Thing mqtt:topic:swBedroomSideLight "Switch Bedroom Side Light" (mqtt:broker:MosquittoMqttBroker) {
Channels:
Type switch : power "Power Switch" [
commandTopic="cmnd/swBedroomSideLight/POWER",
stateTopic="stat/swBedroomSideLight/POWER",
on="ON",
off="OFF"
]
Type switch : reachable "Reachable" [
stateTopic = "tele/swBedroomSideLight/LWT",
on="Online",
off="Offline"
]
Type number:rssi "RSSI" [
stateTopic="tele/swBedroomSideLight/STATE",
transformationPattern="JSONPATH:$.Wifi.RSSI"
]
}
b. Item
Inside mqtt.items
, add the following line:
Number nBedroomSideLightRSSI "RSSI" { channel="mqtt:topic:swBedroomSideLight:rssi" }
c. Sitemap
Add the following into myHome.sitemap
:
Text item=nBedroomSideLightRSSI label="Connection quality [%d %%]" icon="qualityofservice"
Note that the qualityofservice
icon is a dynamic icon (i.e. it can change depending on the value of the openHAB Item), but only for the numbers 0, 1, 2, 3 and 4. It will not work with the percent values provided by Tasmota, so the icon will remain static. A couple of methods of solving this issue are discussed in this post and onwards.
Status on startup
With the configuration in the first post, openHAB knows what state the relay is in because it receives a message on the stat/swBedroomSideLight/POWER
topic. However, Tasmota only sends a message on this topic when the state of the relay is changed, which means that after a restart of openHAB, openHAB doesn’t know what the state of the switch is.
One method of solving this issue is to use Persistence - continually (if configured) saving the state of openHAB Items into a database, which openHAB can then interrogate when it restarts to set all the switches (and other things) to the last-known states.
However, if you don’t want to install extra add-ons, and don’t mind waiting up to 5 minutes for openHAB to learn the state of the relay, you can use the STATE
message - remember that it provides, amongst other things, the current state of the relay with the POWER
key/value pair.
And the magic here is that we can attach more than one Thing Channel to a single Item!
a. Things
i. mqttdevices.things
Add a new switch
type Channel called state_power
to the existing thing, as below:
Type switch:state_power "State Power" [
stateTopic="tele/swBedroomSideLight/STATE",
transformationPattern="JSONPATH:$.POWER"
]
We use the transformationPattern
configuration to extract the POWER
value.
Click here to see the resultant things file.
Thing mqtt:topic:swBedroomSideLight "Switch Bedroom Side Light" (mqtt:broker:MosquittoMqttBroker) {
Channels:
Type switch : power "Power Switch" [
commandTopic="cmnd/swBedroomSideLight/POWER",
stateTopic="stat/swBedroomSideLight/POWER",
on="ON",
off="OFF"
]
Type switch : reachable "Reachable" [
stateTopic = "tele/swBedroomSideLight/LWT",
on="Online",
off="Offline"
]
Type number:rssi "RSSI" [
stateTopic="tele/swBedroomSideLight/STATE",
transformationPattern="JSONPATH:$.Wifi.RSSI"
]
Type switch:state_power "State Power" [
stateTopic="tele/swBedroomSideLight/STATE",
transformationPattern="JSONPATH:$.POWER"
]
}
b. Item
Inside mqtt.items
modify the existing sBedroomSideLight
Item to include the new channel:
Switch sBedroomSideLight "Bedroom Side Light" { channel="mqtt:topic:swBedroomSideLight:power", channel="mqtt:topic:swBedroomSideLight:state_power" }
And that’s it. Everytime the STATE
message is received, openHAB can update the sBedroomSideLight
Item to the reported value.