Possible to have node.js script on RPI retrieve value from balena dash device variables?

Hello forum,

I’m running a basic node.js script on RPI4’s. They compile a JSON after they detect some action in the field and send it on to an API on my server. The JSON contains the RPI’s unique identifier so my API can throw the received data into the right database.

To deal with each unit’s unique identifier I currently change this variable inside the node.js script for each unit. This necessitates me setting up each individual unit as a separate application. As our fleet is expected to start growing soon this will make updates a bit laborious. Ideally I want all my devices under one application so I can bulk update all at once. The only issue is this unique identifier.

My question: Is there a way to set this identifier in the device variables on the balena dashboard, and have the script retrieve that info? This would allow me to flash all units with the same image without the need to change each one’s identifier in the script.

Hello @heinburgh,

Yes, this is a very common use case and that’s what device variables are for! For example, I added a “Device variable” to one of my devices named UNIQUE_ID=12345, then if I ssh into the container I can access it as I would any other env var:

bash-5.0# printenv | grep UNIQUE_ID

bash-5.0# node
Welcome to Node.js v14.16.0.
Type ".help" for more information.
> process.env.UNIQUE_ID

Since I added this “Device variable” to one particular device, other devices in the same application won’t see this. You can individually set it for each one. If you go this route you might be interested in checking our nodejs SDK that will allow you to set env vars programatically: Balena Node.js SDK - Balena Documentation

However I have another suggestion that’s what I’ve done in the past in cases like this and it’s to use balena’s device uuid as the unique identifier. Each device already has a unique identifier (UUID) that’s assigned by balenaCloud. You can see uuid’s for your devices via the dashboard but they are also readily available to use inside your containers as the environment variable BALENA_DEVICE_UUID. The great thing about this approach is that there is nothing extra you need to do. Here is an example from one of my devices:

bash-5.0# printenv | grep BALENA_DEVICE_UUID

bash-5.0# node
Welcome to Node.js v14.16.0.
Type ".help" for more information.
> process.env.BALENA_DEVICE_UUID

Hope this is useful! Cheers!

That’s great, thanks @tmigone. I like your suggestion to use the UUID but we follow a numbering regime which is incompatible with that method.

Hello @tmigone, I have another question more or less in the same vein: the temp, memory, CPU and disk space values in the top righthand corner of the newer supervisor dashboards, are these values available to RPI’s running on Balena?

I’ve just set up a little NodeJS script on a RPI 4 which makes a “heartbeat” entry into a mysql database every 5 minutes so I can check it’s still alive, and it would be awesome to have this data available to log.

Hello, you can get these metrics information from the device-state API endpoint. They are updated every minute.