[NODE-RED] process Tasmota sensor data and transmit single metrics to MQTT

Note to self (must be the third time I re-write this, so I better save it somewhere google will help me find again the next time I need it)

I have recently added to my home monitoring/automation system a bunch of smart plugs from LocalBytes who have the following features:

PXL_20220928_104152020_360x

  • ESP8266 based
  • Tasmota firmware installed by default, no need to open/hack them to reflash them
  • no data is sent to any cloud server in China or anywhere else
  • they are reasonably priced
  • they can be remotely controlled, and provide power consumption data

The plugs can be remotely controlled via MQTT publish/subscribe, and they also provide telemetry data through MQTT, in the form of a json encoded Object:

07:18:52.404 MQT: sonoff/tele/starlink/SENSOR = {"Time":"2022-11-04T07:18:52","ENERGY":{"TotalStartTime":"2022-11-03T15:32:39","Total":1.038,"Yesterday":0.594,"Today":0.444,"Period":0,"Power":39,"ApparentPower":44,"ReactivePower":22,"Factor":0.87,"Voltage":227,"Current":0.196}}

My home monitoring system, based on (https://emoncms.org/) is able to retrieve sensor data from a MQTT host, but it expects the data to be a in
single metric per topic e.g.:

In order to transform the single topic-json encoded object into multiple topics I am using a Node-RED flow:

The flow:

  • subscribes to an MQTT tree (in my case: sonoff/tele/# )
    Screenshot 2022-11-04 at 09.13.27

  • uses a Switch node to filter only SENSOR messages
    Screenshot 2022-11-04 at 09.14.48

  • uses a function node to retrieve the sonoff plug name from the topic, and splits the Json object into multiple key/value pairs, and sets them to the appropriate topic
    Screenshot 2022-11-04 at 09.17.54

var mytopic = msg.topic.replace(/sonoff\/tele\//g,"").replace(/\/SENSOR/g,"");

for (const [key, value] of Object.entries(msg.payload.ENERGY)) {
    const el = {
        "topic": "emon/" + mytopic + "/" + key,
        "payload": value
        }
    node.send(el)    
}
return null;
  • publishes all single metrics to the target topic
    Screenshot 2022-11-04 at 09.21.29

The end result is that a single MQTT message of the form:

{
	"topic": "sonoff/tele/brocade-sottotetto/SENSOR",
	"payload": {
		"Time": "2022-11-04T09:17:27",
		"ENERGY": {
			"TotalStartTime": "2022-11-03T15:23:32",
			"Total": 1.232,
			"Yesterday": 0.572,
			"Today": 0.66,
			"Period": 0,
			"Power": 71,
			"ApparentPower": 91,
			"ReactivePower": 57,
			"Factor": 0.78,
			"Voltage": 226,
			"Current": 0.402
		}
	},
	"qos": 0,
	"retain": false,
	"_msgid": "1eca7e840d7e5188"
}

is split into multiple messages:

{
	"topic": "emon/brocade-sottotetto/TotalStartTime",
	"payload": "2022-11-03T15:23:32",
	"_msgid": "1eca7e840d7e5188"
}
{
	"topic": "emon/brocade-sottotetto/Yesterday",
	"payload": 0.572,
	"_msgid": "1eca7e840d7e5188"
}
{
	"topic": "emon/brocade-sottotetto/Today",
	"payload": 0.66,
	"_msgid": "1eca7e840d7e5188"
}
{
	"topic": "emon/brocade-sottotetto/Power",
	"payload": 71,
	"_msgid": "1eca7e840d7e5188"
}

and is picked up by my energy monitoring system where I can then store the values in appropriate time series …

3 Likes

I have not delved into using NodeRed. I don’t have complex rules and I don’t have a lot of rules but I find that it was just easy to do the YAML by hand.

You mean you wrote a custom piece of code?

No, I use HomeAssistant and between setting up rules in HA and Tasmota and a little bit of YAML to capture parts of messages, you get the same effect of NodeRed.

From of the UI shots, it looks like Home Assistant. I probably should have clarified that.

Ah, no, the parsing was needed for Emoncms (https://emoncms.org/) that expects sensor data in a specific format in a specific subtopic, so I had no direct way of getting the data inside the json
Openhab can do the same (parse a json field inside an mqtt message) :slight_smile:

Nice. I probably should have followed the links. Thanks for sharing this. I am sure this will be useful as I see a lot of people adding “smart” devices to their homes and taking their stuff off of the cloud.

1 Like

In the few years I have been doing this (tinkering with sensors, power management, solar and now batteries) the only thing I did not change was the requirement to only buy stuff that could work without a cloud service.
All my gear does that, the solar (Solaredge)/Battery(ZCS) inverters do need cloud connectivity, but that is the price I have to pay to have 20 year warranty on the inverter and 10 years on batteries and inverter …

I have various sonoff smart plugs reflashed with tasmota, a couple of meross smart plugs that I control via mqtt/node-red/my stream deck of all things, some esp32 nodes based on Souliss , all talking to mqtt/Openhab/Emoncms, Viessmann gas furnace included …

Im in exactly the same position. Battery relay controlled by solar with a tasmota flashed switch, mqtt and node red