fs20_holidays.sh 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/bin/bash
  2. #
  3. # script to generate a random number of on/off events to simulate presence eg.
  4. # while on holidays. normally this script would be executed by an event like a
  5. # dawn-sensor (you wouldn't want light during the day...:-)
  6. #
  7. # Copyright STefan Mayer <stefan@clumsy.ch>
  8. ################## configuration ###########################
  9. #number of events (min - max)
  10. event_min=5
  11. event_max=20
  12. #maximum delay in minutes
  13. delay_max=240
  14. #minimum and maximum ontime in minutes
  15. ontime_min=5
  16. ontime_max=60
  17. #devices to consider
  18. declare -a devices='("dg.gang" "dg.wand" "dg.dusche" "dg.bad" "dg.reduit")'
  19. #output variant [oft|onoff]
  20. #oft: use one at with on-for-timer of system
  21. #onoff: use two at, one for on one for off
  22. variant="onoff"
  23. #command to execute
  24. #command_start="/opt/fhem/fhem.pl 7072 \""
  25. command_start="echo /opt/fhem/fhem.pl 7072 \""
  26. command_end="\""
  27. ##################### Shouldnt need any changes below here #####################
  28. # count number of devices
  29. count=0
  30. for i in ${devices[*]}
  31. do
  32. ((count++))
  33. done
  34. # echo $count
  35. # maximum random in bash: 32768
  36. random_max=32768
  37. #number of events
  38. event=$(($RANDOM * (($event_max - $event_min)) / $random_max +$event_min))
  39. #initialize command
  40. command=$command_start
  41. for ((i=0; i<$event; i++))
  42. do
  43. #calculate starttime
  44. starttime=$(($RANDOM * $delay_max / $random_max))
  45. hour=$(($starttime / 60))
  46. minute=$(($starttime % 60))
  47. second=$(($RANDOM * 60 / $random_max))
  48. #calculate ontime
  49. ontime=$(($RANDOM * (($ontime_max - $ontime_min)) / $random_max +$ontime_min))
  50. #choose device
  51. dev=$(($RANDOM * $count / $random_max))
  52. case $variant in
  53. oft)
  54. printf "event %02d: define at.random.%02d at +%02d:%02d:%02d set %s on-for-timer %d\n" $i $i $hour $minute $second ${devices[$dev]} $ontime
  55. command=`printf "$command define at.random.%02d at +%02d:%02d:%02d set %s on-for-timer %d;;" $i $hour $minute $second ${devices[$dev]} $ontime`
  56. ;;
  57. onoff)
  58. offtime=$(($starttime + $ontime))
  59. hour_off=$(($offtime / 60))
  60. minute_off=$(($offtime % 60))
  61. second_off=$(($RANDOM * 60 / $random_max))
  62. printf "event %02d/on : define at.random.on.%02d at +%02d:%02d:%02d set %s on\n" $i $i $hour $minute $second ${devices[$dev]}
  63. printf "event %02d/off: define at.random.off.%02d at +%02d:%02d:%02d set %s off\n" $i $i $hour_off $minute_off $second_off ${devices[$dev]}
  64. command=`printf "$command define at.random.on.%02d at +%02d:%02d:%02d set %s on;;" $i $hour $minute $second ${devices[$dev]}`
  65. command=`printf "$command define at.random.off.%02d at +%02d:%02d:%02d set %s off;;" $i $hour_off $minute_off $second_off ${devices[$dev]}`
  66. ;;
  67. *)
  68. echo "no variant specifieno variant specified!!"
  69. ;;
  70. esac
  71. done
  72. command="$command $command_end"
  73. #execute command
  74. eval "$command"