Split Data from Tasmota and write them to Influx

Hello together,
I am a newbie in Node Red, MQTT and Influx DB.
At the moment I am receiving different data from different Tasmota devices and write them into an Influx DB.
This works.
But:
In my opinion it must be possible to catch the data via wildcard (topic: tele/+/SENSOR), convert it via function and write the data to the database with only one workflow.
I want to use the function node. Has anybody an idea for that?

In a function node, split the topic & pick out the parts you want/need...

e.g...

const topicParts = msg.topic.split('/')
newPayload = {
  base: topicParts[0],
  name: topicParts[1],
  type: topicParts[2],
  payload: msg.payload
}
msg.payload = newPayload
return msg

Untested - off the top of my head

Clever or meaningful content of the function can be advised only if you describe the contents of the mqtt source and the structures of the data you need to be stored. And be precise. Every detail makes the difference.

1 Like

Are you wanting to write the different topics to different influx measurements? If so show us some examples of the topics and what you want the measurement to be. Also, what structure are the values coming from MQTT? Is each one just a simple value to be written the the measurement?
Or perhaps they are all to go to the same measurement with tag values derived from the topics. Unless you tell us what you need then it is difficult to help.
If you don't know what Influx Measurements, Fields and Tags are then you need to start by reading the Influx docs.

Hello guys, thank you for your help.

Here is a more detailed description for my project.
I want to evaluate the following topics of MQTT devices (these are only two example devices, in reality there are more, but the syntax always remains the same):

  • tele/Couch/SENSOR
  • tele/Server/SENSOR
    ...

As MQTT IN node I specified

  • topic: "tele/+/SENSOR" (to query all sensors),
  • QoS: "0",
  • Output: "a parsed JSON object".

The debug output shows

  • as topic (for example): "tele/Server/SENSOR"
  • as payload: {"Time":"2022-10-25T08:03:48","ENERGY":{"TotalStartTime":"2022-09-18T23:22:51","Total":0.773,"Yesterday":0.075,"Today":0,"Period":0,"Power":0,"ApparentPower":0,"ReactivePower":0,"Factor":0,"Voltage":240,"Current":0}}

I need the topic and the corresponding values for my InfluxDB (to keep the database as slim as possible I need the values: Power, Voltage, Current, Today).

I am very happy about your help, but it takes some time with the answers.

You have not answered some of the crucial questions:

  1. What Influxdb Fields do you want in the database?
  2. What Influxdb Tags do you want in the database? I guess that you want all of the data to go to the same Influxdb Measurement, using a Tag to identify which device the data relates to. Perhaps you want the central section of the topic to be in a deviceID tag. That may not be what you want though.

[Edit] Actually, I think you have implicitly answered question 1. So the outstanding issue is question 2.

Hello, thank you for your answer.

I need the following fields in my DB:

  • Voltage
  • Current
  • Power
  • Today

I want to display the collected information in Grafana. For that I want to display the data for each device.
It would be nice, if the measurement in influx is named with the topic of my devices.
At the moment I am using the following function:

const newPayload = [
    {
        device: msg.topic,
        voltage: msg.payload.ENERGY.Voltage,
        current: msg.payload.ENERGY.Current,
        power: msg.payload.ENERGY.Power,
        today: msg.payload.ENERGY.Today
    }
];
msg.payload = newPayload;
return msg;

The debug output shows the following:

[{"device":"tele/Switch_Wohnzimmer/SENSOR","voltage":238,"current":0.088,"power":12,"today":0.138}]

With the Influx DB out connector I can store data in my DB with the measurement device.
But I want to get measurement name: tele/"device"/SENSOR.

Hi
@Steve-Mcl in post 2 showed you how to split the topic, you would the use topicParts[1] as the device name.

You would be better to use a Tag for the device id, and put them all in one measurement.

The reason for using at tag is so that in grafana you can use clauses like Where deviceID = sensor1 rather than having to select a different measurement for each sensor. Also it means that as you add or remove sensors you are not changing the structure of the database, just adding a new value for the tag.

So in the payload, as described in the help for the influx out node, you would want something like

msg.payload = [
    {
        voltage: msg.payload.ENERGY.Voltage,
        current: msg.payload.ENERGY.Current,
        power: msg.payload.ENERGY.Power,
        today: msg.payload.ENERGY.Today
    },
    {
        device: msg.topic.split('/')[1]
    }
];

Hi Colin,
I have tested your code and it works fine.
As newbie I have one more question:
I understood, why I should use only one measurement.
But only for my interest: how is it possible to generate measurements from variables?

Thank you for your help! It gave me a lot of knowledge :slight_smile:

It is in the help text for the node:
"If the measurement field is not set in the node configuration, the user can send in data with a specified measurement name in msg.measurement to overwrite the measurement field in the configuration of the node."

So in the function node you could do
msg.measurement = topic

Hi Colin,
great, thank you.
I have to lern a lot using node-red, but I have so many topics I have to lern, that I need some help from outside :slight_smile: .
Other topics I have to lern are

  • docker
  • traefik
  • grafana
  • MQTT
  • etc.
    And everything only for energy saving :wink:

Thank you and take care :slight_smile:

Don't worry about docker and traefik unless you have a specific requirement to use them.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.