Smarter programming with subflow and sensors

I want to use one common subflow for many types of sensors to get the sensors value.
The sensors value is received from the MQTT message with "msg.topic" = "tele/xxx/SENSOR", sent with TELEPERIOD interval.
A "Environment Variables" is used to configure the sensor type to the subflow.
I use the "mqtt node in"-node to get the MQTT message to the "Function"-node.

The solution below works, in a "Function node", but there must be a smarter way to avoid all these hardcoded If-conditions (= less code).

if (msg.topic === "tele/" + env.get("MQTTTopic") + "/SENSOR") 
    {
    if (env.get("Sensor") === "DS18B20.Temperature")   { indata = msg.payload.DS18B20.Temperature }
	if (env.get("Sensor") === "DS18B20_1.Temperature") { indata = msg.payload.DS18B20_1.Temperature }
	if (env.get("Sensor") === "DS18B20_2.Temperature") { indata = msg.payload.DS18B20_2.Temperature }
	if (env.get("Sensor") === "DS18B20_3.Temperature") { indata = msg.payload.DS18B20_3.Temperature }
	if (env.get("Sensor") === "DS18B20_4.Temperature") { indata = msg.payload.DS18B20_4.Temperature }
	if (env.get("Sensor") === "DS18B20_5.Temperature") { indata = msg.payload.DS18B20_5.Temperature }
	if (env.get("Sensor") === "DS18B20_6.Temperature") { indata = msg.payload.DS18B20_6.Temperature }
	if (env.get("Sensor") === "DS18B20_7.Temperature") { indata = msg.payload.DS18B20_7.Temperature }
	if (env.get("Sensor") === "DS18B20_8.Temperature") { indata = msg.payload.DS18B20_8.Temperature }
	if (env.get("Sensor") === "ANALOG.Temperature1")   { indata = msg.payload.ANALOG.Temperature1 }
	if (env.get("Sensor") === "ANALOG.Temperature2")   { indata = msg.payload.ANALOG.Temperature2 }
	if (env.get("Sensor") === "ANALOG.Temperature3")   { indata = msg.payload.ANALOG.Temperature3 }
	if (env.get("Sensor") === "ANALOG.Temperature4")   { indata = msg.payload.ANALOG.Temperature4 }
	if (env.get("Sensor") === "ANALOG.Temperature5")   { indata = msg.payload.ANALOG.Temperature5 }
	if (env.get("Sensor") === "ANALOG.Temperature6")   { indata = msg.payload.ANALOG.Temperature6 }
	if (env.get("Sensor") === "ANALOG.Temperature7")   { indata = msg.payload.ANALOG.Temperature7 }
	if (env.get("Sensor") === "ANALOG.Temperature8")   { indata = msg.payload.ANALOG.Temperature8 }
	if (env.get("Sensor") === "DHT11.Temperature")     { indata = msg.payload.DHT11.Temperature }
	if (env.get("Sensor") === "DHT12.Temperature")     { indata = msg.payload.DHT12.Temperature }
	if (env.get("Sensor") === "HTU21.Temperature")     { indata = msg.payload.HTU21.Temperature }
	if (env.get("Sensor") === "SHT1X.Temperature")     { indata = msg.payload.SHT1X.Temperature }
	if (env.get("Sensor") === "SHT30.Temperature")     { indata = msg.payload.SHT30.Temperature }
	if (env.get("Sensor") === "SHT4X.Temperature")     { indata = msg.payload.SHT4X.Temperature }
	if (env.get("Sensor") === "BME280.Humidity")       { indata = msg.payload.BME280.Humidity }
	if (env.get("Sensor") === "BME280.Pressure")       { indata = msg.payload.BME280.Pressure }
.
.
    }

Assuming the env value always exactly matches the property names in the payload, you can use

const prop = env.get('sensor')
indata  = RED.util.getObjectProperty(msg.payload,prop);
...

PS: About your post formatting:

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil: icon.

See this post for more details - How to share code or flow json

1 Like

Further to Steve's info, here is where you can find more api modules Node-RED

1 Like

What does an unaltered incoming MQTT message look like?
Use a debug node showing the complete message object to grab the value.

Great ! It works :wink: