On/Off Control Strategy Execution with IoT System Models
Introduction
A control strategy is a systematic approach used to manage a system's behaviour—typically through actuators—to ensure it meets performance goals, even under complex or variable conditions [1]. One of the most basic yet effective forms of control is the on/off control strategy. This involves activating or deactivating a system or device based on the difference between a monitored variable (known as the process value) and a defined setpoint.
For instance, imagine maintaining a comfortable room temperature: if the desired temperature is 21°C and the measured temperature rises to 28°C, an air conditioner (AC) is activated. When the temperature drops below 21°C, the AC is turned off. This simple rule-based control is particularly useful when frequent switching of devices has minimal cost or wear impact.
In modern IoT-based systems, these strategies are commonly implemented via RESTful APIs. However, relying on device-specific APIs can hinder portability and reusability. Each platform may expose different interfaces, creating significant challenges in adapting or scaling solutions across ecosystems.
This blog post demonstrates how to implement an on/off control strategy independent of vendor APIs, using object-oriented models enabled by the Metamodel for Energy Things (MetamEnTh). This approach promotes interoperability, reusability, and scalability—key goals in smart building automation.
Metamodel for Energy Things (MetamEnTh)
MetamEnTh is an object-oriented metamodel designed to describe mechanical, electrical, and plumbing (MEP) systems, particularly in smart buildings. It offers:
- Structured representations of devices like sensors, actuators, and appliances
- Definitions of spaces (e.g., rooms, floors) and their interactions with devices
- Interfaces for defining and executing control strategies that are decoupled from physical devices or APIs
With MetamEnTh, engineers and researchers can model IoT systems in a portable, extensible, and platform-agnostic manner. This makes it ideal for implementing automation strategies like on/off control in a consistent and scalable way.
Example Scenario: Room Temperature Control
To illustrate this, consider a room equipped with:
- A temperature sensor
- An air conditioner
- A controller that applies an on/off control logic
Here’s the basic control rule:
- If temperature > 21°C → Turn ON the air conditioner
- If temperature ≤ 21°C → Turn OFF the air conditioner
We begin by modelling the relevant components using MetamEnTh.
# Imports for room, sensor, appliance, etc.
from metamenth.misc import MeasureFactory
from metamenth.structure.room import Room
from metamenth.datatypes.measure import Measure
from metamenth.enumerations import (
RoomType, RecordingType, MeasurementUnit,
ApplianceCategory, ApplianceType,
SensorMeasure, SensorMeasureType, SensorLogType
)
from metamenth.subsystem.appliance import Appliance
from metamenth.transducers.sensor import Sensor
# Define room size and create room
area = MeasureFactory.create_measure(RecordingType.BINARY.value, Measure(MeasurementUnit.SQUARE_METERS, 45))
room = Room(area, "Special Office", RoomType.OFFICE)
# Create temperature sensor and assign it to the room
temp_sensor = Sensor("TEMPERATURE.SENSOR", SensorMeasure.TEMPERATURE, MeasurementUnit.DEGREE_CELSIUS, SensorMeasureType.THERMO_COUPLE_TYPE_A, 900, sensor_log_type=SensorLogType.POLLING)
room.add_transducer(temp_sensor)
# Create air conditioner and add it to the room
temp_range = MeasureFactory.create_measure("Continuous", Measure(MeasurementUnit.DEGREE_CELSIUS, 4.4, 37.8))
humidity_range = MeasureFactory.create_measure("Continuous", Measure(MeasurementUnit.RELATIVE_HUMIDITY, 20, 80))
air_conditioner = Appliance("AC.ACT", [ApplianceCategory.HOME, ApplianceCategory.SMART], ApplianceType.AIR_CONDITIONER, operating_conditions=[temp_range, humidity_range])
room.add_appliance(air_conditioner)
Implementing On/Off Control
We assume the existence of:
- An API endpoint to retrieve temperature readings
- An API endpoint to turn the air conditioner on or off
- Idempotent API behaviour (e.g., turning ON when already ON has no effect)
MetamEnTh provides an interface for defining binary control logic. We implement it below:
from metamenth.controls.binary_controls.abstract_binary_control import AbstractBinaryControl
from metamenth.transducers.sensor import Sensor
from metamenth.transducers.actuator import Actuator
from metamenth.datatypes.continuous_measure import ContinuousMeasure
import requests
class OnOffControl(AbstractBinaryControl):
def __init__(self, process_value_sensor: Sensor, process_actuator: Actuator,
control_thresholds: ContinuousMeasure, run_duration: float = None):
super().__init__(process_value_sensor, process_actuator, control_thresholds, run_duration)
def acquire_process_value_data(self) -> float:
"""Acquire temperature from API."""
url = f"https://api.example.com/devices/{self.process_value_sensor.name}/temperature"
headers = {"Authorization": "Bearer YOUR_ACCESS_TOKEN", "Accept": "application/json"}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json().get("current_value")
else:
print(f"Failed to retrieve temperature. Status: {response.status_code}")
return None
def execute_control(self, process_value: float):
"""Turn AC on or off based on threshold."""
if process_value > self.control_thresholds.maximum:
self._control_air_conditioner("on")
else:
self._control_air_conditioner("off")
def _control_air_conditioner(self, state: str):
if state not in ["on", "off"]:
raise ValueError("State must be 'on' or 'off'.")
url = f"https://api.example.com/devices/{self.process_actuator.name}/control"
payload = {"state": state}
headers = {"Authorization": "Bearer YOUR_ACCESS_TOKEN", "Content-Type": "application/json"}
response = requests.post(url, headers=headers, json=payload)
if response.status_code in [200, 204]:
print(f"Air conditioner turned {state} successfully.")
else:
print(f"Failed to set AC to {state}. Status: {response.status_code}, Response: {response.text}")
In the code above, the constructor of the control class takes three key parameters: the process value sensor (in our case, the temperature sensor), an actuator (responsible for turning the air conditioner on or off), and a control threshold (the setpoint representing the target temperature). While we define the setpoint and actuator later in the implementation, the constructor is structured to accept them as part of the control setup. It's important to note how API calls are structured: the temperature data is retrieved using the unique name of the sensor in the API URL, just as the actuator’s unique name is used in API requests to control the air conditioner. This naming convention ensures precise targeting of devices and simplifies integration with external systems.
Executing the Control Strategy
To use the control logic above, we instantiate the controller and associate all relevant components:
from metamenth.subsystem.hvac_components.controller import Controller
from metamenth.transducers.actuator import Actuator
# Create the controller
controller = Controller('CTR')
# Set desired temperature (21°C)
set_point = MeasureFactory.create_measure(RecordingType.CONTINUOUS.value, Measure(MeasurementUnit.DEGREE_CELSIUS, 21, 21))
# Create actuator linked to the air conditioner
actuator = Actuator("AC.ACT", air_conditioner)
# Add sensor and actuator to controller
controller.add_transducer(temp_sensor)
controller.add_transducer(actuator)
# Assign the setpoint to the controller with the sensor-actuator pair
controller.add_set_point(set_point, (temp_sensor.name, actuator.name))
# Initialize and assign the OnOffControl strategy
on_off_control = OnOffControl(temp_sensor, actuator, set_point)
controller.control(on_off_control)
The controller orchestrates the entire operation: it receives input from the sensor, delegates execution to the actuator, and applies the decision-making logic defined in the control strategy. In the example above, we instantiate a controller whose main role is to execute the logic encapsulated in the OnOffControl
class. To do this, the controller needs:
- An input data source – in this case, the temperature sensor
- A trigger mechanism – an actuator linked to the air conditioner
- A control target – defined by a setpoint
We first assign the previously defined temperature sensor as the controller’s input. Then, we create an actuator that is responsible for switching the air conditioner on or off. Notably, when adding a setpoint to the controller, we must specify both the sensor and the actuator involved. This is because a single controller can manage multiple sensor-actuator pairs, each with distinct setpoints. Although a setpoint can include both minimum and maximum thresholds, here we use a single desired temperature—21°C—by setting both the minimum and maximum to that value.
In our implementation, an actuator object is not strictly required if the AC can be controlled directly via an API. However, MetamEnTh is designed to support complex HVAC systems in built environments, where mechanical components often require intermediary actuators. For consistency and interoperability, we assign the actuator the same name as the air conditioner. This ensures that the actuator's name can be seamlessly used in API calls to control the appliance. Finally, the controller’s control()
method is executed periodically—driven by the polling frequency of the process value sensor. Since our sensor polls every 900 seconds (15 minutes), the control logic is evaluated at that interval.
Conclusion
By modelling IoT systems using MetamEnTh, we can implement robust control strategies independent of vendor APIs, promoting code portability, interoperability, and scalability. The on/off control strategy—though basic—serves as a foundational approach for energy-efficient building automation, especially when system behaviour can be abstracted and standardized across platforms. This approach ensures that control logic is decoupled from device-specific implementations, making it easier to adapt and reuse across different environments or integrate into larger smart building systems.
References
- Vangsgaard, A. K., Mauricio-Iglesias, M., Gernaey, K. V., Smets, B. F., & Sin, G. (2013). Control of a biological nitrogen removal process in an intensified single reactor configuration. In Computer Aided Chemical Engineering (Vol. 32, pp. 769-774). Elsevier.