From MrSponti
I’ve created some bash scripts to retrieve information about public and school holidays from the internet. The scripts writes information to Pimatic variables, so that I can consider this information in the open and close times of my shutter steerage.
#!/bin/bash
#
#...phinfo.sh [yyyymmdd]
#
#...post current date to pimatic, if true enhanced by name of public holiday
#
#... "yyyymmdd" # run script in test mode
#
#-----------------------------------------------------------------------------
. /home/pi/pimaticAPI.sh
PIMATIC_VAR1="publicHoliday"
PIMATIC_VAR2="today"
PIMATIC_VAR3="sunposition"
#
#... check and get runstring parameter
#
if [ "$1" ]
then
qday=$1
DEBUG="true"
else
qday="today"
fi
qdate=$(date -d "$qday" +"%d.%m.%Y")
public_holiday=$(curl -s http://www.ferienkalender.com/ferien_deutschland/Nordrhein-Westfalen/2015-ferien-nordrhein-westfalen.htm | grep "$qdate" | awk 'BEGIN{RS="</td></tr><tr";FS="><td align=left>";} {print $2,$3;}'| grep "$qdate" | awk 'BEGIN{FS="<b>";} {print $3,$2;}' | awk 'BEGIN{FS="</b></td ";} {print $2," - ",$1;}' )
if [ "$public_holiday" ]
then
phday="true"
else
phday="false"
public_holiday="$(date -d "$qday" +"%A"), $qdate"
fi
if [ "$DEBUG" = "true" ]; then echo ":$phday:$public_holiday:"; fi
pimatic_var_value="\"$phday\""
write_to_pimatic $PIMATIC_VAR1 "$pimatic_var_value"
pimatic_var_value="\"$public_holiday\""
write_to_pimatic $PIMATIC_VAR2 "$pimatic_var_value"
#
#. get sunrise and sunset time
#
sunrise=$(curl -s http://weather.yahooapis.com/forecastrss?w=642800 | grep astronomy | awk -F\" '{print $2}' | { date -f - +%R; })
sunset=$(curl -s http://weather.yahooapis.com/forecastrss?w=644719 | grep astronomy | awk -F\" '{print $4}'|{ date -f - +%R; })
sunposition="Aufgang $sunrise Uhr, Untergang $sunset Uhr"
if [ "$DEBUG" = "true" ]; then echo "$sunposition"; fi
pimatic_var_value="\"$sunposition\""
write_to_pimatic $PIMATIC_VAR3 "$pimatic_var_value"
exit 0
#!/bin/bash
export LANG=de_DE.UTF-8
export PIMATIC_CONFIG_FILE="/home/pi/pimatic-app/config.json"
export PIMATIC_HOST=$(hostname)
export PIMATIC_USER=admin
#------------------------------------------------------------------------------
#. write value trough pimatic variabale
#
function write_to_pimatic() {
pimatic_var=$1
pimatic_var_value=$2
pimatic_pwd=$(grep -B 1 -w "\"role\": \"admin\"" $PIMATIC_CONFIG_FILE | grep -w "password" | awk 'BEGIN{FS="\"";} {print $4;}')
#. DEBUG
#
if [ "$DEBUG" = "true" ]
then
echo "pimatic variable - ${pimatic_var}"
echo "value - ${pimatic_var_value}"
echo "user:password - ${PIMATIC_USER}:${pimatic_pwd}"
echo "url - http://$PIMATIC_HOST/api/variables/$pimatic_var"
fi
curl --insecure -X PATCH --header "Content-Type:application/json" --data '{"type": "value", "valueOrExpression": '"${pimatic_var_value}"'}' --user "${PIMATIC_USER}:${pimatic_pwd}" http://$PIMATIC_HOST/api/variables/$pimatic_var >/dev/null 2>&1
}
#------------------------------------------------------------------------------
#. post action request trough pimatic
#
function post_action_to_pimatic() {
pimatic_action=$1
pimatic_pwd=$(grep -B 1 -w "\"role\": \"admin\"" $PIMATIC_CONFIG_FILE | grep -w "password" | awk 'BEGIN{FS="\"";} {print $4;}')
#... DEBUG
#
if [ "$DEBUG" = "true" ]
then
echo "pimatic action - ${pimatic_action}"
echo "user:password - ${PIMATIC_USER}:${pimatic_pwd}"
echo "url - http://$PIMATIC_HOST/api/execute-action"
fi
curl --insecure -X POST --header "Content-Type:application/json" --data '{"actionString":'"${pimatic_action}"'}' --user "${PIMATIC_USER}:${pimatic_pwd}" http://$PIMATIC_HOST/api/execute-action >/dev/null 2>&1
}
- list item