Rd-01 for “Boss Detector” (Binary Sensor Entity)
This project integrates the Rd-01 human presence radar sensor with Home Assistant. By setting up automation, it triggers pop - up alerts on tablets or phones, serving as a warning.
This article focuses on using the Home Assistant-C library to connect binary sensors like the Rd-01 radar to Home Assistant.
I. Binary Sensor Overview
A binary sensor has only two states: 0 and 1. Infrared presence sensors, flame sensors, the Rd-01 radar, and escalator presence detectors are all binary sensors. Configuring them in Home Assistant is simple.
II. Creating a Binary Sensor Entity
This section assumes you’ve already created a “switch” entity and made a light turn on.
The following content will only cover the usage of corresponding entities. No other content will be introduced, and the code for testing won’t be set up from scratch.
Open the “main.c” file in the previously created “Home Assistant_switch” project. Under the HA_EVENT_MQTT_CONNECED event in the “ha_event_cb” callback function, create an entity as follows:
// Create a binary entity
static ha_Bsensor_entity_t entity_binary_sensor = {
.name = “Binary Sensor”, // Assign a name
.unique_id = “binary_sensor1”, // Declare a unique ID
};
// Add the binary entity to HomeAssistant
homeAssistant_device_add_entity (CONFIG_HA_ENTITY_BINARY_SENSOR, &entity_binary_sensor);
That’s it for creating a binary entity. Verify by burning the code:
You can see that the “Binary Sensor” has been added to the previously created device, but it's unavailable. What can you do?
Method 1: Reset the development board.
Method 2: Wait for me to fix it...........
III. Uploading Binary Sensor Data
The binary sensor has been created. The next step is data upload. In Home Assistant-C, use Home Assistant_device_send_state to upload data. For binary sensor data:
Home Assistant_device_send_entity_state (CONFIG_HA_ENTITY_BINARY_SENSOR, <Entity Node>, <0/1>);
For example:
After burning and resetting, the “Binary Sensor” in Home Assistant will be in the “on” state. I won’t show this effect here. The second argument of the Home Assistant_device_send_state function is the entity node. To correctly get the node anywhere, the Home Assistant-C library provides:
Home Assistant_fine_entity (char* entity_type, const char* unique_id)
When using this function, just pass the entity type and unique ID. For example, to find the binary entity with unique ID binary_sensor1:
ha_Bsensor_entity_t* entity_bs = Home Assistant_fine_entity (CONFIG_HA_ENTITY_BINARY_SENSOR, "binary_sensor1");
When using this function, just pass the entity type and unique ID. For example, to find the binary entity with unique ID binary_sensor1:
ha_Bsensor_entity_t* entity_bs = Home Assistant_fine_entity (CONFIG_HA_ENTITY_BINARY_SENSOR, "binary_sensor1");
With this function, you can use Home Assistant_device_send_entity_state in other code sections to send data like this:
Home Assistant_device_send_entity_state (CONFIG_HA_ENTITY_BINARY_SENSOR, homeAssistant_fine_entity (CONFIG_HA_ENTITY_BINARY_SENSOR, “binary_sensor1”), 1);
III. Testing
Back to the previous question: How do three switches know that only switch 1 controls the blue light?
Answer: Use the unique ID for identification.
Use Home Assistant_fine_entity to find the required entity. Then, identify the command by comparing unique IDs to confirm if it’s for “switch 1”. Also, upload the binary sensor’s status:
ha_sw_entity_t* sw1_s = homeAssistant_fine_entity (CONFIG_HA_ENTITY_SWITCH, “sw1”); // Find the “switch 1” entity
// Check if it’s a command for switch 1
if (ha_dev -> entity_switch -> command_switch -> unique_id == sw1_s...
Read more »