Check if tags created

Hello.
Is there a way to check if tag was created for device or not in provision script?
When i tag a device with a tag “MyTag” and if i use in provision script:

let IfMyTag=declare("Tags.MyTag", {value: 1}).value[0];
log(IfMyTag);

Then i get true in the logs.
But if i remove MyTag from device i see in the logs:
script.TypeError" faultMessage="Cannot read properties of undefined (reading '0')

I am trying to make 2 different scenarios:
When MyTag presents-nothing to do
When MyTag doen’s presents on device-do something and then add MyTag with
declare("Tags.MyTag", null, {value: true});

Is it possible? And if yes-how?

You are using the wrong syntax for tags. The value property on tags isn’t an array that can be indexed, hence “Cannot read properties of undefined (reading ‘0’)”. Use this function if you’d like to make things easier.

function hasTag(name) {
    return declare(`Tags.${name}`, {value: 1}).value !== undefined;
}
1 Like

And in name i should use tag name?
In my case MyTag?

Yep, now it is true or false without errors.
Many thanks

this link has what you need.
Example of a Provisioning Flow · genieacs/genieacs Wiki · GitHub