Using Methods in Conditionals

Hi all. I’m a big fan of Mycodo’s PID Controller function, but I’ve realized that it’s probably not the best option for some of the controllers I am trying to implement. I’d like to experiment with using simpler controllers (similar to the built-in Bang-Bang Hysteretic Controller) that can use setpoint tracking. However, I can’t find a way to apply the Method I created for setpoint tracking to anything other than a PID setpoint. Is there a robust way for me to do this with, say, a custom Conditional Controller?

(As a side note, I will also submit a feature request on Github asking for setpoint tracking for Bang-Bang Hysteretic Controllers)

2 Likes

You should be able to use this code to get the current setpoint from a method:

import datetime
from mycodo.utils.method import load_method_handler
from mycodo.utils.method import parse_db_time

method_id = "PLACE_METHOD_ID_HERE"

try:
    print(self.ended)
except:
    self.ended = False

if self.ended:
    self.logger.info("Method previously ended. Doing nothing.")
elif not self.get_custom_option("start_time"):
    self.method = load_method_handler(method_id, self.logger)
    self.method_start_time = datetime.datetime.now()
    self.logger.info("Method has not started, starting now: {} (UTC)".format(
        self.method_start_time))
    self.set_custom_option("start_time", self.method_start_time.isoformat())
    self.method_end_time = self.method.determine_end_time(
        self.method_start_time)
    self.logger.info("End time: {} (UTC)".format(self.method_end_time))
else:
    self.method = load_method_handler(method_id, self.logger)
    self.method_start_time = parse_db_time(
        self.get_custom_option("start_time"))
    self.logger.info(
        "Method has previously started. "
        "Resuming from a start time of {} (UTC)".format(
            self.method_start_time))
    self.method_end_time = self.method.determine_end_time(
        self.method_start_time)
    self.logger.info("End time: {} (UTC)".format(self.method_end_time))

now = datetime.datetime.now()
new_setpoint, ended = self.method.calculate_setpoint(
    now, self.method_start_time)
self.logger.info(
    "Method function returned: {}, {}".format(new_setpoint, ended))
if ended:
    self.logger.info(
        "Method has already ended. Deleting start_time from database.")
    self.delete_custom_option("start_time")
    self.ended = True
else:
    self.logger.info(
        "Method hasn't ended, new setpoint is {}".format(new_setpoint))
1 Like

Excellent! I’ll give that a try.

1 Like