{"id":649,"date":"2022-10-28T13:29:35","date_gmt":"2022-10-28T12:29:35","guid":{"rendered":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/?p=649"},"modified":"2024-01-14T18:14:36","modified_gmt":"2024-01-14T17:14:36","slug":"using-mqtt-in-open-energy-monitor-to-capture-external-device-data","status":"publish","type":"post","link":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/using-mqtt-in-open-energy-monitor-to-capture-external-device-data\/","title":{"rendered":"Using MQTT in Open Energy Monitor to Capture External Device Data"},"content":{"rendered":"<p><em>I struggled to find clear and well-organised information about this using web searches, so here is a condensed &#8220;how to&#8221;. The scenario I have is using a home-brew ESP8266 based device attached to my solar PV inverter which I want to relay definitive power output to my Open Energy Monitor via MQTT. This seems quite simple in principle, and is simple in practice, but seemingly not well documented.<\/em><\/p>\n<p>First thing is to publish the data to an <a href=\"https:\/\/guide.openenergymonitor.org\/technical\/mqtt\/\">MQTT broker on the emonpi<\/a> with a topic which starts &#8220;emon\/{source}\/{key}&#8221;, replacing {source} with a suitable name for the data source and {key} with the attribute name for the data being sent. In my case, I used &#8220;emon\/solis\/power&#8221; as the data source is the output power for my Solis PV inverter. The message payload is simply the data value. This immediately makes the published data appear on the &#8220;Inputs&#8221; screen of EmonCMS.<\/p>\n<p>Two refinements\/possibilities:<\/p>\n<p><strong>Send JSON<\/strong><\/p>\n<p>Rather than just a single value, send several in the same message. There are two ways to do this:<\/p>\n<p>a) use a topic of form &#8220;emon\/{source}&#8221;<\/p>\n<p>b) use a topic of form &#8220;emon\/{source}\/{key}&#8221;<\/p>\n<p>If the message payload sent to the MQTT topic &#8220;emon\/solis\/power&#8221; looks like {&#8220;ac&#8221;: 90, &#8220;dc&#8221;: 120}, option (a) creates EmonCMS inputs &#8220;ac&#8221; and &#8220;dc&#8221; under &#8220;solis&#8221;, while option (b) creates inputs called &#8220;power_ac&#8221; and &#8220;power_dc&#8221;.<\/p>\n<p><strong>Include a timestamp<\/strong><\/p>\n<p>To do this, simply include an extra field in the JSON called &#8220;time&#8221;, with a value which is the Unix time. If you are testing, the Unix time needs to be fairly close to the actual time (ignoring summer time) otherwise EmonCMS will indicate &#8220;inactive&#8221;, but it still captures the data.<\/p>\n<hr \/>\n<p>Aside: I used the VSMqtt plugin for VSCode as the MQTT client, as I&#8217;m using the PlatformIO plugin to develop my ESP8266 code (using the Arduino libraries). Update: these days I&#8217;ve changed to using <a href=\"https:\/\/mqtt-explorer.com\">MQTT Explorer<\/a> as my go-to software for viewing MQTT messages.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I struggled to find clear and well-organised information about this using web searches, so here is a condensed &#8220;how to&#8221;&#8230;.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,34,16,35,36],"tags":[],"class_list":["post-649","post","type-post","status-publish","format-standard","hentry","category-esp8266","category-iot","category-mcu","category-mqtt","category-open-energy-monitor","post-archive"],"_links":{"self":[{"href":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/wp-json\/wp\/v2\/posts\/649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/wp-json\/wp\/v2\/comments?post=649"}],"version-history":[{"count":4,"href":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/wp-json\/wp\/v2\/posts\/649\/revisions"}],"predecessor-version":[{"id":859,"href":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/wp-json\/wp\/v2\/posts\/649\/revisions\/859"}],"wp:attachment":[{"href":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/wp-json\/wp\/v2\/media?parent=649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/wp-json\/wp\/v2\/categories?post=649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/wp-json\/wp\/v2\/tags?post=649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}