Ok, I’ve written the scripts to get actual daily information about public and school holidays to consider this information in setting the open and close time for my pimatic shutter steerage. As an example, at public and school holidays the open times are delayed by 1,5 hour compared to normal open times. The scripts refer to public and school holidays in Germany, state NRW. For other countries or german states you need to reference other internet sides and to adjust the HTML text processing with awk and grep in the scripts.
The scripts require the following pimatic variables:
$publicHoliday - logical value, ‘true’ if actual day is a public holiday otherwise ‘false’
$today - string, kepping the date plus name of public holiday if true for the actual day
$schoolHoliday - logical value ‘true’ if actual day is a school holiday, otherwise ‘false’
$schoolHolidayInfo - string, keeping name of holiday periode plus start and end date
$sunposition - string, keeping time on sunrise and sunset
Step 1: Define required variables within pimatic
Stop pimatic, define the variables in config.json and start pimatic
pi@raspberrypi ~ $ sudo service pimatic stop
Edit your config.json and add the following variables in section “variables”:
{
"name": "publicHoliday",
"value": "true"
},
{
"name": "schoolHoliday",
"value": "true",
},
{
"name": "schoolHolidayInfo",
"value": "-"
},
{
"name": "sunposition",
"value": "-"
},
{
"name": "today",
"value": "-"
},
pi@raspberrypi ~ $ sudo service pimatic start
Step 2: Install and test the scripts
Login (ssh) to your raspberry and install (copy) the scripts 'phinfo.sh, ‘hdinfo.sh’ and ‘pimaticAPI.sh’ to pi’s home directory (samba, ftp, …)
Check if the scripts are executable, if not run chmod 744 *.sh).
Here is my directory listing:
pi@raspberrypi ~ $ ls -l
insgesamt 40
-rwxr–r-- 1 pi pi 2918 Jun 27 10:19 hdinfo.sh
-rwxr–r-- 1 pi pi 1741 Jul 4 09:24 phinfo.sh
-rwxr–r-- 1 pi pi 1684 Jul 4 09:18 pimaticAPI.sh
drwxr-xr-x 6 pi pi 4096 Jul 5 10:24 pimatic-app
-rw-r–r-- 1 pi pi 979 Mai 24 14:17 pimatic-init-d
drwxrwxr-x 2 pi pi 4096 Jan 27 09:34 python_games
drwxr-xr-x 2 pi pi 4096 Mai 24 13:22 tmp
Now run the sripts in test mode. The sripts will reference internet pages to retrieve the respective info’s (see scripts) and read from config.json ‘user:password’ for pimatic admin user, required to use the login mechanism of the pimatic API.
pi@raspberrypi ~ $ ./phinfo.sh 20151225
:true:Freitag, 25.12.2015 - 1. Weihnachtstag:
pimatic variable - publicHoliday
value - "true"
user:password - admin:lxyp612
url - http://raspberrypi/api/variables/publicHoliday
pimatic variable - today
value - "Freitag, 25.12.2015 - 1. Weihnachtstag"
user:password - admin:*******
url - http://raspberrypi/api/variables/today
Aufgang 05:23 Uhr, Untergang 21:38 Uhr
pimatic variable - sunposition
value - "Aufgang 05:23 Uhr, Untergang 21:38 Uhr"
user:password - admin:*******
url - http://raspberrypi/api/variables/sunposition
Your output should be similar (Germany, NRW).
Then, run the ‘hdinfo.sh’ script to get dates on holidays in Germany, NRW:
pi@raspberrypi ~ $ ./hdinfo.sh 20150705
05.Jul 2015
Weihnachtsferien 22.Dez - 06.Jan
Osterferien 30.Mär - 11.Apr
Pfingsten 26.Mai - 26.Mai
Sommerferien 29.Jun - 11.Aug
Herbstferien 05.Okt - 17.Okt
Weihnachtsferien 23.Dez - 06.Jan
1 193 186 1 6
2 193 186 89 101
3 193 186 146 146
4 193 186 180 223
5 193 186 278 290
6 193 186 357 365
true Sommerferien 29.Jun - 11.Aug
pimatic variable - schoolHoliday
value - "true"
user:password - admin:*******
url - http://raspberrypi/api/variables/schoolHoliday
pimatic variable - schoolHolidayInfo
value - "Sommerferien 29.Jun - 11.Aug"
user:password - admin:*******
url - http://raspberrypi/api/variables/schoolHolidayInfo
Login to pimatic GUI and check the values of the appropriate variables. The values should be identically to the values seen in the test output on shell level.
Step3: Ensure daily update of the variables
With the following rule the pimatic variables will be nightly updated
{
"id": "get-today",
"name": "Aktuelle Informationen aus dem Internet einlesen",
"rule": "if it is 00:05 then execute \"/home/pi/phinfo.sh\" and execute \"/home/pi/hdinfo.sh\"",
"active": true,
"logging": true
},
Step4: Display the information in your frontend and/or define rules using the variable values
I’m using the information in my pimatic dash board and more important to steer the shutters in my home, especially important to delay the open time during holidays and public holidays in the rooms of my children.
For displaying the values you need to define a respective ‘variable device’. Example:
{
"id": "dtoday",
"name": "Heute",
"class": "VariablesDevice",
"variables": [
{
"name": "Heute",
"type": "string",
"expression": "\"$today\"",
"label": "Heute"
}
]
},
Here is an example where the information is used in a rule definition:
{
"id": "set-shutter-opening-times-for-weekend",
"name": "Rolladen - Öffnungszeiten für Wochenende Sa-So und Feiertage setzen",
"rule": "if its sunrise and [ $weekend = \"true\" or $publicHoliday = \"true\" ] then set $OTshutterSRL to $SRLatWeekend and set $OTshutterSRX to $SRXatWeekend and set $OTshutterSRP1 to $SRP1atWeekend and set $OTshutterSRP2 to $SRP2atWeekend",
"active": true,
"logging": true
},
{
"id": "set-shutter-opening-times-for-workdays",
"name": "Rolladen - Öffnungszeiten für Werktage Mo-Fr und Feiertage setzen",
"rule": "if its 01:05 and [ $weekend = \"false\" and $publicHoliday = \"false\" ] then set $OTshutterSRL to $SRLatWorkday and $OTshutterSRX to $SRXatWorkday and $OTshutterSRP1 to $SRP1atWorkday and $OTshutterSRP2 to $SRP2atWorkday",
"active": true,
"logging": true
},
{
"id": "set-shutter-opening-times-for-holidays",
"name": "Rolladen - Öffnungszeiten der Kinderzimmer für Ferientage setzen",
"rule": "if its 01:15 and $schoolHoliday = \"true\" then set $OTshutterSRL to $SRLatWeekend and set $OTshutterSRX to $SRXatWeekend",
"active": true,
"logging": true
},
Hopefully I didn’t forget something important and everyone who is interested is able to get it running. As we have holidays I’ve only limited time to answer and support requests.
Remark: Pimatic variables ‘sunset’ and ‘sunrise’ are set by the plugin ‘sunrise’ and not by my script.
To be complete, here are the scripts:
#!/bin/bash
# pimaticAPI.sh
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
}
#!/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
#
# hdinfo.sh [yyyymmdd]
#
# post information about school holidays (NRW)to pimatic
# "yyyymmdd" # script return holiday information about requested day
#-----------------------------------------------------------------------------
. /home/pi/pimaticAPI.sh
PIMATIC_VAR1="schoolHoliday"
PIMATIC_VAR2="schoolHolidayInfo"
schoolHoliday="false"
schoolHolidayInfo="-"
#
# check and get runstring parameter
#
if [ "$1" ] # run script in test mode
then
echo $(date -d "$1" +"%d.%b %Y")
iday=$(date -d "$1" +%-j) # requested day in number format (e.q 237)
fcday=$(date -d "$1 +7 days" +%-j)
DEBUG="true"
else
iday=$(date +%-j) # current day in number format (e.q 237)
fcday=$(date -d "+7 days" +%-j)
fi
#
# retrieve information on school holidays from internet
#
year=$(date +%Y)
shdInfo=$(curl -s http://www.kalender-${year}.org/schulferien/nordrhein-westfalen-nrw/ | grep -A 1 '<div class="datum">' | awk 'BEGIN{RS="</div>";FS=">";} {print $2;}')
#echo $shdInfo
# record format: beginn - end holidayname (29.06.15 - 11.08.15 Sommerferien ...)
#
# store information on holidays in array variables
#
i=1
counter=0
for item in $shdInfo
do
#echo $item
counter=$(( counter+1 ))
if [ $counter -eq 1 ];
then
hdbeginn[$i]=$(echo $item | awk 'BEGIN{FS=".";} {print "20"$3$2$1;}')
fi
if [ $counter -eq 3 ]
then
hdend[$i]=$(echo $item | awk 'BEGIN{FS=".";} {print "20"$3$2$1;}')
fi
if [ $counter -eq 4 ]
then
hdname[$i]=$item
hdinfo[$i]="${hdname[$i]} $(date -d ${hdbeginn[$i]} +%d.%b) - $(date -d ${hdend[$i]} +%d.%b)"
ihdbeginn=$(date -d ${hdbeginn[$i]} +%-j) # numeric start
ihdend=$(date -d ${hdend[$i]} +%-j)
ryear=$(date -d ${hdbeginn[$i]} +%Y)
#echo $ihdbeginn $ihdend $ryear $year
if [ $ihdbeginn -gt $ihdend ] && [ $ryear -eq $year ]
then
hdend[$i]=$year"1231"
elif [ $ihdbeginn -gt $ihdend ] && [ $ryear -lt $year ]
then
hdbeginn[$i]=$year"0101"
fi
if [ $DEBUG ]; then echo "${hdinfo[$i]}"; fi
i=$(( i+1 ))
counter=0
fi
done
#
# check if current day is a holiday
#
counter=1
while [ $counter -lt $i ]
do
ihdbeginn=$(date -d ${hdbeginn[$counter]} +%-j) # numeric start
ihdend=$(date -d ${hdend[$counter]} +%-j) # numeric end
if [ $DEBUG ]; then echo $counter $fcday $iday $ihdbeginn $ihdend;fi
if ([ "$iday" -ge "$ihdbeginn" ] && [ "$iday" -le "$ihdend" ] )
then
schoolHoliday="true"
schoolHolidayInfo=${hdinfo[$counter]}
elif [ "$fcday" -ge "$ihdbeginn" ] && [ "$fcday" -le "$ihdend" ]
then
schoolHolidayInfo=${hdinfo[$counter]}
fi
counter=$(( counter+1 ))
done
if [ $DEBUG ]; then echo $schoolHoliday $schoolHolidayInfo;fi
#
# write to pimatic
#
pimatic_var_value="\"$schoolHoliday\""
write_to_pimatic $PIMATIC_VAR1 "$pimatic_var_value"
pimatic_var_value="\"$schoolHolidayInfo\""
write_to_pimatic $PIMATIC_VAR2 "$pimatic_var_value"
exit 0