Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
JavaScript: How to write to multiple outputs
#1
Is there any way to send values to multiple outputs using a Java Script?
Reply
#2
Hi,

You have to include the messages for each ouput tag inside an array like the example below:

[{"tag":"outputTag1","value":'+ value1+'},{"tag":"outputTag2","value":'+ value2+'}]

the better way is to create a json message for each tag first and store them to variables like the example below

//... code
result1 =  '{"tag":"outputTag1", "value":'+ value1+ '};
//..code
result2 =  '{"tag":"outputTag2", "value":'+ value2+ '};
//..code
result3 =  '{"tag":"outputTag3", "value":'+ value3+ '};

result = [result1, result2, result2]




This is a completed example.

Create three tags:

inputTag - V0  (Script runner tag)

outputTag1 -V1

outputTag2 - V2


Code:
//--- Copy values from Virtuino Variables to JavaScript variables
var inputValue = Virtuino.readValue("inputTag"); //Read inputTag value as String.     

//---  enter the JavaScript code like the example below

if ((inputValue!=null) & (!isNaN(inputValue))) {
  const x=100;
  var newValue1 = x * inputValue;                     
  //create the json for outputTag1 
  result1 = '{"tag":"outputTag1", "value":'+ newValue1+ '}';
  const y=1000;
  var newValue2 = y * inputValue;                     
  //create the json for outputTag2
  result2 = '{"tag":"outputTag2", "value":'+ newValue2+ '}';
  //create the final array
  result= [result1,result2];
}
else  {          // no input value available yet
  result = null;  // return null if you don't want to send a value to output variable
}
Reply
#3
Hi, 

I'm trying to convert a selector switch to multiple Modbus outputs.
I can see that the javascript seems to work when I compile, however, the data is not sent to the Modbus Logo.

This is my code.
The outputs themselves do work towards the Modbus Logo.

see also attached screenshot.

Code:
//--- Copy values from Virtuino Variables to JavaScript variables
var BranderModus = Virtuino.readValue("InputTag"); //Read inputTag value as String. 

if (BranderModus == 1) {
  Branderuit = '{"tag":"OutputTag1", "value":1}';
  Branderaan = '{"tag":"OutputTag2", "value":0}';
} else if (BranderModus == 2) {
  Branderuit = '{"tag":"OutputTag1", "value":0}';
  Branderaan = '{"tag":"OutputTag2", "value":0}';
} else if (BranderModus == 3) {
  Branderuit = '{"tag":"OutputTag1", "value":0}';
  Branderaan = '{"tag":"OutputTag2", "value":1}';
} else {
  result = null;  // return null if you don't want to send a value to output variable
}

result = [Branderuit, Branderaan];


Attached Files Thumbnail(s)
   
Reply
#4
Hi, you can check if it works using the emulator first. Just replace the modbus with the emulator output variables

This correction is better in case the input value is not in range 1..3

Code:
if (BranderModus == 1) {
  Branderuit = '{"tag":"OutputTag1", "value":1}';
  Branderaan = '{"tag":"OutputTag2", "value":0}';
  result = [Branderuit, Branderaan];
} else if (BranderModus == 2) {
  Branderuit = '{"tag":"OutputTag1", "value":0}';
  Branderaan = '{"tag":"OutputTag2", "value":0}';
  result = [Branderuit, Branderaan];
} else if (BranderModus == 3) {
  Branderuit = '{"tag":"OutputTag1", "value":0}';
  Branderaan = '{"tag":"OutputTag2", "value":1}';
  result = [Branderuit, Branderaan];
} else {
  result = null;  // return null if you don't want to send a value to output variable
}

I don't know why it happens. I have checked your script. It works fine. You have to post more information about this issue.
Reply
#5
The code indeed works.
I had to close the software and reopen it.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)