Skip to content

trigger_watcher_funct

Pragmatismo edited this page Mar 13, 2023 · 4 revisions

trigger_watcher.py

This is run by cron on boot, it monitors logs in the Pigrow/logs folder.

**note: this is designed to work with logs created using the modular_sensor.py script, any log being monitored must fit the same time=datetime>value=num log format.

When it starts it performs these steps;

startup_set_relays.py

When trigger_watcher.py starts it runs startup_set_relays.py which checks that devices controlled by cron timed relays are set to the correct position, this is important for example to turn lamps on if the system reboots.

load_trigger_events

Loads the file /Pigrow/config/trigger_events.txt, this is a comma separated list of the trigger settings

log_name        - the name of the log to be read e.g. bmetemp.txt
value_label     - the label of the value, e.g. temp 
trigger_type    - the type of trigger
                     - above   = if the log's value is above the threshold 
                                 trigger value < log value
                     - below   = if the log's value is below the threshold 
                                 trigger value > log value
                     - window  = min:max, when value is inside the window  
                                 log val > min val AND log val < max val  
                     - frame   = min:max, when the value is outside the window
                                 not (log val > min val AND log val < max val) 
trigger_value   - the threshold value
condition_name  - a unique name for this trigger 
trig_direction  - set 'on' or 'off' in the trigger_conditions file, 
                  this is used to allow resetting the trigger when the
                  value moves to the other side of the threshold again.  
trig_cooldown   - the amount of time to freeze the trigger before allowing
                  it to be used again. 
cmd             - the system command to be run by the trigger, this can be a 
                  standard pigrow script or a custom .sh file which performs
                  several tasks such as switching a device and messaging the
                  user.
  • Note commas can be included in the cmd section of the line without causing problems.

observe the log files

First it finds all .txt files to monitor in Pigrow/logs folder, when a log changes it checks if that log is mentioned in the trigger_events.txt file, if so reads the last item of the log, extracts the required value and determines which side of the threshold the value is and then it checks the trigger_conditions.txt file which is a list of the current set position of triggers and the cooldown time if one is selected, when it's determined the trigger should be run it calls the associated command.

Conditions File

The trigger_conditions.txt file is used to remember the current state of switches, this is to stop it continually triggering every time it reads a value and allows for the trigger to be paused by other scripts or manual editing of the conditions file. It also includes the cooldown value, this is a timestamp date that represents the end of the cooldown period - the trigger will not switch until the current datetime is past the datetime set for cooldown, if the trigger is set to have a ten minuet cooldown when it's called and the trigger_conditions file is edited the cooldown recorded will be the current time plus ten min.

the trigger_conditions.txt file is cleared when trigger_watcher.py is started, this allows each trigger to be checked and scripts to be initialised.

observe the trigger file

Watches the file /Pigrow/config/trigger_events.txt and updates the trigger settings when it changes so that the script does not need to be restarted when trigger events are added or modified.

Clone this wiki locally