The MUSH-MACHINE: Rule triggering on mqtt values, storing and displaying it

  • Platform information: Raspi

Hi guys,
I´m helping a friend out who runs a (food and completely legal!!!)-mushroom plant growing it on used coffee gounds. While the mushrooms grow, they produce CO2 and heat, that has to be removed out of the cellar. There he needs accurate temperature, moist and Co2 Info, and has to react on that data. Instead of a timeclock !! steering a ventilator a built an nodemcu based (easyESP) multisensor that measures temperature, moist and Co2 and publishes it every via mqtt to my Raspi2 running OH2 with mosquitto as mqtt broker… horray… that works.

Now that that my oh2 has the values I would love to store and display the data via rrdj4 ? and react to some values by predefined rules.
For instance if CO2 level rises above 1500 ppm start the vent for 10 minutes.
If temperatures goes above 20 C start air condition for 20minutes.

For steering air cond and vent I use sonoff switches flashed with tasmota. (also works)
I´m looking for examples that I can use or alter for that applications. And of course I would then write a doku story for best practise.
Ideas?

Hello,
There are plenty of code examples in the forum and the tutos.
I will not write code for you. You need to do that yourself.
If it doesn’t work and you can’t figure out why, then by all means ask for help.

I’ll write a short rule to point you in the right direction

Do your items work in OH?
Do you receive updated values through mqtt and can you command the air con sonoffs from openHAB via mqtt?

I’ll assume that the answer is Yes

Then create a *.rules file in the rules folder

var Timer timer

rule "Too much CO2"
when
    Item_CO2_Sensor received update
then
    if (Item_CO2_Sensor.value > 1500) {
        if (timer === null) { 
            Item_Vent.sendCommand(ON)                     //turn on the vent
            timer = createTimer(now.plusMinutes(10), [ |  //start and create the timer
                    Item_Vent.sendCommand(OFF)            //end of timer action
                    timer = null                          //reset the timer
            ] )
        }
    }
end

For storing data:

https://www.openhab.org/addons/persistence/rrd4j/

Merci Vincent,
Yep, the item allready work fine within OH. Thank you for the code! That gives me a solid base to start working. I will try my best and will report the status thereafter! Apparantly I´m measuring CO2 with an MH-Z19 and was impressed by the accurat results.
bye for now!

Hi all, meanwhile the MH-z19 is working fine on an NODEMCU running the lastest easy ESP firmware.
I never had input values running into openhab, so first I´ve set up an mqtt broker (mosquito) on OH2 and an items file and a sitemap to visualize the incoming CO2 values. But somehow I expirience troubles:

sitemap test label=“Test Co2_Messung” {

Frame label=“co2_messung” {
Switch item=test icon=“big_bulb”
}
}

test.items
Number co2_messung “co2 ESP2 [%. PPM]” (CO2_Belastung) {mqtt="<[openhabian:/DeviceESP12E/co2_esp/gas:state:default]"}

esp_device

mqtt.cfg

Quite desperate…
ohbroker is running according the log…

altered the sitemap to:
[01:33:23] openhabian@openHABianPi:/etc/openhab2/sitemaps$ more co2.sitemap
sitemap co2 label=“My home automation” {

Frame label="Demo" {

    Text item=co2 label="Konzentration [%.1f PPM]"

    }

}


and the co2.items to:

[01:36:24] openhabian@openHABianPi:/etc/openhab2/items$ more co2.items
Number co2 “Temperatur [%.0f °C]” (livingRoom) [“CurrentTemperature”] {mqtt="<[ohbroker://co2_esp/gas/PPM/:state:default]" }
[01:36:33] openhabian@openHABianPi:/etc/openhab2/items$


