# Buggy code
def pid_activate(pid_id):
    if has_required_pid_values(pid_id):
        return redirect(url_for("page_routes.page_pid"))

    action = "{action} {controller}".format(
        action=gettext("Actuate"), controller=gettext("PID")
    )
    error = []

    # Check if associated sensor is activated
    pid = PID.query.filter(PID.id == pid_id).first()
    sensor = Sensor.query.filter(Sensor.id == pid.sensor_id).first()

    if not sensor.is_activated:
        error.append(
            gettext(
                "Cannot activate PID controller if the associated sensor "
                "controller is inactive"
            )
        )

    if (
        (pid.direction == "both" and not (pid.lower_relay_id and pid.raise_relay_id))
        or (pid.direction == "lower" and not pid.lower_relay_id)
        or (pid.direction == "raise" and not pid.raise_relay_id)
    ):
        error.append(
            gettext(
                "Cannot activate PID controller if raise and/or lower relay IDs "
                "are not selected"
            )
        )

    if not error:
        # Signal the duration method can run because it's been
        # properly initiated (non-power failure)
        mod_method = Method.query.filter(Method.id == pid.method_id).first()
        if mod_method and mod_method.method_type == "Duration":
            mod_method.start_time = "Ready"
            db.session.commit()

        time.sleep(1)
        controller_activate_deactivate("activate", "PID", pid_id)

    flash_success_errors(error, action, url_for("page_routes.page_pid"))


# Traceback (IOError)
2017-06-07 11:03:18,941 - mycodo.sensors.raspi - ERROR - RaspberryPiCPUTemp.get_measurement() method raised IOError: [Errno 24] Too many open files: '/sys/class/thermal/thermal_zone0/temp'
2017-06-07 11:03:20,138 - mycodo.pid_1 - ERROR - Run Error: (sqlite3.OperationalError) unable to open database file

========= Remote Traceback (1) =========
Traceback (most recent call last):
File "/var/www/mycodo/env/local/lib/python2.7/site-packages/rpyc/core/protocol.py", line 329, in _dispatch_request
File "/var/www/mycodo/env/local/lib/python2.7/site-packages/rpyc/core/protocol.py", line 583, in _handle_call
File "/var/www/mycodo/mycodo/mycodo_daemon.py", line 201, in exposed_relay_off
File "/var/www/mycodo/mycodo/mycodo_daemon.py", line 553, in relay_off
File "/var/www/mycodo/mycodo/controller_relay.py", line 348, in relay_on_off
self.check_conditionals(relay_id, duration)
File "/var/www/mycodo/mycodo/controller_relay.py", line 397, in check_conditionals
for each_conditional in conditionals.all():

# Fixed code
def pid_activate(pid_id):
    if has_required_pid_values(pid_id):
        return redirect(url_for("page_routes.page_pid"))

    action = "{action} {controller}".format(
        action=gettext("Actuate"), controller=gettext("PID")
    )
    error = []

    # Check if associated sensor is activated
    pid = PID.query.filter(PID.id == pid_id).first()

    sensor_unique_id = pid.measurement.split(",")[0]
    sensor = Sensor.query.filter(Sensor.unique_id == sensor_unique_id).first()

    if not sensor.is_activated:
        error.append(
            gettext(
                "Cannot activate PID controller if the associated sensor "
                "controller is inactive"
            )
        )

    if (
        (pid.direction == "both" and not (pid.lower_relay_id and pid.raise_relay_id))
        or (pid.direction == "lower" and not pid.lower_relay_id)
        or (pid.direction == "raise" and not pid.raise_relay_id)
    ):
        error.append(
            gettext(
                "Cannot activate PID controller if raise and/or lower relay IDs "
                "are not selected"
            )
        )

    if not error:
        # Signal the duration method can run because it's been
        # properly initiated (non-power failure)
        mod_method = Method.query.filter(Method.id == pid.method_id).first()
        if mod_method and mod_method.method_type == "Duration":
            mod_method.start_time = "Ready"
            db.session.commit()

        time.sleep(1)
        controller_activate_deactivate("activate", "PID", pid_id)

    flash_success_errors(error, action, url_for("page_routes.page_pid"))