|
Post by bone450 on Feb 1, 2017 1:29:24 GMT
I have a sonoff device setup with v1.2 of Computourist esp firmware. Works great, thanks for the work on that. The rest of my system is using the mosquitto broker and have openhab2 installed. It appears to function as it should to turn the relay on and off with openhab. I have also verified the devices on the sonoff trigger the proper mqtt messages with mqttfx.
What I am trying to do is have openhab display if the device is connected / online or not. My initial thought was to use the "home/esp_gw/disconnected" topic to drive the change of status in openhab. This topic properly triggers in mqttfx when the sonoff goes offline, not sure how but it fires the message out. I am new to openhab so I started with the following:
items file:
Number Node11Rssi "Node11 Signal [%.3f db]" <network> {mqtt="<[mosquitto:home/esp_gw/nb/node11/dev02:state:default]"} Switch Act_Node11 {mqtt="<[mosquitto:home/esp_gw/nb/node11/dev16:state:default::]", mqtt=">[mosquitto:home/esp_gw/sb/node11/dev16:command:*:default]" } String Node11Disconnect "Status : [%s is Disconnected]" {mqtt="<[mosquitto:home/esp_gw/disconnected:command:default]"} rules file:
rule "Node 11 Disconnect" when Item Node11Rssi received update then postUpdate(Node11Disconnect, 1) end sitemap file:
Frame label="Node11" { Text item=Node11Disconnect }
Currently this doesn't clearly show if the node is online, and also what specific node is online, once the signal strength gets sent. When node 11 disconnects it shows "ESP_11 is Disconnected". When the node reconnects and sends the signal strength it shows "1 is Disconnected". Is their an easier way to clearly show if a node is online or not? If not could I clarify the messages by adding a mapping to a tranform file?
On a side note I am not sure what the difference is between OH1.8 and OH2.
|
|
|
Post by papa on Feb 2, 2017 21:09:20 GMT
Greetings, bone450, & welcome to the DIY Home Automation party (& sometimes source of frustration) I use OpenHAB 1.8.x & do not use OpenHAB 2 yet so I cannot comment much on that. I believe 1.8.x config files tend to work in OpenHAB 2. I don't use Sonoffs or other ESP8266 devices so I can only respond based on what I read & observe in your OpenHAB config files & what I can find online. I hope others with ESP8266 experience (like jimkernsjr or computourist) will weigh in on this.github.com/computourist/ESP8266-MQTT-client/tree/master/ESP_SONOFF_12 ^^ Is it correct that you downloaded & used this programming? String Node11Disconnect "Status : [%s is Disconnected]" {mqtt="<[mosquitto:home/esp_gw/disconnected:command:default]"} ^^ This item's mqtt binding looks "off" to me. However you report that with the item as it is, when node 11 disconnects it shows "ESP_11 is Disconnected". So maybe that item's mqtt binding is correct for ESP. Based on how you say that much works, I'll make a suggestion below that I cannot test (not having a Sonoff). This seems to be what is happening in your existing OpenHAB configs: Your rule is doing what you programmed (but did not intend), when the node reconnects and sends the signal strength it shows "1 is Disconnected." As your rule stipulates when Node11RSSI receives update, postUpdate fills the String Node11Disconnect with the value of 1 (replacing ESP_11) which replaces %s in the formatting guide [%s is Disconnected] My suggestion: I believe you could accomplish what you want with something like the following: Two items, the first one deletes some of Node11Disconnect String Node11Disconnect {mqtt="<[mosquitto:home/esp_gw/disconnected:command:default]"} String Node11Status "Node 11 Status [%s]" Delete or disable the rule you have & try this rule "Node 11 Status" when Time cron "0 0/1 * * * ?" then var String LINK11 = Node11Disconnect.state var String 11_Stat = "Connected" if LINK11 == "ESP_11" 11_Stat = "Disconnected" postUpdate(Node11Status,11_Stat) end sitemap file: (the new 2nd item is displayed) Frame label="Node11" { Text item=Node11Status } What the rule is supposed to do: Once a minute (Time cron ...), check the state of Node11Disconnect. Then 11_Stat starts with a default value of "Connected" but if Node11Disconnect reports "ESP_11" then 11_Stat changes to the value "Disconnected" Finally, the rule given value of 11_Stat is postUpdated into Node11Status & is displayed on the sitemap. ===================== P.S. You might look at this community.openhab.org post It has an item with an "available" function & includes a NODEID esp_111
|
|
|
Post by bone450 on Feb 4, 2017 17:21:37 GMT
Papa, thanks for the idea. I tried what you suggested but couldn't get it to work. I understand the thought behind it, and I would prefer to use your method to display the status as a different line item on the sitemap. I think it may be some syntax errors or something like that. I am a complete novice when it comes to coding / scripting and I don't really understand the different type of variables like numbers, strings, text , if / then statement formatting etc and I think that may be where the issue is. Or maybe I need to have some imports at the beginning of the rules file, currently I have none.
What I have which is working is the following:
items file:
Number Node11Rssi "Node11 Signal (0=Offline) [%.1f db]" <network> {mqtt="<[mosquitto:home/esp_gw/nb/node11/dev02:state:default]"} Switch Act_Node11 {mqtt="<[mosquitto:home/esp_gw/nb/node11/dev16:state:default::]", mqtt=">[mosquitto:home/esp_gw/sb/node11/dev16:command:*:default]" } String NodeDisconnectESP "[%s]" <network-off> {mqtt="<[mosquitto:home/esp_gw/disconnected:state:default]"}
rules file:
rule "Node 11 Disconnect" when Item NodeDisconnectESP received update ESP_11 then postUpdate(Node11Rssi, 0) end
sitemap file:
Frame label="Node11" { Switch item=Act_Node11 label="Node 11" Text item=Node11Rssi } How this works is: 1. The sonoff node (11) broadcasts the signal strength every 20secs to Node11Rssi via home/esp_gw/nb/node11/dev02. 2. This signal strength is displayed in the sitemap as the text item Node11Rssi. Example full display is "Node11 Signal (0=Offline) -77.0 db". 3. If the node goes offline it (or MQTT) sends a last will statement via the following "home/esp_gw/disconnected/ESP_11". 4. NodeDisconnectESP gets that message and the rule triggers and sets Node11Rssi to 0. It will stay 0 until the node reconnects and sends and updated signal strength, which happens every 20secs when connected. Example full display when it goes offline is "Node11 Signal (0=Offline) 0.0 db"
This is simple, but I am going to keep searching for a better solution similar to what you suggested.
|
|
|
Post by papa on Feb 4, 2017 19:56:43 GMT
bone450, congratulations on experimenting & getting something to work. That's how I learned: imitating & adapting other people's stuff, researching & just trying things until something worked. Have patience with yourself & keep trying. I still have big gaps of knowledge, still make mistakes, & still have much to learn.
BTW, my compliments on how well both of your post described what you have & what you need. Also helpful is the way you described what you got working. You've added to the ESP8266 information on this forum.
I'm not sure what I suggested before is correct. I just had no way to test it.
Here's the imports I put at the top of my rules file. Maybe one of them will help. import org.openhab.core.library.types.* import org.openhab.core.persistence.* import org.openhab.model.script.actions.* import org.openhab.model.transformation.actions.* import java.lang.Math import java.util.Calendar import java.util.Date import java.util.TimeZone import java.text.SimpleDateFormat import java.util.Random ============================================= You might try this modification of what you have working: (my suggested changes in blue text)
items file:
String Node11Rssi "Node11 Signal (db) [%s]" <network> {mqtt="<[mosquitto:home/esp_gw/nb/node11/dev02:state:default]"} Switch Act_Node11 {mqtt="<[mosquitto:home/esp_gw/nb/node11/dev16:state:default::]", mqtt=">[mosquitto:home/esp_gw/sb/node11/dev16:command:*:default]" } String NodeDisconnectESP "[%s]" <network-off> {mqtt="<[mosquitto:home/esp_gw/disconnected:state:default]"}
rules file:
rule "Node 11 Disconnect" when Item NodeDisconnectESP received update ESP_11 then postUpdate(Node11Rssi,"Offline") end
No change in the sitemap file
Notice I changed the Node11RSSI item to a String which I confirmed will work. I did this so the rule could postUpdate the item to the string Offline.
The way I think this will work: If Node11RSSI gets updated to -27 then it will be displayed Node11 Signal (db) -27 If NodeDisconnectESP received update ESP_11 then Node11RSSI will be displayed Node11 Signal (db) Offline
Let us know what works & not & help us to add more forum information on using ESP8266
|
|
|
Post by camblonie on Feb 10, 2017 17:27:06 GMT
Have you guys looked at the Last Will and Testament function of MQTT? I can't seem to figure out how to get it to setup in arduino though.
|
|
|
Post by papa on Feb 10, 2017 17:57:38 GMT
Have you guys looked at the Last Will and Testament function of MQTT? I can't seem to figure out how to get it to setup in arduino though. I have not. Who could shed some light on Last Will and Testament function of MQTT?
|
|
|
Post by computourist on Feb 10, 2017 20:03:25 GMT
Hi All,
Last Will and Testament (LWT) is a message that is stored in the broker. It is set on connection setup by the client. For an example look at line 244 of ESP_Sonoff_12.ino. The broker will send this message to all subscribers on disconnection of the client.
To detect whether a client is online I can imagine using one (or both) of two methods: - Mosquitto knows the status of all connections. I know one can query Mosquitto via MQTT on status, but I'm not sure on the level of detail. The total number of active connections can be queried, but I would have to dive in to see if data on individual connections is available. If a connection goes down the broker will send the LWT message; Openhab could react to that. - use the regular transmission feature of the client. Set the node software to send status messages on regular intervals (f.e. every 30 seconds). In Openhab a timestamp is updated on every status reception. If the timelapse since the last status update is larger than the refresh interval the client is not connected.
Hope this helps...
Regards, Computourist
|
|
|
Post by camblonie on Feb 11, 2017 4:36:14 GMT
Hah, I missed the obvious. The disconnect message is posted because Computourist is way ahead of us and setup the Last Will and Testament (LWT). Papa your last post works brilliantly though you can remove the switch item as it is unrelated. This is a great feature.
|
|
|
Post by papa on Feb 11, 2017 5:10:03 GMT
camblonie, thanks for letting me know that my proposal (the Feb 4, 2017 post that starts "bone450, congratulations on experimenting") worked. Not having an ESP8266 I am unable to test it. You're correct, I also realized the switch item is not related to the issue of this thread. I assumed bone450 copied all 3 items from his .items file & I just repeated his list. Seems like in this thread, the Aug 1, 2016 post, computourist outlined another approach that could work on all nodes, ESP8266 or not
|
|
|
Post by bone450 on Feb 11, 2017 16:08:30 GMT
Thanks Papa. Changing the item to a string allowed the "Offline" text to be displayed in the sitemap. To close the loop here are the final settings I used to display both signal strength and then offline on the same line in the sitemap. This is using v1.2 of Computourist sonoff firmware with the device id set to 11.
Yes the switch item isn't required in the items file or the sitemap file. I included it incase someone was trying to get a sonoff running.
items file:
// Gets the signal strength from the node
String Node11Rssi "Node11 Signal (db) [%s]" <network> {mqtt="<[mosquitto:home/esp_gw/nb/node11/dev02:state:default]"}
// Gets, sets, updates the toggle switch status from the node
Switch Act_Node11 {mqtt="<[mosquitto:home/esp_gw/nb/node11/dev16:state:default::]", mqtt=">[mosquitto:home/esp_gw/sb/node11/dev16:command:*:default]" }
/////////////
// ESP Node Disconnected , Item to collect when any ESP node disconnects /////////////
String NodeDisconnectESP "[%s]" <network-off> {mqtt="<[mosquitto:home/esp_gw/disconnected:state:default]"}
rules file:
///////////
//Rule to change the signal to Offline if node disconnects
///////////
rule "Node 11 Disconnect"
when
Item NodeDisconnectESP received update ESP_11
then
postUpdate(Node11Rssi,"Offline")
end
sitemap file:
Frame label="Node11" {
Switch item=Act_Node11 label="Node 11 Switch"
Text item=Node11Rssi
}
|
|
|
Post by papa on Feb 11, 2017 16:50:53 GMT
You're welcome, bone450. I'm glad the proposal worked & thanks for reporting back. Feedback helps volunteers like me keep going.
Questions like yours & the creative responses they generate help this forum be useful & informative.
|
|
ragoth
Junior Member
Posts: 64
|
Post by ragoth on Feb 12, 2017 12:07:23 GMT
Dear bone450, I tried your ESP Node Connection Status setup and it works fine. Thanks
|
|
|
Post by papa on Feb 12, 2017 13:23:53 GMT
& thank you, ragoth, for the feedback. Both good to know: what works & what does not.
|
|
|
Post by papa on Feb 17, 2017 20:37:13 GMT
bone450, ragoth, & all I found that a newish Expire Binding provides an an easy way for OpenHAB to report offline ANY node (ESP8266 or NOT). This way needs no rules. The Expire Binding can also serve other purposes. See this thread: Expire Binding: Have OpenHAB Report Offline Nodes
|
|