Hi,

i have some rules in my pimatic config for switching everything off when leaving home. So what about a short leave or grab some beer at the local store? when i return i have to setup all light, radio and so on.

I wrote a little dirty script to saves the state of simple switches and can restore them on demand (when i enter home).

Feel free to write an pimatic addon or something:

Parameter to this script are:

  • save
  • restore
  • status
#!/bin/bash

### INSTALL JQ 
# git clone https://github.com/stedolan/jq.git
# cd jq
# autoreconf -i
# ./configure --disable-maintainer-mode
# make
# sudo make install

saveFile="/tmp/pimatic-device.state"  # you have to change this in the script aswell
HOST="rasp"
username_password="USERNAME:PASSWORD"

# Define List of saved switches, just the device-id from config.json
devicelist=(
     'bett_licht'
     'esstisch_licht'
     'sofa_licht'
     'licht_ecke'
     'kapelle'
     'zocken'
     'cody'
     'party'
     'Kamin'
     'disco_licht'
     'disco_licht_zwei'
   )

# save the current state to textfile /tmp/pimatic-device.state
save_state() {
rm $saveFile;

# Iterate devices
for i in ${devicelist[@]}; do
      
      # write status to array
      DEVICESTATE=$(curl -s http://$HOST/api/device/$i/getState --user "$username_password" | jq -r '.result')
      
      # DEBUG Devicestatus ausgeben
      #echo "Status: " $i $DEVICESTATE
      
      # Devicestate in Datei schreiben
      echo $i$DEVICESTATE >> /tmp/pimatic-device.state
      

done

}

# Restore the saved state
restore_state() { 

for i in ${devicelist[@]}; do

STATUS=`cat /tmp/pimatic-device.state |grep $i |sed "s/^$i//"`

# anschalten
if [ "$STATUS" == "true" ]; then 
	echo "device $i on" # DEBUG
	curl -s http://$HOST/api/device/$i/turnOn --user "$username_password" > /dev/null
elif [ "$STATUS" == "false" ]; then
	echo "device $i off" # DEBUG
	curl -s http://$HOST/api/device/$i/turnOff --user "$username_password" > /dev/null
fi

STATUS=""
done
}

status() { 

for i in ${devicelist[@]}; do

STATUS=`cat /tmp/pimatic-device.state |grep $i |sed "s/^$i//"`

# 
if [ "$STATUS" == "true" ]; then 
	echo "device $i on" # DEBUG
elif [ "$STATUS" == "false" ]; then
	echo "device $i off" # DEBUG
fi
STATUS=""
done
}

case "$1" in
        save) 
        	save_state
            ;;
        restore) 
        	restore_state
			;;
		status) 
       		status
			;;
esac

exit 0