Data API

It is possible to connect to other data sources that are not yet directly supported by using the Application Programming Interface (API). The data API provides a way to send data to ConstantGraph using a set of URL calls combined with an API key which is entered as part of the request. By this mechanism any third party data source can be connected via a small amount of code.

API calls are accessed using a POST method to the base URL using a JSON encoded request body and returning a JSON encoded response. The base URL is:

https://api.constantgraph.com/

Note that the old base url https://data.mongodb-api.com/app/constantgraph-iwfeg/endpoint/http/ is now deprecated and will be switched off in August 2025. Any applications that use the old base url must be converted to use the new one above.


Authentication

ConstantGraph uses API keys to autheticate requests. You can create and view API keys in the API Keys and Access Tokens section of the account page. The API key is applied to the request using the X-Api-Key header.

curl https://api.constantgraph.com/data/data
	-H X-Api-Key:0123456789ABCDEF 
    -H 'Content-Type: application/json' 
    -d '{"app":"my app name", "version":"1.2.3", "channels": [
            {"id":888, "v":100, "Name":"channel name" },{"id":36,"v":17.4}]}'

If a bad url endpoint is called then a NotFound error code will be returned

{"error_code":"NotFound", "error":"Function not found"}

If the API key is incorrect the response will contain an error code of InvalidSession:

{"error_code": "InvalidSession", "error": "user not found" }

If the request body is incorrectly formed, the response will contain an error code of FunctionExecutionError:

{"error_code": "FunctionExecutionError","error":"{"message": "invalid character 'u' looking for beginning of value"}", "link": "..."}

Depending on your susbscription level, the requests may be limited with an error code of RateLimit. Basic subscriptions are limited to 100 channel updates per 10 minutes, enhanced subscriptions are limited to 200 channel updates per 10 minutes and premium subscriptions allow many times more than this.

{"error_code":"RateLimit","error": "too many requests for this subscription", "lastRequest": time of last request, "product": product code}

Every request must contain an app name and version; if they are missing an error code of BadRequest is returned

{"error_code": "BadRequest", "error": "App name or version missing"}

If there is no data to process in the request then this also returns a BadRequest error code

{"error_code":"BadRequest", "error":"No channels in request - nothing to processes"}

Note that non-numeric values can be sent through the data and timedata calls as long as the data type of the channel it is being sent to accepts non-numeric data. For example sending the value "off" to a channel set as an on/off switch will work, custom data types can be created to handle other values.


Send current data

The data command is available for all subscriptions. It sends data which will be logged at the current time. The id is the channel number that uniquely identifies the channel and is mandatory, v is the numeric value being sent. Any number of channels (up to the maximum in your subscription) can be specified. The channel name and device number are optional but will show in the Graphs configuration page if they are supplied. Once supplied they will remain set until they are changed.

curl https://api.constantgraph.com/data/data
	-H X-Api-Key:0123456789ABCDEF 
    -H 'Content-Type: application/json' 
    -d {"app":"my app name", "version":"my app version",
	    "channels": [{"id":id, "v":value, "Name":"channel name"},{"id":36,"v":17.4}]}

If successful, the following result will be returned:

{"status":"success","user":internal user id,"inserted":number of data points inserted,"ignored":number of data points ignored,"message":"helpful information"}

Data points may be ignored if they are non-numeric or they have been filtered out using the basic filtering function. If the channel id had not been used before, it will be created automatically.


Send timestamp data

The timedata call is only available on the Premium subscription and takes the following form:

curl https://api.constantgraph.com/data/timedata
	-H X-Api-Key:0123456789ABCDEF 
    -H 'Content-Type: application/json' 
    -d {"app":"my app name", "version":"my app version",
	    "channels": [{"id":id, "values":[{"t":timestamp, "v":value},{"t":1605711317,"v":195}]}]}

The timestamp is the time of the data as a Unix epoch in seconds and value is the numeric value being sent. By this mechanism historic data may be added to the system.

Attempting to use the timedata call without a Premium subscription will return:

