Last active
December 26, 2019 15:12
-
-
Save thatguychrisw/67166fc018c3bb28a0925a9e826b8c0c to your computer and use it in GitHub Desktop.
Shell script to toggle a device using a smart switch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In IFTTT create two webhooks named using the pattern, "$DEVICE_on" and "$DEVICE_off" | |
DEVICE="desk_fan" | |
# This is where the state of the device is kept | |
DEVICE_STATE_FILE=~/.${DEVICE}.state | |
IFTTT_KEY="" | |
# Set the default state of the device to "off" if the status file does not exist | |
[ -s $DEVICE_STATE_FILE ] || echo "off" > $DEVICE_STATE_FILE | |
# Get the current state of the device | |
DEVICE_STATE=$(<$DEVICE_STATE_FILE) | |
# Toggle the state of the device | |
DEVICE_STATE=$([ "$DEVICE_STATE" = "on" ] && echo "off" || echo "on") | |
# Tell IFTTT to toggle the device | |
curl -s https://maker.ifttt.com/trigger/${DEVICE}_${DEVICE_STATE}/with/key/${IFTTT_KEY} > /dev/null | |
# Update the device state | |
echo "${DEVICE_STATE}" > $DEVICE_STATE_FILE | |
# Capitalizes the first letter of the state | |
echo "$(tr '[:lower:]' '[:upper:]' <<< ${DEVICE_STATE:0:1})${DEVICE_STATE:1}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment