Skip to content

Instantly share code, notes, and snippets.

@jeffstieler
Created May 1, 2022 16:19
Show Gist options
  • Select an option

  • Save jeffstieler/fb20bcef982b497a20e7edad7d6a1712 to your computer and use it in GitHub Desktop.

Select an option

Save jeffstieler/fb20bcef982b497a20e7edad7d6a1712 to your computer and use it in GitHub Desktop.
Control smart plug based on Jetpack's battery level
#!/bin/bash
date
mifi_status_url="http://192.168.5.1/srv/status"
mifi_charge_threshold=15
mifi_full_charge=100
plug_name="Jetpack-Plug"
echo "Finding plug IP address..."
plug_ip=$( tplink-smarthome-api search --broadcast 192.168.0.255 | grep "$plug_name" | grep -oE '((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])' )
echo "Getting Jetpack status..."
mifi_status=$( curl -sS $mifi_status_url )
mifi_is_charging=$( echo $mifi_status | jq '.statusData.statusBarBatteryChargingSource | test("ChargingSourceAC")' )
mifi_battery_percent=$( echo $mifi_status | jq '.statusData.statusBarBatteryPercent | tonumber' )
echo "Charging: $mifi_is_charging"
echo "Battery: $mifi_battery_percent%"
if [[ $mifi_is_charging == "true" && $mifi_battery_percent -ge $mifi_full_charge ]]; then
echo "Jetpack is fully charged. Turning off plug."
tplink-smarthome-api setPowerState $plug_ip 0
fi
if [[ $mifi_is_charging == "false" && $mifi_battery_percent -le $mifi_charge_threshold ]]; then
echo "Jetpack is at $mifi_battery_percent%, threshold is $mifi_charge_threshold%. Turning on plug."
tplink-smarthome-api setPowerState $plug_ip 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment