1

I want to change the attribute “Nr” of Current User using a custom action on a widget (by click on marker).

I tried to configure this code:

let attributeService = widgetContext.$injector.get(widgetContext.servicesMap.get('attributeService'));

let attributes = [{
    "key": "Nr",
    "value": Nr
}];

attributeService.saveEntityAttributes(entityId, 'SERVER_SCOPE', attributes).
    subscribe(
        () => {
            console.log('Saved!');
        },
        error => {
            console.log('Error');
        }
    );

but if I insert another Entity-Id manually instead of “entityId”, I get the error “No enum constant org.thingsboard.server.common.data.EntityType.undefined” when I use the action.

I also do not know the Entity-Id of Current User (I think there is none?).

Do you know how I could solve this problem?

Thanks!

Context: I have a dashboard with a map with buildings on the left side and detail-widgets of the buildings on the right side. When I click on the buildings on the map, I want to filter the details, so that just the details of that clicked building remain. My idea is to build in a dynamic filter with the Key name “Nr” from Current User. If you click on the marker of a building, the attribute “Nr” of current user would change to the number of the clicked building and the widgets on the right would only show the information of the clicked building.

If I use the “New Update Multiple Attributes” Widget to change the attribute “Nr” everything works fine, but I want to use the map to filter the detail-Widgets.

2 Answers2

1

I got you. Thingsboard needs as "entityId" a JSON that looks like this:

entityId={
    "id": the ID you are tring to put in manually,
    "entityType": the type of the entity (written like this "ASSET", "DEVICE" ecc...)
}

you are giving him the Id of the entity directly so he is looking for the entityType, thats what that errors message says

to get the current user Id use: "self.ctx.currentUser.userId"

or if you are in a custom action: "widgetContext.currentUser.userId"

All together it's:

newEntityId = {};

newEntityId = {"entityType":"USER", "id":self.ctx.currentUser.userId};

attributeService.saveEntityAttributes(newEntityId, 'SERVER_SCOPE', attributes).
    subscribe(
        () => {
            console.log('Saved!');
        },
        error => {
            console.log('Error');
        }
    );

bye!

  • 1
    Thank you very much! If I enter your code or try to use the variable "self", Thingsboard says "self is not defined". I have read that "self" is a build-in variable but it does not work on my widgets. – Hanna Kattermann Sep 05 '22 at 06:29
  • My bad! If you are in a custom action you need to use "widgetContext.currentUser.userId" – Manuel Romano Sep 05 '22 at 06:33
0

Not exactly the answer to your question, but an easier approach to accomplish your usecase, which is provided by ThingsBoard without relying on custom actions:

https://thingsboard.io/docs/pe/user-guide/ui/widget-actions/#update-current-dashboard-state

Just use a second Entity-Alias of Type "Entity From Dashboard State". This alias is updated each time you click on a Marker in the Map (Action "Update Current Dashboard State"). Then you don't need to update any attributes.

mdeuchert
  • 248
  • 2
  • 8