Setpoints from API/...
 

Setpoints from API/database - Best way / Is it possible?  

  RSS

sam66
(@sam66)
New Member
Joined: 1 month ago
Posts: 4
04/06/2019 9:33 am  

Is it possible to take setpoints (such as temperature) from an external website API (or database stored locally) so they could change on (for example) an hourly basis?

Ideally I would like a gradual change between values so I think using 'Methods' to set a start and end setpoint (obtained from the API or database), letting Mycodo modify the setpoint and regulate the environment over the time period.  Once the time period is up then a new end setpoint should be set (obtained from the API or database) and the process repeated.

Is this achievable using Mycodo?  If so what would be the best way to implement it?


Quote
Kyle Gabriel
(@kylegabriel)
Member Admin
Joined: 4 years ago
Posts: 52
04/06/2019 10:20 am  

The easiest way I see would be to create a Duration Trigger that will execute a python script every x seconds. That script will query your API and set the new setpoint. You could also use a conditional to evaluate Python code to determine when to activate/deactivate this Duration Trigger, if you only want it running for particular periods of time. Here's an example of the script:

import sys
sys.path.append('/home/pi/Mycodo')
from mycodo.mycodo_client import DaemonControl
control = DaemonControl()

def api_get_setpoint():
setpoint = 0 # code to get setpoint
return setpoint

new_setpoint = api_get_setpoint()
control.pid_set('0000-0000-0000-0000', 'setpoint', new_setpoint)
This post was modified 1 month ago 2 times by Kyle Gabriel

Mycodo Developer


ReplyQuote
Kyle Gabriel
(@kylegabriel)
Member Admin
Joined: 4 years ago
Posts: 52
04/06/2019 10:35 am  

You could also set up a Conditional that runs every x seconds and just drop that code into the Conditional Statement. That way you don't have to mess with extraneous files. You could also have the code that determines when to use the API within the code. For example, working from my base example above, you could use this as a Conditional Statement if you wanted to only have the PID on and using the API between 2PM and 5PM, using a fixed setpoint between 5PM and 7PM, and deactivated all other times. Here's what that conditional statement would look like:

import sys
import datetime
sys.path.append('/home/pi/Mycodo')
from mycodo.mycodo_client import DaemonControl
control = DaemonControl()

def api_new_setpoint():
"""Code to get setpoint"""
setpoint = 0
return setpoint

def in_between(now, start, end):
"""Determine if current time between two hours"""
if start <= end:
return start <= now < end
else:
return start <= now or now < end

if in_between(
datetime.datetime.now().time(),
datetime.time(14),
datetime.time(17)):
control.controller_activate('PID', '0000-0000-0000-0000') # Ensure PID is activated
control.pid_set('0000-0000-0000-0000', 'setpoint', api_new_setpoint()) # Set setpoint to API value
elif in_between(
datetime.datetime.now().time(),
datetime.time(17),
datetime.time(19)):
control.controller_activate('PID', '0000-0000-0000-0000') # Ensure PID is activated
control.pid_set('0000-0000-0000-0000', 'setpoint', 0) # Set setpoint to fixed value
else:
control.controller_deactivate('PID', '0000-0000-0000-0000') # Deactivate PID
This post was modified 1 month ago by Kyle Gabriel

Mycodo Developer


ReplyQuote
Kyle Gabriel
(@kylegabriel)
Member Admin
Joined: 4 years ago
Posts: 52
04/06/2019 10:39 am  

With the above code, you'll probably have your log fill with messages like "PID Controller is already activated", because it's not checking if the controller is already active when it sends the command to activate it. I could add a parameter to the function to suppress warning messages to clean up the log.

Mycodo Developer


ReplyQuote
sam66
(@sam66)
New Member
Joined: 1 month ago
Posts: 4
04/06/2019 12:35 pm  

Thanks for the super fast response on this and for the examples.

If you could add the warning suppression that would be great but no rush as I'm not sure when I will next have time to test it out..

This post was modified 1 month ago by Kyle Gabriel

ReplyQuote
Kyle Gabriel
(@kylegabriel)
Member Admin
Joined: 4 years ago
Posts: 52
04/06/2019 12:42 pm  

Alternatively, I could just add the ability to check the status of a controller. This would be of more use in the long run.

Mycodo Developer


ReplyQuote
sam66
(@sam66)
New Member
Joined: 1 month ago
Posts: 4
08/06/2019 12:46 pm  

Alternatively, I could just add the ability to check the status of a controller. This would be of more use in the long run.

Yes, that makes sense


ReplyQuote