cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Programmatically add a property to a Thing Template

SOLVED
Highlighted
Garnet

Programmatically add a property to a Thing Template

Adding a property definition to a Thing programmatically requires a Thing restart in order for the property to come up (https://community.ptc.com/t5/ThingWorx-Developers/Unable-to-add-new-property-programatically-using-s...). Is there a similar process for a Thing Template? There is no "RestartThing" service on the template and it doesn't look like my properties that I add programmatically work.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Programmatically add a property to a Thing Template

Yes you will have to.

 

If you manually add a property to a Thing Template you have to save the changes. When a Thing Template is saved, all the Things with that Thing Template are automatically restarted. In this case, you are not saving so the Things are not restarting. Here is the improved generalized code snippet:

try{
    Things["SimpleTest"].property = 5;
    
    }catch(err){
       var error=err.message;
        if(error.match(/not found on SimpleTest/)!=null){
            var params = {
                defaultValue: undefined /* STRING */,
                remoteBindingAspects: undefined /* JSON */,
                description: undefined /* STRING */,
                readOnly: undefined /* BOOLEAN */,
                type: "NUMBER" /* BASETYPENAME */,
                remote: undefined /* BOOLEAN */,
                remotePropertyName: undefined /* STRING */,
                timeout: undefined /* INTEGER */,
                pushType: undefined /* STRING */,
                dataChangeThreshold: undefined /* NUMBER */,
                logged: undefined /* BOOLEAN */,
                name: "property" /* STRING */,
                pushThreshold: undefined /* NUMBER */,
                dataChangeType: undefined /* STRING */,
                category: undefined /* STRING */,
                persistent: true /* BOOLEAN */,
                dataShape: undefined /* DATASHAPENAME */
            };

            // no return
            ThingTemplates["SimpleTestTemplate"].AddPropertyDefinition(params);
            
            // no return
            Things["SimpleTest"].RestartThing();

	}
}

 Kind regards,

Ezanne

4 REPLIES 4

Re: Programmatically add a property to a Thing Template

Hi agondek

 

I have just tried to add a property to a Thing Template without restarting it and it was successful. I used the ADDPropertyDefinition service under the Thing Template that I wanted to add the property to. Could you please attach the script you are using or any error messages that you are receiving.

What version of ThingWorx are you using?

 

Kind regards,

Ezanne 

Re: Programmatically add a property to a Thing Template

The template does not need a restart, as it does not store the values of the parameters, only their description

Re: Programmatically add a property to a Thing Template

    try{
        Things["ImplementedThing"].property = value;
    }
    catch(err){
        var error=err.message;
        if(error.match(/not found on ImplementedThing/)!=null){
            var params = {
                defaultValue: undefined /* STRING */,
                remoteBindingAspects: undefined /* JSON */,
                description: undefined /* STRING */,
                readOnly: undefined /* BOOLEAN */,
                type: "NUMBER" /* BASETYPENAME */,
                remote: undefined /* BOOLEAN */,
                remotePropertyName: undefined /* STRING */,
                timeout: undefined /* INTEGER */,
                pushType: undefined /* STRING */,
                dataChangeThreshold: undefined /* NUMBER */,
                logged: undefined /* BOOLEAN */,
                name: property /* STRING */,
                pushThreshold: undefined /* NUMBER */,
                dataChangeType: undefined /* STRING */,
                category: undefined /* STRING */,
                persistent: undefined /* BOOLEAN */,
                dataShape: undefined /* DATASHAPENAME */
            };

            // no return
            ThingTemplates["Template"].AddPropertyDefinition(params);
}

Generalized code snippet above. I'm finding that on the next iteration of this service, it attempts to add the property again (ie: the property is still not found on the implemented thing after adding it to the template) Do I need to restart the Implemented Thing?

Re: Programmatically add a property to a Thing Template

Yes you will have to.

 

If you manually add a property to a Thing Template you have to save the changes. When a Thing Template is saved, all the Things with that Thing Template are automatically restarted. In this case, you are not saving so the Things are not restarting. Here is the improved generalized code snippet:

try{
    Things["SimpleTest"].property = 5;
    
    }catch(err){
       var error=err.message;
        if(error.match(/not found on SimpleTest/)!=null){
            var params = {
                defaultValue: undefined /* STRING */,
                remoteBindingAspects: undefined /* JSON */,
                description: undefined /* STRING */,
                readOnly: undefined /* BOOLEAN */,
                type: "NUMBER" /* BASETYPENAME */,
                remote: undefined /* BOOLEAN */,
                remotePropertyName: undefined /* STRING */,
                timeout: undefined /* INTEGER */,
                pushType: undefined /* STRING */,
                dataChangeThreshold: undefined /* NUMBER */,
                logged: undefined /* BOOLEAN */,
                name: "property" /* STRING */,
                pushThreshold: undefined /* NUMBER */,
                dataChangeType: undefined /* STRING */,
                category: undefined /* STRING */,
                persistent: true /* BOOLEAN */,
                dataShape: undefined /* DATASHAPENAME */
            };

            // no return
            ThingTemplates["SimpleTestTemplate"].AddPropertyDefinition(params);
            
            // no return
            Things["SimpleTest"].RestartThing();

	}
}

 Kind regards,

Ezanne