Hi,
I'd like to use this nice piece of code to read the temperatures from my house and see if my setting are good (if my house is cooling down when I leave and warming up again before I come back.
I have more foreseen, but one step at a time.
Here's my issue: I did setup a script that "calls" all me devices every 15 minutes and fills a DB with the values. Every time I activate this I have about 50% of my devices loosing their config over a single day. Typically one value is missing: I have 2 open/close cycles and when I open the iOS app, I can only see 3 values instead of 4.
Did anyone experience this?
I was thinking about using only the "backup" command and then splitting the JSON inside my script, but I'm afraid I'll just invest a few hours to come to the same result :(
Hereby my script just in case someone would like to do something similar:
`#!/bin/bash
#read the devices from the DB
declare -A myDevices
deviceCount=0 # I have to keep track of it as my 2D array is simulated and Bash can't really loop them itself
declare -A myMeasures
while read device
do
devices+=("$device")
done < <(mysql -u CometBlue -pCometBluePass -D CometBlue --host localhost -BN -e "select id , '|', MAC, '|', PIN, '|', Name , '|', description from devices;")
#read the array of devices and split the lines to get the details into a bigger 2D table
for device in "${devices[@]}"
do
#echo "Device: " $device
((deviceCount++))
IFS='|' read -r -a array <<< "$device"
#trimming the whitespaces... should probably be done at "read" part rather than here :(
trimmed_ID=$(echo ${array[0]} | xargs)
myDevices[$trimmed_ID, MAC]=$(echo ${array[1]} | xargs)
myDevices[$trimmed_ID, PIN]=${array[2]}
myDevices[$trimmed_ID, Name]=${array[3]}
myDevices[$trimmed_ID, Description]=${array[4]}
done
#read the data from the CometBlues
for ((cptr=1; cptr<=$deviceCount; cptr++))
do
myMeasures[$cptr, MAC]=${myDevices[$cptr, MAC]}
echo ============================================================
echo $cptr - ${myDevices[$cptr, Description]} - ${myDevices[$cptr, MAC]}
echo ============================================================
counter=0
temperatures=""
until [ "$temperatures" ]
do
((counter++))
echo "Temperatures, try "$counter
temperatures=$(cometblue device -p ${myDevices[$cptr, PIN]} ${myDevices[$cptr, MAC]} get temperatures 2>/dev/null)
if [ $counter -ge 5 ]
then break
fi
done
#echo "temperatures " $temperatures
myMeasures[$cptr, t_current]=$(echo $temperatures | awk '{print ($3)}')
myMeasures[$cptr, t_manual_mode]=$(echo $temperatures | awk '{print ($9)}')
myMeasures[$cptr, t_target_low]=$(echo $temperatures | awk '{print ($14)}')
myMeasures[$cptr, t_target_high]=$(echo $temperatures | awk '{print ($19)}')
myMeasures[$cptr, t_offset]=$(echo $temperatures | awk '{print ($23)}')
myMeasures[$cptr, W_open_detection]=$(echo $temperatures | awk '{print ($28)}')
myMeasures[$cptr, W_open_minutes]=$(echo $temperatures | awk '{print ($32)}')
counter=0
until [ "${myMeasures[$cptr, Battery]}" ]
do
((counter++))
echo "Battery, try "$counter
myMeasures[$cptr, Battery]=$(cometblue device -p ${myDevices[$cptr, PIN]} ${myDevices[$cptr, MAC]} get battery 2>/dev/null)
if [ $counter -ge 5 ]
then break
fi
done
#remove the % sign
myMeasures[$cptr, Battery]=${myMeasures[$cptr, Battery]//%}
echo "Battery: "${myMeasures[$cptr, Battery]}"%"
echo "T_current: "${myMeasures[$cptr, t_current]}"°C"
echo "T_manual: "${myMeasures[$cptr, t_manual_mode]}"°C"
echo "T_target_low: "${myMeasures[$cptr, t_target_low]}"°C"
echo "T_target_high: "${myMeasures[$cptr, t_target_high]}"°C"
echo "T_offset: "${myMeasures[$cptr, t_offset]}"°C"
echo "W_open_detection: "${myMeasures[$cptr, W_open_detection]}
echo "W_open_miuntes: "${myMeasures[$cptr, W_open_minutes]}" minutes"
SQLInsert="insert into measures (device, t_current, t_manual_mode, t_target_low, t_target_high, t_offset, W_open_detection, W_open_minutes, Battery) values ((SELECT id from devices WHERE MAC='${myMeasures[$cptr, MAC]}'), '${myMeasures[$cptr, t_current]}', '${myMeasures[$cptr, t_manual_mode]}', '${myMeasures[$cptr, t_target_low]}', '${myMeasures[$cptr, t_target_high]}', '${myMeasures[$cptr, t_offset]}', '${myMeasures[$cptr, W_open_detection]}', '${myMeasures[$cptr, W_open_minutes]}', '${myMeasures[$cptr, Battery]}');"
mysql -u CometBlue -pCometBluePass -D CometBlue --host localhost -BN -e "$SQLInsert"
if [ $? -eq 0 ];then
echo -ne ""
else
echo "SQL insert failed: "$SQLInsert
fi
done
`
If someone wants the SQL creation script, just ask :)
BR
Benja
Hi,
I'd like to use this nice piece of code to read the temperatures from my house and see if my setting are good (if my house is cooling down when I leave and warming up again before I come back.
I have more foreseen, but one step at a time.
Here's my issue: I did setup a script that "calls" all me devices every 15 minutes and fills a DB with the values. Every time I activate this I have about 50% of my devices loosing their config over a single day. Typically one value is missing: I have 2 open/close cycles and when I open the iOS app, I can only see 3 values instead of 4.
Did anyone experience this?
I was thinking about using only the "backup" command and then splitting the JSON inside my script, but I'm afraid I'll just invest a few hours to come to the same result :(
Hereby my script just in case someone would like to do something similar:
`#!/bin/bash
#read the devices from the DB
declare -A myDevices
deviceCount=0 # I have to keep track of it as my 2D array is simulated and Bash can't really loop them itself
declare -A myMeasures
while read device
do
devices+=("$device")
done < <(mysql -u CometBlue -pCometBluePass -D CometBlue --host localhost -BN -e "select id , '|', MAC, '|', PIN, '|', Name , '|', description from devices;")
#read the array of devices and split the lines to get the details into a bigger 2D table
for device in "${devices[@]}"
do
#echo "Device: " $device
((deviceCount++))
IFS='|' read -r -a array <<< "$device"
#trimming the whitespaces... should probably be done at "read" part rather than here :(
trimmed_ID=$(echo ${array[0]} | xargs)
myDevices[$trimmed_ID, MAC]=$(echo ${array[1]} | xargs)
myDevices[$trimmed_ID, PIN]=${array[2]}
myDevices[$trimmed_ID, Name]=${array[3]}
myDevices[$trimmed_ID, Description]=${array[4]}
done
#read the data from the CometBlues$cptr - $ {myDevices[$cptr, Description]} - ${myDevices[$cptr, MAC]}
for ((cptr=1; cptr<=$deviceCount; cptr++))
do
myMeasures[$cptr, MAC]=${myDevices[$cptr, MAC]}
echo ============================================================
echo
echo ============================================================
done
`
If someone wants the SQL creation script, just ask :)
BR
Benja