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.
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
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 |
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.
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.
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 Environment Variables by Device request to the Notehub API.
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 Environment Variables by Fleet request to the Notehub API.
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 Environment Variables request to the Notehub API.
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.
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, device, or even on
the device itself with env.set
. 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 and clicking "Save."
From the Notehub API, delete environment variables at the Notehub Device, Fleet, and Project scope using a Delete Environment Variable request on the Notehub API.
For Notecard local and default variables, use env.set
and env.default
with the name
argument and no text
field to clear a variable.
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_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 |
_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_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_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 | Contents of the log.qo Notefile. | 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 |
_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 |
_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 |