How to compare numeric values in the function node in node-red? - function

I've been trying to do a simple numeric comparison since days in my function node but I really don't have any idea why it's not working. I have a function node which accepts two values. I've even converted it from object to Number but still the comparison won't work. Please find the full flow here:
[{"id":"39421a3d.5cda36","type":"function","z":"251d0ac6.958a36","name":"getL1MagneticCount","func":"msg.payload = {\"getCarCount1\":msg.payload};\nreturn msg;","outputs":1,"noerr":0,"x":586.6666259765625,"y":606.6666259765625,"wires":[["31136d74.228fb2"]]},{"id":"a171070a.1ba198","type":"function","z":"251d0ac6.958a36","name":"getL2MagneticCount","func":"msg.payload = {\"getCarCount2\":msg.payload.Car};\nreturn msg;","outputs":1,"noerr":0,"x":586.6666259765625,"y":719.9999732971191,"wires":[["31136d74.228fb2"]]},{"id":"31136d74.228fb2","type":"function","z":"251d0ac6.958a36","name":"comparison","func":"var count1 = Number(msg.payload.getCarCount1);\nvar count2 = Number(msg.payload.getCarCount2);\n\nif(count1 >= count2){\n console.log(\"In\");\n msg.payload = msg.payload.getCarCount1;\n return [msg,null];\n \n} else {\n console.log(\"Out\");\n msg.payload = msg.payload.getCarCount2;\n return [null,msg];\n \n}","outputs":"2","noerr":0,"x":824.4443950653076,"y":663.3333148956299,"wires":[["57c8e7b7.c948e8"],["10b4a39f.16338c"]]},{"id":"57c8e7b7.c948e8","type":"debug","z":"251d0ac6.958a36","name":"","active":true,"console":"false","complete":"payload","x":1025.5556182861328,"y":626.6666140556335,"wires":[]},{"id":"10b4a39f.16338c","type":"debug","z":"251d0ac6.958a36","name":"","active":true,"console":"false","complete":"false","x":1028.8889236450195,"y":709.9999084472656,"wires":[]},{"id":"1a6938ca.0d2bf7","type":"inject","z":"251d0ac6.958a36","name":"","topic":"","payload":"3","payloadType":"str","repeat":"","crontab":"","once":false,"x":256.6666679382324,"y":605.555606842041,"wires":[["39421a3d.5cda36"]]},{"id":"d23e60e5.adb83","type":"inject","z":"251d0ac6.958a36","name":"","topic":"","payload":"0","payloadType":"str","repeat":"","crontab":"","once":false,"x":254.66665649414062,"y":719.5555419921875,"wires":[["a171070a.1ba198"]]}]
Please tell me where my mistake is. Thank you very much.

The problem is that each of the input messages are handled as independent events in the function node, so you only ever have 1 value to compare each time a message arrives.
What you need to do is make use of the context to store values between each message. Something like this:
//get stored values if present
var count1 = context.get("count1");
var count2 = context.get("count2");
if (msg.payload.hasOwnProperty("getCarCount1")) {
count1 = msg.payload.getCarCount1;
context.set("count1", count1);
}
if (msg.payload.hasOwnProperty("getCarCount2")) {
count2 = msg.payload.getCarCount2;
context.set("count2", count2);
}
if (count1 != undefined && count2 != undefined) {
if(count1 >= count2){
console.log("In");
msg.payload = count1;
return [msg,null];
} else {
console.log("Out");
msg.payload = count2;
return [null,msg];
}
}

I'm saving to global variables for the beginning temperature and humidity data from different sensors. In a block I'm checking these variables for "is not null", "nan" and then condition.. Maybe it will help.

Related

Is there a way to sort a table based on a cell value in Angular?

My current table looks like this:
Status
Draft
Pending
Complete
I want to sort them based on the value of the cells. Is there a way to do that? I've only been able to sort them using this code:
onChange(status: string){
const sortState: Sort = {active: status, direction: 'desc'};
this.sort.active = sortState.active;
this.sort.direction = sortState.direction;
this.sort.sortChange.emit(sortState);
}
But I want to sort using the values of the status themselves since I'd want to create a button which when click sorts starting from complete or draft or pending.
I'm a little confused by your question, but I think I understand what you're asking.
You're going to want to convert your values into an array and then use the .sort() function. So, assuming you have an array of your cells, we can call that let array = Cell[], you can then access the status of the cells like this:
sortCells(){
let array = Cell[]; // here we're assuming there is already a cell type and a cell.active parameter, like shown in your example.
let possibleValues = ["Draft","Pending","Complete"]; // easier way to compare two values
array.sort((a,b)=>{
let aIndex = possibleValues.indexOf(a.active); // index of gets the location of the element in an array
let bIndex = possibleValues.indexOf(b.active);
if(a > b){
return -1;
} else if(b > a){
return 1;
}else{
return 0; // they are equal
}
})
}
You can read more about sort here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

