[APP][Pro] Archive Insights - export all your Insights data to a NAS, FTP or web folder

For users who export this data to a Synology and want to use it.
I use this Bash script that i run as task on the Synology every hour.
Every hour i export all insights from everything from the last hour, and about 20 minutes later, i run the next bash script on the synology.

Here is the Bash script for putting details into CSV files for energie and wind and weather info.
All files are also relocated after extracting.

Data="/volume1/Homey/Insights/Data"
Root="/volume1/Homey/Insights/Exported"
Home="$(pwd)"
Purged=""

#Dir = "$(dirname "$0")"


cd "$Root" # "$(dirname "$0")"

subdircount=$(find $Root -maxdepth 1 -type d | wc -l)

if [[ "$subdircount" -eq 1 ]]
then
	echo "!!!Bestanden niet gevonden. Controleer Homey Insights!!!"
    exit -1
fi

echo "Looping through directories"
# cd $Dir 
for folder in *; do
	echo "FOUND DIR $folder"
	cd "$Root"		
    if [ -d "$folder" ]; then #if directory
        # $f is a directory		
		echo "going into $folder"
		YEAR=${folder:0:4}
		# echo "$YEAR"
		cd "$folder"
		for file in *.zip; do #if file
			if [ -f "$file" ]; then
				# $f is a directory		
				echo "================================================================================================"
				echo "found $file"
				7z x -y "${file}"
				if [ $? -eq 0 ]; then
					echo "Successfully unpacked; purging rar files"
					#rm -f "${file}"
				else
					echo "Unable to unpack;"
				fi
			fi
		done
		for deviceFolder in *; do
    		if [ -d "$deviceFolder" ]; then
				echo "found $deviceFolder"

				# Energiemeter
					find "$deviceFolder" -name 'meter_power.csv' -type f |sort |  while read File; do
						meterFolder="$Data/Energiemeter/$YEAR/$deviceFolder/"
						mkdir -p "$meterFolder"
						>> "$meterFolder/meter_power.csv"
						sed -i '1d' "$File"
						cat "$File" >> "$meterFolder/meter_power.csv"
						#rm -f "$File"

						# sed 1d "$File" | while read d; do
						# 	echo $d
						# done
						# 	Dir="$(dirname "$File")"
						# echo "" #$File"
				 	done


				# Weer

					find "$deviceFolder" -name 'measure_gust_angle.csv' -type f |sort |  while read File; do
						meterFolder="$Data/Weer/$YEAR/$deviceFolder/"
						mkdir -p "$meterFolder"
						>> "$meterFolder/measure_gust_angle.csv"
						sed -i '1d' "$File"
						cat "$File" >> "$meterFolder/measure_gust_angle.csv"
					done

					find "$deviceFolder" -name 'measure_gust_strength.csv' -type f |sort |  while read File; do
						meterFolder="$Data/Weer/$YEAR/$deviceFolder/"
						mkdir -p "$meterFolder"
						>> "$meterFolder/measure_gust_strength.csv"
						sed -i '1d' "$File"
						cat "$File" >> "$meterFolder/measure_gust_strength.csv"
					done
					
					find "$deviceFolder" -name 'measure_wind_angle.csv' -type f |sort |  while read File; do
						meterFolder="$Data/Weer/$YEAR/$deviceFolder/"
						mkdir -p "$meterFolder"
						>> "$meterFolder/measure_wind_angle.csv"
						sed -i '1d' "$File"
						cat "$File" >> "$meterFolder/measure_wind_angle.csv"
					done

					find "$deviceFolder" -name 'measure_wind_strength.csv' -type f |sort |  while read File; do
						meterFolder="$Data/Weer/$YEAR/$deviceFolder/"
						mkdir -p "$meterFolder"
						>> "$meterFolder/measure_wind_strength.csv"
						sed -i '1d' "$File"
						cat "$File" >> "$meterFolder/measure_wind_strength.csv"
					done
				
				# Temperatuur

					find "$deviceFolder" -name 'measure_temperature.csv' -type f |sort |  while read File; do
						meterFolder="$Data/Temperatuur/$YEAR/$deviceFolder/"
						mkdir -p "$meterFolder"
						>> "$meterFolder/measure_temperature.csv"
						sed -i '1d' "$File"
						cat "$File" >> "$meterFolder/measure_temperature.csv"
					done

					find "$deviceFolder" -name 'target_temperature.csv' -type f |sort |  while read File; do
						meterFolder="$Data/Temperatuur/$YEAR/$deviceFolder/"
						mkdir -p "$meterFolder"
						>> "$meterFolder/target_temperature.csv"
						sed -i '1d' "$File"
						cat "$File" >> "$meterFolder/target_temperature.csv"
					done


				rm -r "$deviceFolder"
				# #cd "$deviceFolder"
				# for file in $(dirname $Root)*; do #if file
				# 	echo  "asdasdsa$file"
				# 	if [ -f "$file" ]; then
				# 		# $f is a directory		
				# 		echo "================================================================================================"
				# 	fi
				# done
			fi
		done



		movedfilecount=$(find $Data/Alle/$folder -maxdepth 1 -type f | wc -l)

		if [[ "$movedfilecount" -gt 0 ]]
		then
			echo "!!!Bestanden al verplaatst!!!"
			rm -r "$Root/$folder/"
		else
			mv -f "$Root/$folder/" "$Data/Alle"
		fi

		





		#COPY folder to DATA!


		# find ${Root} '.zip' |sort | while read File; do
		# 	Dir="$(dirname "$File")"
		# 	# if [ "${Prevdir}" != "${Dir}" ]; then
		# 		cd "${Dir}"
		# 		echo "${File}"
		# 		echo "================================================================================================"
		# 		# Process the first rar file in this directory thanks to |sort
		# 		7z x -y "${File}"
		# 		if [ $? -eq 0 ]; then
		# 			echo "Successfully unpacked; purging rar files"
		# 			# rm -f "${File}"
		# 		else
		# 			echo "Unable to unpack;"
		# 		fi
		# 	# fi
		# 	# Prevdir=$Dir
		# done
		
		# cd $folder
		# for folder in "$(dirname "$0")"/*; do
		# 	if [ -d "$folder" ]; then
		# 		# $f is a directory		
		# 		echo "going into $folder"
		# 		cd $folder
				
		# 	fi
		# done

    fi
done


exit

# zip
echo "Looking for zip files to unpack..."
# Prevdir=""
find ${Root} '*/*.zip' |sort | while read File; do
	Dir="$(dirname "$File")"
	# if [ "${Prevdir}" != "${Dir}" ]; then
		cd "${Dir}"
		echo ""
		echo "= $(pwd)"
		echo "${File}"
		echo "================================================================================================"
		# Process the first rar file in this directory thanks to |sort
		7z x "${File}"
		if [ $? -eq 0 ]; then
			echo "Successfully unpacked; purging rar files"
			rm -f "${File}"
			# rm -f *.r[0-9][0-9]
			# rm -f *.s[0-9][0-9]
			# rm -f *.t[0-9][0-9]
		else
			echo "Unable to unpack; purging entire directory"
			# Purged="${Purged}${Dir}\n"
			# cd ..
			# rm -rf "${Dir}"
		fi
	# fi
	# Prevdir=$Dir
done
cd "${Home}"