How to dynamically assign new devices to new users

Hi all, I really hope you can help me with this. I have been looking at tutorials and at the posts in the community, and I cannot seem to find an answer to the problem I´m facing. I have created a blueprint dashboard, where each user will see sensor data from his/her device (or filter with the dropdown in case they have more than one). When the user logs in for the first time, they go to a form where they add their device (creating a new fantasy name and scanning a QR code that contains a serial number defined by me), and then I need a way to map this device to this user.

Previous to that, in the factory, I had to create the device in Tago so that I can obtain the token to put in the firmware of my device, and I also stored a tag called serial_number in the device with the value of the serial number of this device (which will later be obained by the user scanning the QR code in the phisical device).

When the user that acquired a new device fills the “Add new device” form, there should be some sort of analysis that maps this user to the device, based on the serial number. I was thinking of using as an internal_mapping_tag the email of the user. So what I´m struggling now is with how to implement the following in the analysis:

  1. new user email has to be mapped to tag internal_mapping_tag inside the user.
  2. when user does the process of adding a device, then I have to find the device filtered by serial_number that the user scanned, and then that device has to get its name changed by the one the user created, and the tag internal_mapping_tag of the device has to have the value=user email.

I would appreciate if you can point me in the right direction of how to implement this easily.

Regards,
Enrique

Hi,
This can only be done by using analysis. Almost all widgets has an option to trigger an Analysis when the user takes some action. In the input widget this is done by clicking in the button, and then Visual to enable the analysis for this widget.

Once your widget triggering the analysis, you can handle any sort of action.
Input widget also has a field type for “Capture User” if you need to use user information in anyway.

You can try to find an specfic device by using the account.devices.list method and edit the device using the account.devices.edit method.

You can see multiple cases of use of this methods by going through the Analysis snippets at TagoIO.

For example, if you want to search for a device by it’s tags:

  const account = new Account({ token: environment.account_token });

  const devices_found = await account.devices.list({ fields: ['id', 'name', 'tags'], filter: { tags: [{ key: 'serial_number', value: 'XXXX' }] } });
  if (!devices_found.length) return context.log('No device found');
  
  const first_device_found = devices_found[0];

And if you want to edit a specific device tags:

  const account = new Account({ token: environment.account_token });

  const device_info = await account.devices.info("Device ID");

  let new_tag = device_info.tags;

  new_tag = new_tag.filter((tag) => tag.key !== "internal_mapping_tag"); // remove any internal_mapping_tag

  new_tag.push({ key: "internal_mapping_tag", value: "user@mail.com" }); // add a new internal_mapping_tag

  account.devices.edit("Device ID", { tags: new_tag });

Hi vitor, sorry for the late reply, but I managed to implement this thanks to the help from the Support team and some additional research (and trial and error :wink: ). By the way, now that you mention “Capture User”, this didn´t work for me. I included that field in the Form widget of my blueprint dashboard in order to capture the User´s email, but then in my analysis that came always empty from the scope. Maybe there is something not working right regarding that feature? Thanks for your help!