I know there are a number of examples out there but I could not find one which worked for me.
I have primarily two bins collected every other week.
*Blue - Mixed Recycling
*Black - Landfill
There alternate each week but if there is a bank holiday (aka public holiday) then the date is shifted out by one. All these dates are pre-published by the local council.
The basis of this was with the help of @hannibal29’s post
Operation
- Every Monday night at 3am a cron rule kicks off a shell script
- There is a text file which contains the date and the colour of the bin for collection
- The shell script runs through a text file of dates and looks for the first date newer than today and returns the line
- A rule executes when the “run” channel changes the item to OFF
- The rule parses out the returned line which is the date and the colour.
- I downloaded 3 icons from flaticon - trash, trash-blue and trash-black (Same svg, just different colours)
7.The rule sets the value of TrashNextDate and TrashNextColour appropriately - In Habpanel I have two Dummy widgets which display the date and the icon with correct colour.
What you need:
things
Thing exec:command:trash "Trash" [ command="/opt/apps/trash/trash.sh", interval=0, autorun=false ]
*Note: Your file must be executable by openhab. A cheat is
chmod 777 “path to file”
chmod +x “path to file”
bash script
#Current date
datum=$(date +%s)
trash_file="/opt/apps/trash/trash.txt"
while read raw_line
do
datum_line=$(echo $raw_line| cut -c1-10)
datum_line_sec=$(date -d $datum_line +%s)
if [ $datum_line_sec -ge $datum ]
then
echo $raw_line
exit 0
fi
done < $trash_file
echo "Error"
exit 1;
dates file
2019-04-26|blue
2019-05-02|black
2019-05-10|blue
2019-05-16|black
2019-05-23|blue
2019-05-31|black
2019-06-06|blue
2019-06-13|black
2019-06-20|blue
2019-06-27|black
2019-07-04|blue
2019-07-11|black
2019-07-18|blue
2019-07-25|black
2019-08-01|blue
2019-08-08|black
2019-08-15|blue
2019-08-22|black
2019-08-29|blue
2019-09-05|black
2019-09-12|blue
2019-09-19|black
2019-09-26|blue
2019-10-03|black
2019-10-10|blue
2019-10-17|black
2019-10-24|blue
2019-10-31|black
2019-11-07|blue
2019-11-14|black
2019-11-21|blue
2019-11-28|black
2019-12-05|blue
2019-12-12|black
2019-12-19|blue
2019-12-26|black
2020-01-02|blue
items
// items for exec-binding
String TrashRaw "Script - trash [%s]" { channel="exec:command:trash:output" }
Switch TrashRaw_Run "Script - trash - Run [%s]" { channel="exec:command:trash:run" }
Number TrashRaw_Result "Script - trash - exit [%d]" { channel="exec:command:trash:exit" }
DateTime TrashNextDate "Next bin date [%1$td.%1$tm.%1$tY]"
String TrashNextColour "Next bin colour [%s]" <trash>
rules
rule "trash - parse output"
when
Item TrashRaw_Run changed to OFF
then
if (TrashRaw_Result.state != 0) {
logError("trash - parse output", "Last exit code from Trash Run was not zero. Something went wrong.")
return;
}
val String trashRes = TrashRaw.state.toString
if (!trashRes.contains("|")){
logError("trash - parse output", "Result, '" + trashRes + "' is not formatted correctly.")
return;
}
val trashDateString = trashRes.substring(0, 10)
val trashColour = trashRes.substring(11)
TrashNextDate.sendCommand(new DateTimeType(trashDateString + "T00:00:00"))
TrashNextColour.sendCommand(trashColour)
end
rule "trash - get next date"
when Time cron "0 0 3 ? * MON *"
then
logInfo("trash - get next date", "Triggering")
TrashRaw_Run.sendCommand(ON)
end
The final home screen on the wall-mounted tablets look like this:
Hopefully someone else can use this or at least get some ideas from it.