Update variable from script
This example shows how to update a variable directly from a detached script that runs at a fixed interval.
Declare a detached script with
detached.scriptsto run a JS script every second using a cron expression.Create an integer variable with
owner.variablesto store the result.Write a JavaScript function that generates a random number and writes it directly to the variable.
git checkout origin/osp-scripts-configuration .
git checkout origin/osp-variables-configuration .
git checkout origin/example-variables-update-from-script .
Steps
1. Create a detached script that will be executed every second
root/examples/variables/update-variable-from-script/script/detached.scripts
{
"moduleId": "modules.scripts.scripts-1",
"scheduledExecutions": ["* * * ? * * *"],
"accessedValues": [],
"sourceFile": "root/variables/update-variable-from-script/script/script.js"
}
2. Create an integer variable
root/examples/variables/update-variable-from-script/variable/value.ospp
{
"name": "Integer variable",
"description": "Variable updated from a script",
"type": "INTEGER"
}
root/examples/variables/update-variable-from-script/variable/owner.variables
{
"moduleId": "modules.variables.variables-1"
}
3. Write a script that generates a random number and updates the variable
root/examples/variables/update-variable-from-script/script/script.js
main();
function main() {
let number = Math.floor(Math.random() * 100);
values.update("root.variables.update-variable-from-script.variable", number);
}
4. Visualize the value change
The example Visualize current state of values shows how to display a value.