{"error":"timedata requests are not allowed on this subscription", "error_code":"AccessDenied", "product":product code}

Data points may be ignored if they are non-numeric or they have been filtered out as per the filtering options provided in the config call. Successful calls will result in:

{"status":"success", "user":internal user id, "inserted":number of data points inserted, "ignored":number of data points ignored, "message":"helpful information"}

Variable, graph and device configuration

The config call takes three main sections: Variables, Graphs, and Devices with each being optional. Variables describe the channels used to record data and Graphs decribes the graphs shown on the dashboard and Devices describe the devices the channels belong to.

Attempting to use the config call without the correct subscription will return:

{"error":"config requests are not allowed on this subscription", "error_code":"AccessDenied", "product": product code}

Variables

The Variables section relates to channels that you can graph and the channel configuration.

curl https://api.constantgraph.com/data/config
	-H X-Api-Key:0123456789ABCDEF 
    -H 'Content-Type: application/json' 
    -d {"Variables": [{"Service": "urn:upnp-org:serviceId:Dimming1", "Units":"0", "FilterMaximum":1000, "FilterEnable":0, "Id":22, "FilterMinimum":0, "Device":3, "Name":"Rope Light - LoadLevelStatus", "Logging":1, "Variable":"LoadLevelStatus"}]}
ItemDescription
Idthe unique channel number for the data
Namethe display name for the channel
Unitsthe units of the channel
DataTypethe type of data for the channel from the data type table below
FilterEnabletrue if filtering of channel data is enabled
FilterMaximumabsolute maximum value for filtering
FilterMinimumabsolute minimum value for filtering. Set to zero to ignore negative values.
FilterRateMaxmaximum rate of change for filtering (number of units per second)
FilterRateMinminimum rate of change for filtering (number of units per second). Set to zero to ignore decreasing values.
Devicedevice number id shown in Graph config
Servicestring data about the device
Variablestring data about the device
Loggingtrue if logging is enabled.

Here are the data type ids that can be used. Note that data types refer to the individual items that are being monitored such as temperature or whether something is on or off.

Type idData type description
0node (none or other)
1door lock
2brightness
3energy meter (KWh)
4energy (Watts)
5HVAC mode status
6battery charge
7humidity
8security sensor
9dimming bulb
10on/off switch
11temperature
12high setpoint
13low setpoint
14sensor armed
15generic sensor level
16HVAC operating state
103Siren on/off
109Window Covering
111TVOC
112Voltage
113Contact Sensor (on/off)
114Presence Sensor (on/off)
115Button (on/off)

Graphs

curl https://api.constantgraph.com/data/config
	-H X-Api-Key:0123456789ABCDEF 
    -H 'Content-Type: application/json' 
    -d {"Graphs":[{"Channels": [{"AggregateBy":4, "AggregationType":6, "yAxis": 1, "Id":22 } ],"Name": "Heating Hours per Week","Night": 0,"Reference": "weeklyheating","Period": 31536000}]}

The Graphs section is used to describe a graph can can be displayed in ConstanGraph or embedded in a separate application. It can include the following items:

ItemDescription
Namea descriptive name for the graph
Nightwhether to display night time bands on the graph (0 = off, 1 = on)
ReferenceA short name that is used as a unique reference for the graph
Periodthe time period of the graph in seconds
Channelsan array of channel data that will be included in the graph as described below:

The channels section includes details of each data channel that is included in the graph

ItemDescription
Idthe channel Id of the channel to be graphed
AggregateBynumber id of the aggregation period from the following list (0 - 5): Auto, None (Raw Data), Hour, Day, Week, Month.
AggregationTypenumber id of the aggregation function from the following list (0 - 8): Sample, Change (Max - Min), Sum, Weighted Average, Minimum, Maximum, On Duration, On Count, Sample Count
Typenumber id of the graph type from the following list (0 - 5): Spline, Line, Scatter, Area, Column, AreaRange
yAxisthe yAxis of the channel
DataMultipliernumber to multiply the data by, if scaling is required (defaults to 1)
DataOffsetnumber to add to the data, if scaling is required (defaults to 0)