Separate strings in a string with Actionscripts3

I'm trying to separate two part of a string, one is Title one is Value, RegExp is confused me. I need your help to solve this thanks
var pattern2:RegExp = new RegExp("TZ_NUM_ANSWER:Telegram code([0-9.-]+)");//TZ_NUM_ANSWER:Telegram code 32263
var data2:Object = pattern2.exec(response);
if (data2 != null && data2[1] != null)
{
var value2:Number = parseFloat(data2[1]);
trace("TZ_NUM_ANSWER " + value2);
txt_BUY1.text = String(value2);
}
Output:
TZ_NUM_ANSWER:Telegram code 32263
It must be:
"TZ_NUM_ANSWER:" "Telegram code 32263"
The result of split is an Array you can access to Array indexes and assign them to a variable.
var STR1:String = "TZ_NUM_ANSWER:Telegram code 32263";
var STR2:String;
var STR3:String;
trace(STR1.split(":"));
STR2 = STR1.split(":")[0];
STR3 = STR1.split(":")[1];
trace (STR2);
trace (STR3);
Result:
TZ_NUM_ANSWER
Telegram code 32263
Don't use RegEx for simple stuff. All you need is basic string methods:
response.split(":");

Is there a simple way to have a local webpage display a variable passed in the URL?

I am experimenting with a Firefox extension that will load an arbitrary URL (only via HTTP or HTTPS) when certain conditions are met.
With certain conditions, I just want to display a message instead of requesting a URL from the internet.
I was thinking about simply hosting a local webpage that would display the message. The catch is that the message needs to include a variable.
Is there a simple way to craft a local web page so that it can display a variable passed to it in the URL? I would prefer to just use HTML and CSS, but adding a little inline javascript would be okay if absolutely needed.
As a simple example, when the extension calls something like:
folder/messageoutput.html?t=Text%20to%20display
I would like to see:
Message: Text to display
shown in the browser's viewport.
You can use the "search" property of the Location object to extract the variables from the end of your URL:
var a = window.location.search;
In your example, a will equal "?t=Text%20to%20display".
Next, you will want to strip the leading question mark from the beginning of the string. The if statement is just in case the browser doesn't include it in the search property:
var s = a.substr(0, 1);
if(s == "?"){s = substr(1);}
Just in case you get a URL with more than one variable, you may want to split the query string at ampersands to produce an array of name-value pair strings:
var R = s.split("&");
Next, split the name-value pair strings at the equal sign to separate the name from the value. Store the name as the key to an array, and the value as the array value corresponding to the key:
var L = R.length;
var NVP = new Array();
var temp = new Array();
for(var i = 0; i < L; i++){
temp = R[i].split("=");
NVP[temp[0]] = temp[1];
}
Almost done. Get the value with the name "t":
var t = NVP['t'];
Last, insert the variable text into the document. A simple example (that will need to be tweaked to match your document structure) is:
var containingDiv = document.getElementById("divToShowMessage");
var tn = document.createTextNode(t);
containingDiv.appendChild(tn);
getArg('t');
function getArg(param) {
var vars = {};
window.location.href.replace( location.hash, '' ).replace(
/[?&]+([^=&]+)=?([^&]*)?/gi, // regexp
function( m, key, value ) { // callback
vars[key] = value !== undefined ? value : '';
}
);
if ( param ) {
return vars[param] ? vars[param] : null;
}
return vars;
}

Using ItemCollection on a BoxFolder type with Box API only returns 100 results and cannot retrieve the remaining ones

