t-up-check script


#!/bin/bash
#    This is run as a cron job to see if the data file
#    has been written recently. If it has been
#    activity.txt written (as a record of the test).
#    If the data file wasn't written, then write
#    noactivity.dat so the "moving" automation
#    is triggered to send a phone message.
#
# #### This is the fill in area. ####
#     The number of days for the test
t_aget=1
#    The directory where the tested data file is located
t_ddir=/usr/share/hassio/homeassistant/hafiles/data/
#    The file name to ne tested
t_dfile=tomup.txt
#  #### end of fill in ####
#
#    Make sure we're in the data directory
cd $t_ddir
#    remove noactivity.dat if it exists
if [ -e noactivity.dat ] ; then
 rm -f noactivity.dat
fi
#    get the dates
#    real date
t_check=$(date +"%Y-%m-%d")
#    file to check
t_stat=$(stat $t_dfile | grep Modify | cut -d' ' -f2)
#    for the age test
###t_late=$(date +%Y-%m-%d)
#t_late=$(date +%d)
t_late=$(echo $t_check | cut -d'-' -f3)
#    trim to day number only to compare dates
t_ctoday=$(echo $t_stat | cut -d'-' -f3)
#t_ctwo=$(echo $t_late | cut -d'-' -f3)
#     compare for notify if
#t_cmath=$(($t_ctwo-$t_ctoday))
t_cmath=$(($t_late-$t_ctoday))
#
#    if all good write file with .txt extension and quit
#    note the filecould be empty 
echo "Checking" > activity.txt
if [ "$t_check" = "$t_stat" ] ; then
  echo "tested - no activity" >> activity.txt
  exit 0
#
#     if not written today do notify if more than two days ago
else
  if [ "$t_cmath" -gt "$t_aget" ] ; then
#     echo to file for automation action
   echo "Do the notify" > noactivity.dat
  fi
fi