Devices

curl https://api.constantgraph.com/data/config
	-H X-Api-Key:0123456789ABCDEF 
    -H 'Content-Type: application/json' 
    -d {"Devices":[{"Name": "Kitchen Light", "Number": 3, "Parent": 1, "Room": "Kitchen", "Type": 2}]}
ItemDescription
Namethe display name of the device.
Numberthe number id of the Device which matches the number id given in the Variables section
Parentthe number id of any parent device
Roomthe name of the room the the device is in
Typethe type id of the device from the list below

Here are the device data type ids that can be used. Note that devices typically refer to physical devices such as a light switch. A single device can have many channels and each channel can have its own data type.

Type idDevice type description
666Virtual Plug-in Device
1Interface
2Dimmable Light
3Switch
4Security Sensor
5HVAC
6Camera
7Door Lock
8Window Covering
9Remote Control
10IR Transmitter
11Generic IO
12Generic Sensor
13Serial Port
14Scene Controller
15AV
16Humidity Sensor
17Temperature Sensor
18Light Sensor
19Z-wave
20Insteon
21Power Meter
22Alarm Panel
23Alarm Partition
24Siren
25Weather
26Phillips Controller
27Appliance
28UV Sensor
29Mouse Trap
30Doorbell
31Keypad
32Garage Door
33Flow Meter
34Voltage Sensor
35Contact Sensor
36Mobile Prescence
37Button

Read aggregated graph data

To read data from the API you need to create a read only key from the account page. The graph data GET request returns the aggregated data that is used to create the graphs.

curl https://api.constantgraph.com/graph/graphdata?channel=129&node=1&start=2024-10-15T00:00&stop=2023-10-16T23:59&aggType=1&aggBy=3&compare=1
    -H X-Api-Key:0123456789ABCDEF

If successful, a result similar to the following will be returned:

{
    "time": "2024-10-15T00:00:00.000Z",
    "value": 15.3,
    "min": 12307.011,
    "max": 12322.312,
    "energyRate": 0.310,
    "pLow": 15.927,
    "pMid": 17.818,
    "pHigh": 21.212
},{
    "time": "2024-10-16T00:00:00.000Z",
    "value": 14.871,
    "min": 12322.312,
    "max": 12337.184,
    "energyRate": 0.306,
    "pLow": 14.024,
    "pMid": 14.957,
    "pHigh": 16.376
}

The parameters that can be passed to the graphdata request are summarised below:

parameterDescription
channelthe channel number of the data channel to be displayed, as can be found of the Data > Channel Config menu
nodethe number id the location the channel belongs to as seen on the list of locations in the account page
startthe start date and time of the graph data in ISO 8601 format e.g. YYYY-MM-DDT00:00
stopthe stop date and time of the graph data in ISO 8601 format e.g. YYYY-MM-DDT23:59
aggTypethe aggregation type of the data as described in the table below
aggBythe aggregation period of the data as described in the table below
compare0 = 1 comparison and 1 = compare with historical norms for this channel

The aggregation type refers to how the data is summarised over the chosen time period

aggTypeAggregation Type
0Sample - the value of the first data point
1Difference - the difference between data points
2Sum - the sum of data points
3Average - the weighted mean average
4Minimum - the minimum value
5Maximum - the maximum value
6Time On - the number of hours the value remained at 1 or above
7On Count - the number of times the value switched from zero to non-zero
8Sample Count - the number of samples taken

The aggregation period refers to the length of time over which the data is summarised

aggByAggregation Period
0Auto - period chosen based on the date range
1None - the data is not aggregated and the raw data is returned
2Hour - aggregated by hour
3Day - aggregated by day where the day starts according to the timezone configured in the admin settings
4Week - calendar week starting on Monday
5Month - calendar month
6Not used
7Not used
87 days - last 7 days - not used by Time Series data
930 days - last 30 days - not used by Time Series data