Storing float from MQTT > Influx

Hi all,
I have a MQTT topic that seems to be storing a float as a string. Example:
It is a reading from a electricity consumption monitor and stores 33.2 or 45.3 in MQTT as a string. I have no control of how its doing this.

I have a simple flow in node red that does:
MQTT Electricity Topic > Influx (Measurement)

The issue is it creates a measurement with the following Field Keys:
Time , Value (as string)

I went into the Influx CLI and created my own measurement with Time, Value (Float) but of course (and I expected this) when I reroute the flow to that new measurement, I get the following error:


Error: A 400 Bad Request error occurred: {"error":"partial write: field type conflict: input field \"value\" on measurement \"B14023Device0Study\" is type string, already exists as type float dropped=1"

Which tells me of course that the flow is trying to pass a string value to a field key pairing that requires float.

My question

How I am thinking of it is that in the flow there is going to be a need for me to convert the value coming out of the MQTT topic as string to float, then pass that value to the influx db measurement I created with the field key pairing with the float.

So:

  1. Is my thinking correct
  2. What would be the easiest way to do this conversion of the string value out of the MQTT topic into a float before I pass it to the influx node to write into that measurement? From the research I found this article Convert I can pretty much make out how to convert it. Just want to check my thinking on point 1 first.

Would really appreciate the guidance.

Insert a function node before the influx node and in there put

msg.payload = Number(msg.payload)
return msg

Feed it into a debug node before you connect it to the influx node to check it now shows it as a number, not a string.

Thank you very much @Colin I will test that. Thank you once again I really do appreciate it.

Or try this:
grafik

Yes, much better. I didn't think that worked if the data is a string containing just a number, but it does. If ever the string is not a valid number (or other valid json) then it generates an error, but that can be caught by a Catch node if necessary.

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