pythonvolttron

volttron agent development call configure method onstart


I am experimenting with the cron scheduling feature in the volttron agent development to a run a volttron control agent at a specific time.

How to do I call the configure method when agent starts via the onstart method on my agent.py file?

@Core.receiver("onstart")
def onstart(self, sender, **kwargs):
    """
    This is method is called once the Agent has successfully connected to the platform.
    This is a good place to setup subscriptions if they are not dynamic or
    do any other startup activities that require a connection to the message bus.
    Called after any configurations methods that are called at startup.
    Usually not needed if using the configuration store.
    """

This is my configure method. I know this question is most likely blatantly obvious but what is config_name, action, contents that I am supposed to pass through in onstart to call the configure method?

def configure(self, config_name, action, contents):
    """
    Called after the Agent has connected to the message bus. If a configuration exists at startup
    this will be called before onstart.

    Is called every time the configuration in the store changes.
    """
    config = self.default_config.copy()
    config.update(contents)

    _log.debug("*** [Setter Agent INFO] *** - Configuring CRON to schedule Agent")


    try:
        cron_schedule = str(config["cron_schedule"])


    except ValueError as e:
        _log.error("ERROR PROCESSING CONFIGURATION: {}".format(e))
        return


    self.cron_schedule = cron_schedule
    self.core.schedule(cron(self.cron_schedule), self.raise_setpoints_up)
    _log.info(f'*** [Setter Agent INFO] *** -  raise_setpoints_up CRON schedule from onstart sucess!')

My config file is config:

{

  "cron_schedule": "50 13 * * *"

}

Solution

  • This function "should" be associated with the config store. Please see https://volttron.readthedocs.io/en/main/platform-features/config-store/agent-configuration-store.html for that.

    The config_name is the location in the config store that was modified. So when you update a configuration entry in the configstore via the command or through one of the "set" calls in the example I give below, this function will get called with the updated data.

    The relevant methods for dealing with configuration store though an agent are https://volttron.readthedocs.io/en/main/platform-features/config-store/agent-configuration-store.html#configuration-subsystem-agent-methods.

    # From your agent onstart method
    
    #self.vip.config.set( config_name, contents, trigger_callback=False ) 
    
    # Using True will have the configstore callback (call the configure function)
    self.vip.config.set('my_config_file_entry', {"an": "entry"}, trigger_callback=True)
    

    Of course there is nothing preventing you from using that function yourself in the onstart method, however that is not how it was designed.

    Another point to making sure this works is to make sure you have the subscription in the agent init function in order for the callback to be triggered correctly.