Now, I have a new sensor which is not transmitting humidity. but the topic structure have to be the same.
So, at the moment, I get an error because no hum value exists.
Is it possible to change this script to make it able to do both kinds of mqtt messages?
If it is possible, I won't like to use a seperate flow or a new topic structure.
If there is no humidity value then preferably you should not save it to a Measurement that normally includes temp and hum as this means that your db is full of empty holes, which is inefficient. Possibly the best way would be to split it into two measurements, temp and hum, and feed values to both measurements for those sensors that have humidity and only to the temp one for those without humidity.
If you feed the output of that into a function node you will see that it is still showing hum: undefined. It is always best when testing to add a debug node to the output of the node you are changing to see what comes out.
I think, if you do want them all to go to the same measurement even though you will end up with a db full of holes, then you need something like
let thisRoom = msg.topic.split("/")[2]
const hum = msg.payload.hum
msg.payload = [{temp: msg.payload.temp},{room: thisRoom}]
// add the humidity into the message if it exists
if(typeof hum != "undefined"){
msg.payload[0].hum = hum
}
return msg
Check in a debug node that this gives what you expect before connecting it to the db.
yeah, maybe it is not absolutly clean. but hey, I planed to do the humidity measurement in that room too. I have just messed up the order for the sensor. So my sensor is not able to do the humidity measurement. Maybe I replace it sometime.
Or do you think it's better to write a "0" instead of just nothing like now?
It was intended to be a rhetorical question, to suggest whether zero or nothing should go in the db. I would say don't put zero in the db unless the value is zero. If you don't know what it is then don't put anything in.
thank you colin,
I know you are an expert and you did help me a lot in the past and also this time. but i am not sure if it is worth to change my db that much just because of this guinea pig barn sensor....
or do I just have to add some kind of measurement2 and ask you for the filtering function? I can do a bit C++ Arduino programming but I am not into this function scripting in node-red so I have to ask for everything. feels bad man.
lol
I fear the will never happen. to be honest, i placed that device in the barn and as long as it is working well, the motivation to replace it is not really existant
maybe I should give it a try.
So you suggest not to change the temp/hum sensors. Just to add a new flow in node-red which filters the room "sau" and write only the temp.
1.) So, therefore I have to change the function of my temp/hum flow in that kind, that they do not try to process date from the topic "sensors/room/sau".
at the moment they respond on "sensors/room/#". So before I switched to your code today, they throw an error everytime the message to "sensors/room/sau" arrived. That was not a desaster but it is also not very clean.
2.) I have to add a flow only for "sensors/room/sau". I think I am able to do that by myself. at least I hope so.
You just need one flow, but it should send all temperature data to one measurement and humidity to another. You can do that by making it send one or two messages.
That will cope with either one of both values present, passing on one or two messages as appropriate. The convention if you just have one value in a measurement is to call the value 'value' as what it is is identified by the measurement name. You will need to clear the measurement name in the influx node so that it will use the measurement name from the messages.
The Optional Chaining operator ?. tests the preceding property to see if it exists, so in that code you are testing msg.payload, which will always exist (in this context). So that line is exactly the same as msg.payload = [{temp: msg.payload.temp, hum: msg.payload.hum},{room: thisRoom}]