Grafana + InfluxDB live monitoring of a SmartThings Samsung Air Conditioner

I am in the process of working out how to log live data from a Smart Things Samsung Air Conditioner, store it into a time series DB ( InfluxDB ) and display all the data via Grafana all hosted on
a Raspberry PI 2 (1 GB RAM ) running Raspbian Linux with a 32GB microSD card for storage.

I have successfully done the following so far:

  • Setup Raspbian OS linux on the Raspberry PI
  • Obtain an Smart Things API key then connect to SmartThings API to list the device to monitor (get the DevID of my A/C unit)
  • Dump Status of the A/C smart device into a JSON file which is then parsed using the JSON parser JQ to pull out relevant fields to extract for storage
  • Setup an INFLUX DB time series database to store the live data
  • Setup a Grafana Server instance and add Influx DB as a data source
  • Started writing a BASH script that calls CURL and JQ to obtain and extract the data then push the data into Influx DB
  • Created a custom display panel to show live data of the AC - On/Off state, Mode, Fan Mode, Temperature and Humidity both graphed over time and latest current values
  • Setup a CRON job to fire off a call to the SmartThings API every 1 minute.

I plan to document it more thoroughly when its in a state thats more useful to share.

I have a fairly extensive experience with Grafana + Influx with live monitoring of computer systems at my work.

I am using BASH because its very simple to understand and does not require a full understanding of a more complex higher level language like python, GO etc.

Would anyone be interested in what I am doing?

Here is just a start of what I have done in the past 2 days.

2 Likes

Looks cool! I think all the Grafana charting of SmartTHings data i’ve seen in the past is using an older Groovy smartapp integration. So it would be cool if you put together a guide using the new API.

I have now added power demand in Watts.

I have not worked out how to record the accumulated watts for a given period as yet but for now, the live power demand is ok.

These are the arguments given to JQ extract the relevant values from the device stat JSON file

Here are some extracts from my simple BASH script, these are the key things to know to extract the relevant values in the A/C status JSON file

JQ is a JSON parser and these JQ variable assignments set the arguments to pass to JQ to pull out the values for Temperature, Humidity, A/C State (On/Off), A/C Mode, and A/C Fan mode.

SmartThings URL

ST_URL=https://api.smartthings.com/v1/devices

JQ Extract Temp from status file

JQ_TEMP=’.components.main.temperatureMeasurement.temperature.value’

JQ Extract Hum from status file

JQ_HUM=’.components.main.relativeHumidityMeasurement.humidity.value’

JQ AC State

JQ_STATE=’.components.main.switch.switch.value’

JQ AC Mode

JQ_MODE=’.components.main.airConditionerMode.airConditionerMode.value’

JQ AC FAN Mode

JQ_FAN_MODE=’.components.main.airConditionerFanMode.fanMode.value’

JQ AC Instantaneous power in Watts

JQ_POWER=’.components.main.powerConsumptionReport.powerConsumption.value.power’

To pull a value from the JSON status file retrieved from a device status API call:

First pull down a status file for the device with device ID set to the variable $AC_DEVID using your own API key set to the variable $API_KEY

Get A/C status in one request dumping the status JSON output into one file $STATUS_FILE

which we later query directly using JQ and not needing additional API calls

curl -s -k -X GET “${ST_URL}/${AC_DEVID}/status” -H ‘Content-Type: application/json’ -H “Authorization: Bearer ${API_KEY}” -o $STATUS_FILE

AC_DEVID is your device ID - you can find this out by calling the device ID API:
API_KEY is the key that is handed to you on request to get access to the SmartThings API.

Just call this API to list all your devices:

https://api.smartthings.com/v1/devices

Then once you have the output JSON file you can extract the relevant fields from the file, this also saves you making extra API calls to the cloud.

Here are the JQ calls with the relevant arguments to extract the values:

Get Temperature, Humdity, Status

temp=$(jq $JQ_TEMP $STATUS_FILE)
hum=$(jq $JQ_HUM $STATUS_FILE)
state=$(jq -r $JQ_STATE $STATUS_FILE)
mode=$(jq -r $JQ_MODE $STATUS_FILE)
fan_mode=$(jq -r $JQ_FAN_MODE $STATUS_FILE)
power=$(jq -r $JQ_POWER $STATUS_FILE)

jq -r removes the " " from string values leaving the contained raw string.

Once you have the raw values all you need to do is push them to INFLUX DB
I record each value as separate measurements

Eg.

$PUSH_INFLUX_CMD “ac_temp,state=$state value=$temp $epoch”
$PUSH_INFLUX_CMD “ac_hum,state=$state value=$hum $epoch”
$PUSH_INFLUX_CMD “ac_state value=”$state",state_val=$state_val $epoch"
$PUSH_INFLUX_CMD “ac_mode,state=$state value=”$mode",mode_val=$mode_val $epoch"
$PUSH_INFLUX_CMD “ac_fan_mode,state=$state value=”$fan_mode",fan_mode_val=$fan_mode_val $epoch"
$PUSH_INFLUX_CMD “ac_power,state=$state value=$power $epoch”

$epoch is the UNIX time stamp in nano seconds - this it the time the script ran to time stamp the data when it goes into Influx

$PUSH_INFLUX_CMD is a function call I wrote that just wraps up the arguments into a single CURL POST command to post the data to the INFLUX web URL to ingest the data.

Because Grafana doesn’t map textual values to different colours I have mapped state strings to specific values so that the OFF string (mapped to 0) can be rendered in light gray then ON (mapped to 1) renders in green etc.

So that is why some influx measurements like ‘ac_fan_mode’ have a following textual string ‘value=$fan_mode’ and pure numeric value ‘fan_mode_val=$fan_mode_val’

I will expand more in future posts on how to set this all up once I have the final panel in a form I like.