For a while now, I've been using the Box API to connect Acumatica ERP to Box and everything has been going fine until recently. Whenever I try to use a BoxCollection type with the property ItemCollection, I'll only get the first 100 results no matter the limit I set in the GetInformationAsync(). Here is the code snippet:
[PermissionSet(SecurityAction.Assert, Name = "FullTrust")]
public BoxCollection<BoxItem> GetFolderItems(string folderId, int limit = 500, int offset = 0)
{
var response = new BoxCollection<BoxItem>();
var fieldsToGet = new List<string>() { BoxItem.FieldName, BoxItem.FieldDescription, BoxItem.FieldParent, BoxItem.FieldEtag, BoxFolder.FieldItemCollection };
response = Task.Run(() => Client.FoldersManager.GetFolderItemsAsync(folderId, limit, offset)).Result;
return response;
}
I then pass that information on to a BoxFolder type variable, and then try to use the ItemCollection.Entries property, but this only returns 100 results at a time, with no visible way to extract the remaining 61 (in my case, the Count = 161, but Entries = 100 always)
Another code snippet of the used variable, I am basically trying to get the folder ID based on the name of the folder inside Box:
private static void SyncProcess(BoxFolder rootFolder, string folderName)
{
var boxFolder = rootFolder.ItemCollection.Entries.SingleOrDefault(ic => ic.Type == "folder" && ic.Name == folderName);
}
I wasn't able to find anything related to that limit = 100 in the documentation and it only started to give me problems recently.
I had to create a work around by using the following:
var boxCollection = client.GetFolderItems(rootFolder.Id);
var boxFolder = boxCollection.Entries.SingleOrDefault(ic => ic.Type == "folder" && ic.Name == folderName);
I was just wondering if there was a better way to get the complete collection using the property ItemCollection.Entries like I used to, instead of having to fetch them again.
Thanks!
Box pages folder items to keep response times short. The default page size is 100 items. You must iterate through the pages to get all of the items. Here's a code snippet that'll get 100 items at a time until all items in the folder are fetched. You can request up to 1000 items at a time.
var items = new List<BoxItem>();
BoxCollection<BoxItem> result;
do
{
result = await Client.FoldersManager.GetFolderItemsAsync(folderId, 100, items.Count());
items.AddRange(result.Entries);
} while (items.Count() < result.TotalCount);
John's answer can lead to a duplicate values in your items collection if there will be external/shared folders in your list. Those are being hidden when you are calling "GetFolderItemsAsync" with "asUser" header set.
There is a comment about it in the Box API's codeset itself (https://github.com/box/box-windows-sdk-v2/blob/main/Box.V2/Managers/BoxFoldersManager.cs)
Note: If there are hidden items in your previous response, your next offset should be = offset + limit, not the # of records you received back.
The total_count returned may not match the number of entries when using enterprise scope, because external folders are hidden the list of entries.
Taking this into account, it's better to not rely on comparing the number of items retrieved and the TotalCount property.
var items = new List<BoxItem>();
BoxCollection<BoxItem> result;
int limit = 100;
int offset = 0;
do
{
result = await Client.FoldersManager.GetFolderItemsAsync(folderId, limit, offset);
offset += limit;
items.AddRange(result.Entries);
} while (offset < result.TotalCount);

Need to alter space from postal code in results array

Hi my requirement need to get postal code from
var address= results[0].formatted_address ;
this formatted value.
Because nether land address have "Danzigerkade 12,1013 AP Amsterdam,Netherlands" this kind of address. but i don't want postal code like this"1013 AP" . i need "1013AP" like this.
Please give me the solution.
Thanks in advance.
I don't recommend parsing the formatted_address to get the postal code or any other specific address fields. Instead, you should scan through the address_components and check the types array of each one to find the address field you need. This is much more reliable than parsing the formatted string.
Once you have the postal code, removing the space is trivial.
To find the postal code for an entry in your results array (e.g. results[0]), you can use code like this:
function getAddressComponent( result, type ) {
var components = result.address_components;
for( var i = 0; i < components.length; ++i ) {
var component = components[i], types = component.types;
for( var k = 0; k < types.length; ++k ) {
if( types[k] == type ){
return component;
}
}
}
return {};
}
var component = getAddressComponent( results[0], 'postal_code' );
var postalCode = component ? component.short_name : '';
var postalCodeNoSpace = postalCode.replace( ' ', '' );
console.log( postalCodeNoSpace );
Update in reply to your comment:
The code you're asking about with ? and : uses the conditional operator found in JavaScript and many other languages:
var postalCode = component ? component.short_name : '';
That works just like this longer form that should look more familiar:
if( component )
postalCode = component.short_name;
else
postalCode = '';
The idea was to not try to reference component.short_name if component itself is null or undefined, because of course that would be an error. In other words, to protect the program from crashing if getAddressComponent() does not find a postal code.
But interestingly enough, there's a bug in the way I was using it. Look at the last line of getAddressComponent():
return {};
Originally I was going to return null there - and then the code you asked about would have been correct - but for some reason I decided to return an empty object instead. So the code in question wasn't quite right with that change.
One way to fix this would be to go back to what I originally meant to do, and change the last line of getAddressComponent() from this:
return {};
to:
return null;