In this tutorial, we will explain how to create simple interfaces between the EQSP32 and Home Assistant. The EQSP32 uses the MQTT protocol to communicate with Home Assistant. For the two systems to exchange information, we need to have some control and monitoring parameters, which we call interfaces.
Each interface has a unique name that will appear in Home Assistant, either as a control variable or a displayed sensor value. To create such parameters, we need to use the respective “create” function.
Briefly the available functions for creating interfacing entities are:
createDisplay_BinarySensor( ... )
createDisplay_Sensor( ... )
createControl_Switch( ... )
createControl_Value( ... )
Prerequisites
Before we begin, ensure you have the following:
- An EQSP32 device.
- MQTT device discovery enabled.
- The Home Assistant server running an MQTT broker (Mosquitto broker)
Check out our tutorial on how to set up your MQTT broker and enable MQTT device discovery in your EQSP32.
Create a Monitoring Interface
Let’s start with the first case where we create an interface for a temperature sensor using the ADIO pin 1 in TIN mode. To create a temperature interface we can define the sensor type as “temperature” and we may also assign a desired default icon for the entity.
Here is a code example:
#include "EQSP32.h"
EQSP32 eqsp32;
void setup() {
// Initialize EQSP32 module
eqsp32.begin(true); // Verbose enabled
// Configure pin 1 as Temperature Input (TIN)
eqsp32.pinMode(1, TIN);
eqsp32.configTIN(1, 3988, 10000); // Beta and reference resistance values
// Create a sensor in Home Assistant to monitor the temperature
createDisplay_Sensor("Temperature Sensor", 1, "°C", "mdi:thermometer", "temperature");
}
void loop() {
// Read the temperature from the sensor (value is in °C × 10)
float temperature = eqsp32.readPin(PIN_TEMPERATURE_SENSOR) / 10.0; // Convert to Celsius
// Update the Home Assistant sensor with the new temperature reading
updateDisplay_Sensor("Temperature Sensor", temperature);
delay(10); // Small delay for non-blocking operation
}
In this example, we:
- Initialize the EQSP32 module.
- Configure pin 1 as a Temperature Input (TIN) with our specific NTC thermistor parameters.
- Create a Home Assistant sensor named “Temperature Sensor” for monitoring temperature readings.
- Periodically update the entity with new temperature measurements.
Creating the Interface
Defining the Interface
The createDisplay_Sensor function is used to define a new sensor interface for Home Assistant.
- The first argument is the sensor name, which will be displayed in Home Assistant. In this case, it is “Temperature Sensor”.
- The second argument specifies the number of decimal places for the displayed value (e.g., 23.5°C).
- The third argument defines the unit of measurement, which is
"°C"for temperature. - Additional optional arguments allow specifying a Home Assistant icon (
"mdi:thermometer") and defining the sensor type ("temperature").
Updating the Interface’s Value
Once the temperature sensor interface is created, we can update it will the measured temperature values. The updated values will then be published in Home Assistant.
- The sensor on pin 1 continuously reads temperature values from the thermistor.
- The EQSP32 library processes and updates the temperature data in Home Assistant in real-time.
- Home Assistant displays the readings as temperature values without requiring manual intervention.
Real-time monitoring in Home Assistant
Let’s take a look at our discovered EQSP32 device.
- Navigate to Settings, then Devices & Services.
- Click on Device inside the MQTT integration.
Adding EQSP32 in Home Assistant’s dashboard
Since our device is available, we can now add it to our dashboard as a system or as separate entities. This allows us to integrate it with existing automations to expand its capabilities.
- Click Add to dashboard.
- Choose the dashboard we want EQSP32 to be added.
- Our device is successfully added in our dashboard.
Create a Control Interface – Virtual Switch Example
In this example, we demonstrate how to create a control interface for a virtual switch in Home Assistant. The created switch will appear as a Home Assistant entity, allowing users to send commands to the EQSP32.
As a simple use case, the virtual switch’s state will be used to control a relay connected to the EQSP32.
#include "EQSP32.h"
EQSP32 eqsp32;
void setup() {
// Initialize EQSP32 module
eqsp32.begin(true); // Verbose enabled
// Configure pin 1 as Relay
eqsp32.pinMode(1, RELAY);
eqsp32.configRELAY(1, 500, 1000); // Default hold value and derate delay
// Create a Home Assistant switch entity to control the relay
createControl_Switch("Relay Control", "mdi:power");
// Create a separate Home Assistant binary sensor to display the relay's actual state
createDisplay_BinarySensor("Relay State", "mdi:power-plug");
}
void loop() {
// Read the virtual switch state from Home Assistant
bool relayState = readControl_Switch("Relay Control");
// Apply the switch command to control the relay
eqsp32.pinValue(1, relayState ? 1000 : 0);
// Get the actual relay state
bool actualRelayState = eqsp32.readPin(1) > 0;
// Update the separate display entity with the actual relay state
updateDisplay_BinarySensor("Relay State", actualRelayState);
delay(10); // Small delay for non-blocking operation
}
Example Explanation
Initialization:
- The EQSP32 module is initialized using
eqsp32.begin(). - Verbose mode is enabled to provide detailed logging for debugging and monitoring.
Configure Pins:
- Pin 1 is configured as a relay using
pinMode(1, RELAY). - The relay is further set up with
configRELAY(1, 500, 1000), which defines the default hold value and derate delay.
Create Custom Interfaces:
- A virtual switch interface named
"Relay Control"is created usingcreateControl_Switch("Relay Control", "mdi:power").- This allows Home Assistant to send ON/OFF commands to control the relay.
- A binary sensor interface named
"Relay State"is created usingcreateDisplay_BinarySensor("Relay State", "mdi:power-plug").- This monitors the actual state of the relay and updates Home Assistant accordingly.
Loop:
- The state of the virtual switch
"Relay Control"is read usingreadControl_Switch("Relay Control"). - If the switch is ON, the relay is set to 100% power; otherwise, it is turned OFF.
- The actual relay state is retrieved using
eqsp32.readPin(1) > 0. - The relay state is then updated in Home Assistant using
updateDisplay_BinarySensor("Relay State", actualRelayState), ensuring that the system reflects real-time feedback.
Real-time control in Home Assistant
For our second example, our device’s entities look a little bit different. We have a binary sensor to display the load’s state but we’ve also added a virtual switch to the interfaces. Since the switch is a control element, we see that we now have the “Controls” category added to our device.
By toggling the switch’s state, we can now control our load. By monitoring the “Relay state”, we have feedback on our load’s state.
This enhancement allows for direct interaction with the device through the Home Assistant dashboard, providing real-time control and monitoring of the connected relays.
Conclusion
We have successfully created monitoring interfaces for directly monitoring sensor measurements to Home Assistant. Furthermore, we created a control interface to control relays with a virtual switch.
It is important to mention that once each entity is available in Home Assistant (HA), it can be used internally for additional integrations and automations within Home Assistant. These entities can be combined with existing automations, sensors, and controls, greatly expanding the overall system’s functionality.
For a more complex real life application example, you will enjoy reading the Smart Solar Water Heater with Home Assistant Integration article.