FORUM CTRLX AUTOMATION
ctrlX World Partner Apps for ctrlX AUTOMATION
Dear Community User! We have started the migration process.
This community is now in READ ONLY mode.
Read more: Important
information on the platform change.
02-29-2024 10:31 AM - edited 02-29-2024 10:32 AM
I am so curious, is that possible to simple increment item value via builded write-item function in widget behaviour attributes? something like this:
My notation of course not working. Or I have to create my own UI-Action?
Solved! Go to Solution.
02-29-2024 11:49 AM
Write-item does exactly one thing: write the specified item to the absolute input in the value field - there's no magic parsing happening here so it will not work as you expected. This is true for all fields in WebIQ Designer - nowhere can you do any calculations.
You can however create a custom UI action to achieve this. Please note that this example requires at least WebIQ 2.15.7 - with previous versions you'd have to subscribe to the item just to get the item properties (min/max) and then unsubscribe again.
/**
* Custom UI-Action 'increment-item-value'.
*
* Description:
* [Add description here]
*/
(function() {
var actions = shmi.pkg("visuals.session.userActions"); //get reference to userActions object
/**
* UI-Action 'increment-item-value' implementation
*
* @params {any[]} parameters configured ui-action parameters
* ---- Initial parameters, needs to be updated manually when changed ----
* @param {string} parameters[0] Item
*
*/
actions["increment-item-value"] = function(parameters) {
const itemName = parameters[0],
im = shmi.visuals.session.ItemManager;
// Read item value
im.readDirect([itemName], function(err, result) {
if (!err) {
let itmData = {};
// Read min/max
im.getItemProperties([itemName], (err, properties) => {
if (!err) {
const max = properties[itemName].max; // min would be for minimum
const newValue = result[itemName] + 1;
if (newValue > max) {
console.log("Not updating item value, because it would exceed the maximum value of " + max);
} else {
// Write item value
itmData[itemName] = result[itemName] + 1;
im.writeDirect(itmData, function(err, result) {
if (!err) {
console.log('Increased item value to ' + itmData[itemName]);
} else {
shmi.notify("Couldn't write item: " + itemName, 'ERROR');
}
});
}
} else {
shmi.notify("Couldn't read item properties: " + itemName, 'ERROR');
}
});
} else {
shmi.notify("Couldn't read item: " + itemName, 'ERROR');
}
});
};
}());