Understanding Environment Variables
Regardless of the use case or end application, many IoT solutions must manage application state or configuration settings across multiple devices, even after those devices have been deployed into the field. Rather than forcing you to implement your own state management system, the Notecard has two built-in approaches for managing shared state:
- Environment Variables.
- Database (DB) Notefiles.
DB Notefiles are useful for sharing state directly between Notehub and a Notecard on a 1-1 basis. For more information, see Using Database Files for Replicated State.
Environment Variables, on the other hand, are a state and settings management feature that works for device fleets of any size, and allows several levels of control for variables that should apply to a device, across a Fleet, or to all Devices in a Project. These variables are key-value pairs, can be set in the Notehub UI or through the Notehub API, and propagate to devices in a project or fleet using the same synchronization mechanism used for Notes and Notefiles.
See environment variables in action in these accelerator projects.
The Hierarchy of Environment Variables
Environment variables can be defined in a number of locations: the Notecard, the Notehub device, the Device Fleet, and the Notehub Project. Variables set at different levels of this hierarchy can override one another. When obtaining an environment variable, the Notecard uses the following priority order, where the first matched result is returned:
- The value set on that Notecard with the (deprecated)
env.set
request. - The value set in Notehub directly at the Device-level.
- The value set in Notehub on a Fleet to which the Device belongs.
- The value set in Notehub on the Project to which the Device belongs.
- The value set on that Notecard with the
env.default
request.
When a host requests a variable from the Notecard using env.get
, the Notecard
returns the current value based on the following decision tree.
Setting Environment Variables
Environment Variables can be managed in multiple places, depending on the scope of the variable. The following sections provide additional guidance for setting variables across scopes.
User-created environment variables should not start with an underscore to avoid any potential conflicts with system environment variables reserved by Blues.
Setting a Device Local Variable
Set Where | Set How | Available on Notecard |
---|---|---|
Notecard | env.set request | Immediately |
The env.set
API is deprecated as of v7.2.2. We recommend setting environment
variables in Notehub using either the Notehub user interface or
Notehub API. You may also
use the env.default
API to provide a default value for an environment
variable, until that variable is overridden by a value from Notehub.
Set an environment variable directly on a Device using an
env.set
request.
Once set, this variable can be retrieved immediately with an
env.get
request.
When setting environment variables on the Notecard LoRa, the environment variables must be templated.
A Device local variable is the highest-priority setting. Variables set in this way cannot be overridden by variables set at the Notehub Device, Project, or Fleet level.
{
"req": "env.set",
"name": "temp-threshold",
"text": "80"
}
J *req = notecard.newRequest("env.set");
JAddStringToObject(req, "name", "temp-threshold");
JAddStringToObject(req, "text", "80");
notecard.sendRequest(req);
req = {"req": "env.set"}
req["name"] = "temp-threshold"
req["text"] = "80"
card.Transaction(req)
Setting a Notehub Device Variable
Set Where | Set How | Available on Notecard |
---|---|---|
Notehub | Notehub Device Settings or a request through the Notehub API | After Next Sync |
Environment variables can also be managed within Notehub on individual
Devices. This provides the ability to configure device-specific settings for the
Notecard from within Notehub without issuing env.set
requests directly to the
Device. In addition, it provides developers with the ability to remove
Device-specific settings and apply Fleet or Project variables without having
to modify host firmware.
To set an environment variable on the Notehub Device, browse to the Device in notehub.io, select the Device and click "View."
Then, select the "Environment" tab to view and edit environment variables.
Alternatively, you can set a Notehub Device variable using a Set Device Environment Variables request to the Notehub API or by uploading key-value pairs of environment variables in CSV format.
A Notehub Device variable is available to the Notecard via an env.get
request after the next sync.
Setting a Fleet Variable
Set Where | Set How | Available on Notecard |
---|---|---|
Notehub | Set request through the Notehub API | After Next Sync |
Fleet-level variables apply to all Notecards in a Notehub-managed Fleet. They have a higher-priority than Project and Device default variables, but are overridden by Notehub Device and Device-local variables.
To set an environment variable on the Fleet, browse to the Fleets tab in notehub.io, select the fleet you wish to update, and click "Settings."
Then, view and edit environment variables from the Environment tab on the Fleets settings screen.
Alternatively, Fleet variables can be set using a Set Fleet Environment Variables request to the Notehub API or by uploading key-value pairs of environment variables in CSV format.
A Notehub Fleet variable is available to the Notecard via env.get
after the
next sync.
Setting a Project-Wide Variable
Set Where | Set How | Available on Notecard |
---|---|---|
Notehub | Notehub Project Settings or a request through the Notehub API | After Next Sync |
Project-level variables apply to all Notecards across all Fleets in a Notehub project. They have a higher-priority than Device default variables, but are overridden by Fleet, Notehub Device, and Device-local variables.
To set an environment variable on a Project, browse to the Project in notehub.io and select the "Environment" menu under "Settings."
Alternatively, you can set a Project variable using a Set Project Environment Variables request to the Notehub API or by uploading key-value pairs of environment variables in CSV format.
A Notehub Project variable is available to the Notecard via env.get
after the
next sync.
Setting a Default Variable
Set Where | Set How | Available on Notecard |
---|---|---|
Notecard | env.default request | Immediately |
Set a default environment variable on a Device using an
env.default
request. Once set, this variable can be retrieved immediately with an
env.get
request,
assuming this value is not overridden by a local value, or a synced value from
the Notehub Device, Fleet or Project.
Notecard LoRa
When setting environment variables on the Notecard LoRa, the environment variables must be templated.
In addition, environment variables set with env.default
on Notecard LoRa do
not propagate up to Notehub. If looking for a simple way to share a variable
from a Notecard to Notehub, please refer to
var.set.
{
"req": "env.default",
"name": "temp-threshold",
"text": "100"
}
J *req = notecard.newRequest("env.default");
JAddStringToObject(req, "name", "temp-threshold");
JAddStringToObject(req, "text", "100");
notecard.sendRequest(req);
req = {"req": "env.default"}
req["name"] = "temp-threshold"
req["text"] = "100"
card.Transaction(req)
Setting Environment Variable Templates
Notefile templates provide the Notecard with a schema the Notecard uses to store Notes as fixed-length records, rather than as flexible JSON objects. Just like Notefiles, environment variables can also be templated, and are required when using the Notecard LoRa.
Set Where | Set How | Available on Notecard |
---|---|---|
Notecard | env.template request | Immediately |
The body
of an env.template
request specifies environment variable names,
with values as "hints" for the data type. See
Understanding Template Data Types
for more information.
{
"req": "env.template",
"body": {
"env_var_int": 11,
"env_var_string": "10",
}
}
J *req = NoteNewRequest("env.template");
J *body = JCreateObject();
JAddNumberToObject(body, "env_var_int", 11);
JAddStringToObject(body, "env_var_string", "10");
JAddItemToObject(req, "body", body);
NoteRequest(req);
req = {"req": "env.template"}
req["body"] = {
"env_var_int": 11,
"env_var_string": "10"
}
rsp = card.Transaction(req)
Batch Upload of Environment Variables
Notehub allows you to upload a CSV-formatted file with key-value pairs to create multiple environment variables. This is accomplished by navigating to the appropriate Project, Fleet, or Device environment variable settings page and uploading a CSV file.
The uploaded file must conform to the following requirements:
- The file is limited to 2KB in size.
- The key-value pairs are limited to 256 characters each.
- The file must be in a
.csv
file format. - The file must include two columns (first is
key
and the second isvalue
). Anything with more than two columns will be ignored. - If any of the entries in the
key
orvalue
columns contain commas, the entry needs to be in quotes. - If the
key
exists multiple times, the last one in the list will be used. - If the system cannot process the file for some reason, all values in the file will be rejected and an error will be displayed.
For example:
temp_threshold,30
pump_status,true
default_mode,"off"
valid_values,"30,60,90"
Session Environment Variables
You may have noticed that there is a Notefile called _session.qo
that doesn't
exist on the device, but for which a single event is generated at the start of
each session. It typically contains information about the device that is
considered to be too noisy or would require too much overhead to include in
each event.
Using a special naming convention, developers can create environment variables
that are included in the _session.qo
event payload at the start of each
session. To differentiate themselves from standard environment variables;
session environment variables begin with a $
prefix (e.g. $foo
). Session
environment variables can be created for a project, fleet, or device. Once
created, the _session.qo
event JSON will contain an object that looks like
this:
"environment": {
"$foo": "bar",
},
The primary distinction of a session environment variable is the ability to
provide a unique pathway for the device to share intermittent information back
to Notehub.io and on to your routes. Using session environment variables can
alleviate the burden of sharing information which is considered important, but
seldom changes during the life of a session. The session environment variables
appear in the _session.qo
event, which provides a way for a custom route to be
created. This is not possible using standard environment variables, and would
require non-trivial logic when using standard data queues.
In summary, at the beginning of each session, Notehub.io will receive a copy of
any environment variables beginning with a $
, and those variables allow the
device to report information to Notehub.io.
Deleting Environment Variables
In the Notehub UI, you can delete environment variables by clicking the trashcan icon next to a variable.
From the Notehub API, delete environment variables at the Notehub Device, Fleet, and Project scope using the following Notehub APIs:
- Delete Device Environment Variables
- Delete Fleet Environment Variables
- Delete Project Environment Variables
For Notecard local and default variables, use env.default
with the name
argument and no text
field to clear a variable.
{
"req": "env.default",
"name": "temp-threshold"
}
J *req = notecard.newRequest("env.default");
JAddStringToObject(req, "name", "temp-threshold");
notecard.sendRequest(req);
req = {"req": "env.default"}
req["name"] = "temp-threshold"
card.Transaction(req)
Reserved Environment Variables
Notehub reserves a set of system environment variables that can be used to
override default values on the Notecard. Note that all reserved environment
variable names start with an _
, so it is best to never start an environment
variable name with an underscore when creating user-defined variables.
Variable | Description | Data Type | Related API or Guide |
---|---|---|---|
_aux_en | Used to toggle the AUX_EN pin HIGH or LOW . | string | Using AUXEN, AUXRX, and AUXTX |
_aux_gpio_report | Set by the Notecard to reflect the current state of the AUX GPIO inputs. | string | Monitoring AUX GPIO State |
_aux_gpio_report_enable | Used to enable reporting via aux_gpio_report . | string | Monitoring AUX GPIO State |
_aux_gpio_set | Used to set AUX GPIOs HIGH or LOW , or pulse them HIGH or LOW , for a specified period. | string | card.aux GPIO Mode |
_comment | Used to get or set a device comment in Notehub. Must be set in Notehub device summary or with Notehub API. | string | Notehub Env Var by Device API |
_contact_affiliation | Affiliation of the Notecard maintainer (overrides "default" affiliation). | string | card.contact/affiliation |
_contact_affiliation_default | Affiliation of the Notecard maintainer. | string | card.contact/affiliation |
_contact_email | Email of the Notecard maintainer (overrides "default" email). | string | card.contact/email |
_contact_email_default | Email of the Notecard maintainer. | string | card.contact/email |
_contact_name | Name of the Notecard maintainer (overrides "default" name). | string | card.contact/name |
_contact_name_default | Name of the Notecard maintainer. | string | card.contact/name |
_contact_role | Role of the Notecard maintainer (overrides "default" role). | string | card.contact/role |
_contact_role_default | Role of the Notecard maintainer. | string | card.contact/role |
_dfu_enabled | Enable DFU downloads based on Notecard voltage. Use a boolean 1 (on) or 0 (off) for each source/voltage level: usb:<1/0>;high:<1/0>;normal:<1/0>;low:<1/0>;dead:0 . For example, with usb:1;0; , DFU downloads would occur when USB-powered, but not in any other state. | string | dfu.status/vvalue |
_dfu_period | A period within which to enable automatic Notecard Outboard Firmware Updates. The value provided must be in the form "0000000,00,0" , where a value of "1010000,20,5" allows firmware updates to occur on Sundays and Tuesdays, starting at 20:00 local time, for 5 hours. | string | N/A |
_fw | Name of a host firmware binary to send to the Notecard for delivery to a host. Must match the name of a binary that has already been uploaded to the Notehub project. | string | N/A |
_fw_download_always_enabled | Set to 1 to override host behavior and allow Notehub to force DFU to be enabled, preventing a host bug from permanently disabling DFU. | int | N/A |
_fw_retry | The UNIX epoch time, in seconds, when a host firmware update retry was requested. | int | N/A |
_gps_expiry_secs | Used for overriding the default 900 second GPS timeout. | int | N/A |
_gps_hdop_convergence_secs | Used for overriding of GPS HDOP convergence default of 15 seconds. | int | N/A |
_gps_hdop_minimum | Used for overriding the default GPS quality determination default of 5. | int | N/A |
_gps_mode | Specify the GPS module mode. | string | card.location.mode/mode |
_gps_ring_lat | When geofence is enabled, latitude in degrees of the geofence center. | float | card.location.mode/lat |
_gps_ring_lon | When geofence is enabled, longitude in degrees of the geofence center. | float | card.location.mode/lon |
_gps_ring_meters | Meters from a geofence center. Used to enable geofence location tracking. | int | card.location.mode/max |
_gps_ring_secs | Used for overriding for standard 5m debounce period of going in/out of geofenced area. | int | N/A |
_gps_secs | When in periodic mode, location will be sampled at this interval, if the Notecard detects motion. | int | card.location.mode/seconds |
_gps_track | Set to 1 to start Notefile tracking. | 1 or 0 | card.location.track/start |
_gps_track_heartbeat_hours | If heartbeat is enabled, add a heartbeat entry at this interval. | int | card.location.track/hours |
_lat | Used to override the Notecard's current GPS position latitude. | float | N/A |
_log | Used to enable log messages in the _log.qo Notefile for debugging. Available values: "gps" , "gpsmax" , "modem" , "all" . | string | N/A |
_lon | Used to override the Notecard's current GPS position longitude. | float | N/A |
_net_mins | Used for overriding the cellular network information update period of 7 days. | int | N/A |
_restart | If set or updated, the Notecard performs a controlled restart. Suggest setting to the current UNIX Epoch time when setting/updating. | string | card.restart |
_restart_host | If set or updated, the Notecard performs a controlled restart of a connected host. Suggest setting to the current UNIX Epoch time when setting/updating. | string | N/A |
_restart_host_every_hours | If set, the Notecard restarts a connected host at the configured interval. | int | N/A |
_restart_host_no_activity_hours | If set, the Notecard restarts a connected host after no activity has been detected for the provided number of hours. | int | N/A |
_restart_host_use_attn | If set, the Notecard will restart a connected host by pulsing its ATTN pin low. If not set, the Notecard will restart a connected host using the AUX3 and AUX4 pins. | boolean | N/A |
_restart_every_hours | If defined and non-zero, the Notecard will perform a controlled restart at the specified interval in hours. | int | card.restart |
_restart_no_activity_hours | If the Notecard fails to sync with Notehub after the number of hours set in this variable, restart the device. | int | card.restart |
_session_mins | When in continuous mode, the amount of time, in minutes, of each session. | int | hub.set/duration |
_sn | The end product's serial number (overrides "default" sn). | string | hub.set/sn |
_sn_default | The end product's serial number. | string | hub.set/sn |
_stale_gps_mins | The number of minutes after which a GPS/GNSS-determined location is considered stale by Notehub. Stale locations do not appear in the best_ fields in Notehub events. | int | Data Notehub Appends to Events |
_stale_tri_mins | The number of minutes after which a Wi-Fi or cell tower triangulated location is considered stale by Notehub. Stale locations do not appear in the best_ fields in Notehub events. | int | Data Notehub Appends to Events |
_stale_tower_mins | The number of minutes after which a cell tower location is considered stale by Notehub. Stale locations do not appear in the best_ fields in Notehub events. | int | Data Notehub Appends to Events |
_sync_continuous | Enables an always-on network connection, for high power devices. | 1 or 0 | hub.set/mode:continuous |
_sync_continuous_inbound | Automatically and immediately sync each time a Notefile change is detected on Notehub. | 1 or 0 | hub.set/mode:continuous sync:true |
_sync_inbound_mins | The max wait time, in minutes, to sync inbound data from Notehub. | int | hub.set/inbound |
_sync_outbound_mins | The max wait time, in minutes, to sync outbound data from the Notecard. | int | hub.set/outbound |
_tags | Used to get or set device tags in Notehub. Must be set in Notehub device summary or with Notehub API. | string | Notehub Env Var by Device API |
_tri | The triangulation approach to use for determining the Notecard location. | string | card.triangulate/mode |
_tri_always | Triangulate even if there was no motion detected. | 1 or 0 | card.triangulate/set:true on:true |
_tri_gps_failure | Triangulate as triggered by the failure to acquire a GPS signal successfully. | 1 or 0 | N/A |
_tri_mins | Minimum delay, in minutes, between triangulation attempts. Use 0 for no time-based suppression. | int | card.triangulate/minutes |
_usb_disable | Set to 1 to disable the Notecard's USB port, for security purposes. | 1 or 0 | card.io/mode |