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.