How to check if payload is a subset of an array in function nodes

Hi there,

following situation: I integrated the "Open weather map" node which delivers current weather information for my location. This information is encoded using different IDs for different weather conditions (i.e. 202 - thunderstorm with heavy rain, 221 - ragged thunderstorm etc.). I´m using a change node to extract this information from the original object and set a corresponding msg.payload.

Now comes the challenge: I would like to use a function node to check whether the weather ID is subset of a defined array of IDs (true) or not (false). I´m sure this is easy Java Script magic but all attempts that I trief failed miserably :grimacing: Some Pros around that can help me with that? :slightly_smiling_face:

A javascript switch statement?
https://www.w3schools.com/js/js_switch.asp

Or maybe?

1 Like

Thanks for the quick answere @Paul-Reed. That probably is an option, but I would need to define a case for every weather condition. Is there not a more easy way where I can define an array of numbers and check the payload against that?

Thanks @E1cid. I tried sth. like this now

const CurrentState = msg.payload;
const WeatherIDs = ['202', '212'];

console.log(WeatherIDs.includes(CurrentState));

Is that correct and if so how do I pass on the result?

Try;

const CurrentState = msg.payload;
const WeatherIDs = ['202', '212'];
msg.payload = (WeatherIDs.includes(CurrentState));
return msg;

Note: the incoming msg.payload would be a string "202" and not a number 202 as your example is expecting const WeatherIDs = ['202', '212'];

1 Like

Perfect, that´s it @Paul-Reed and also thanks for the notice of the strings! :slight_smile:

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