still no value is displayed… hmpf going to have some sleep now… ;-((

Number co2 “Temperatur [%.0f °C]” (livingRoom) [“CurrentTemperature”] {mqtt="<[ohbroker://co2_esp/gas/PPM/:state:default]" }

There is something wrong with the topic. It shouldn’t end with / and start with 2 //

Have you got an MQTT tools like mqttspy or mqtt-fx installed?
What topic are the value published on?

You´re surely right… now first I did my homeworks and installed Oracle Java and mqtt spy to see whats going on…

good news: the Broker ohbroker is running and reacts when i publish to a testswitch item:

2018-07-08 10:43:59.651 [vent.ItemStateChangedEvent] - MQTT_Test changed from NULL to ON

2018-07-08 10:44:05.726 [vent.ItemStateChangedEvent] - MQTT_Test changed from ON to OFF

2018-07-08 10:44:15.313 [vent.ItemStateChangedEvent] - MQTT_Test changed from OFF to ON

Now I have to go the beach, but thereafter I will continue trying to reveive data from the nodeMCU

See the new co2.items and the new co2.sitemaps and also the output:
co2_sitemaps

I suppose where I´m going wrong is

Apparently I want to appologize for contributing and asking really beginners issues into this community. Thank you anyways for politely answering me!

This morning I found that the version of EASY ESP 8.7.2018 had a problem with the generation of the JSON output which now is fixed. I´m now trying with the current version.
[JSON] Fixed broken JSON output on /json URL

Use mqtt spy and subscribe to # (all topics)
You should see what the ESP throws out. Make a note of the topic and use that in your oh binding:

Number co2_messung "co2 ESP2 [%0.f PPM]" {CO2_Belstung) { mqtt="<[openhabian:TOPICFROMESP:state:default]" }

Now, the ESP will throw out a JSON so you will need the JSONPATH transform installed.

Post the payload from the ESP and I’ll show you how to get the proper value out of the json with the JSONPATH transform.

See ya later

See what we got here:


As you said I subscribed to all # and the ESP nicly sends data to the client.
Problem is surely the interpretation of the JSON Path.

Can you also please give me a hint, how I eventually can visualize the data via rrdj4?

thanks and if you send a mail to robert@kotal.at with your adress I would be glad to send you some Mozartkugeln next week when I´m home. This saved me a lot of time.

I suggest: ???
Item
String co2_json “co2 [JSONPATH($.co2_esp/gas/PPM.status.co2):%s PPM]” {…}
Number co2 “co2 [%.1f PPM”

You won’t need the jsonpath
The co2 ppm value is the payload of topic /co2_esp/gas/PPM

Number co2_messung "co2 ESP2 [%0.f PPM]" {CO2_Belstung) { mqtt="<[openhabian:/co2_esp/gas/PPM:state:default]" }

Yep! success… but still no value in the frontend?? and sometimes strange messages in the log:
see:
=> /var/log/openhab2/openhab.log <==
2018-07-08 18:13:09.164 [WARN ] [ui.internal.items.ItemUIRegistryImpl] - Exception while formatting value ‘790.00’ of item co2 with format ‘%0.f PPM’: {}
java.util.UnknownFormatConversionException: Conversion = ‘0’
at java.util.Formatter.checkText(Formatter.java:2579) [?:?]
at java.util.Formatter.parse(Formatter.java:2565) [?:?]
at java.util.Formatter.format(Formatter.java:2501) [?:?]
at java.util.Formatter.format(Formatter.java:2455) [?:?]
at java.lang.String.format(String.java:2940) [?:?]
at org.eclipse.smarthome.core.library.types.DecimalType.format(DecimalType.java:77) [109:org.eclipse.smarthome.core:0.10.0.b1]
at org.eclipse.smarthome.ui.internal.items.ItemUIRegistryImpl.getLabel(ItemUIRegistryImpl.java:372) [155:org.eclipse.smarthome.ui:0.10.0.b1]
at org.eclipse.smarthome.ui.basic.internal.render.AbstractWidgetRenderer.preprocessSnippet(AbstractWidgetRenderer.java:118) [196:org.eclipse.smarthome.ui.basic:0.10.0.b1]
at org.eclipse.smarthome.ui.basic.internal.render.TextRenderer.renderWidget(TextRenderer.java:42) [196:org.eclipse.smarthome.ui.basic:0.10.0.b1]
at org.eclipse.smarthome.ui.basic.internal.render.PageRenderer.renderWidget(PageRenderer.java:172) [196:org.eclipse.smarthome.ui.basic:0.10.0.b1]
at org.eclipse.smarthome.ui.basic.internal.render.PageRenderer.processChildren(PageRenderer.java:137) [196:org.eclipse.smarthome.ui.basic:0.10.0.b1]
at org.eclipse.smarthome.ui.basic.internal.render.PageRenderer.processChildren(PageRenderer.java:158) [196:org.eclipse.smarthome.ui.basic:0.10.0.b1]
at org.eclipse.smarthome.ui.basic.internal.render.PageRenderer.processPage(PageRenderer.java:100) [196:org.eclipse.smarthome.ui.basic:0.10.0.b1]
at org.eclipse.smarthome.ui.basic.internal.servlet.WebAppServlet.service(WebAppServlet.java:159) [196:org.eclipse.smarthome.ui.basic:0.10.0.b1]
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:848) [88:org.eclipse.jetty.servlet:9.3.22.v20171030]
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:584) [88:org.eclipse.jetty.servlet:9.3.22.v20171030]
at org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.doHandle(HttpServiceServletHandler.java:71) [191:org.ops4j.pax.web.pax-web-jetty:6.0.7]
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) [87:org.eclipse.jetty.server:9.3.22.v20171030]
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548) [85:org.eclipse.jetty.security:9.3.22.v20171030]
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226) [87:org.eclipse.jetty.server:9.3.22.v20171030]
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1180) [87:org.eclipse.jetty.server:9.3.22.v20171030]
at org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.doHandle(HttpServiceContext.java:284) [191:org.ops4j.pax.web.pax-web-jetty:6.0.7]
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:512) [88:org.eclipse.jetty.servlet:9.3.22.v20171030]
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) [87:org.eclipse.jetty.server:9.3.22.v20171030]
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1112) [87:org.eclipse.jetty.server:9.3.22.v20171030]
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) [87:org.eclipse.jetty.server:9.3.22.v20171030]
at org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(JettyServerHandlerCollection.java:80) [191:org.ops4j.pax.web.pax-web-jetty:6.0.7]
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134) [87:org.eclipse.jetty.server:9.3.22.v20171030]
at org.eclipse.jetty.server.Server.handle(Server.java:534) [87:org.eclipse.jetty.server:9.3.22.v20171030]
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:333) [87:org.eclipse.jetty.server:9.3.22.v20171030]
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251) [87:org.eclipse.jetty.server:9.3.22.v20171030]
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:283) [79:org.eclipse.jetty.io:9.3.22.v20171030]
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:108) [79:org.eclipse.jetty.io:9.3.22.v20171030]
at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:93) [79:org.eclipse.jetty.io:9.3.22.v20171030]
at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.executeProduceConsume(ExecuteProduceConsume.java:303) [90:org.eclipse.jetty.util:9.3.22.v20171030]
at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:148) [90:org.eclipse.jetty.util:9.3.22.v20171030]
at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:136) [90:org.eclipse.jetty.util:9.3.22.v20171030]
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:671) [90:org.eclipse.jetty.util:9.3.22.v20171030]
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:589) [90:org.eclipse.jetty.util:9.3.22.v20171030]
at java.lang.Thread.run(Thread.java:748) [?:?]
==> /var/log/openhab2/events.log <==
2018-07-08 18:13:24.555 [vent.ItemStateChangedEvent] - co2 changed from 790.00 to 789.00
2018-07-08 18:13:26.544 [vent.ItemStateChangedEvent] - co2 changed from 789.00 to 788.00
2018-07-08 18:13:28.544 [vent.ItemStateChangedEvent] - co2 changed from 788.00 to 787.00
2018-07-08 18:13:30.541 [vent.ItemStateChangedEvent] - co2 changed from 787.00 to 785.00
2018-07-08 18:13:32.550 [vent.ItemStateChangedEvent] - co2 changed from 785.00 to 783.00
2018-07-08 18:13:34.553 [vent.ItemStateChangedEvent] - co2 changed from 783.00 to 781.00
2018-07-08 18:13:36.543 [vent.ItemStateChangedEvent] - co2 changed from 781.00 to 779.00
2018-07-08 18:13:38.543 [vent.ItemStateChangedEvent] - co2 changed from 779.00 to 778.00
2018-07-08 18:13:40.543 [vent.ItemStateChangedEvent] - co2 changed from 778.00 to 777.00