hueplusplus 1.2.0
Loading...
Searching...
No Matches
Sensors

Sensor support

The library supports the sensor types listed on the Hue developer documentation. Include hueplusplus/ZLLSensors.h for ZigBee sensors and hueplusplus/CLIPSensors.h for CLIP sensors. Other sensors can be used with the generic Sensor class.

Working with a known sensor

In most cases, the type of the sensors is known in advance, such as a switch. The classes in the sensors namespace provide the documented functionality. The type can be specified when accessing the sensor. When it does not match, an exception is thrown.

hueplusplus::sensors::ZLLSwitch switchSensor = bridge.sensors().getAsType<hueplusplus::sensors::ZLLSwitch>(2);
ZigBee sensor reporting button presses.
Definition ZLLSensors.h:69

You can also get all sensors of a specified type by using getAllByType<T>().

std::vector<hueplusplus::sensors::ZLLSwitch> allSwitches
= bridge.sensors().getAllByType<hueplusplus::sensors::ZLLSwitch>();

Working with an unknown sensor

When the sensor type is not known, use the generic sensor class. In this case, some attributes might not exist, so they have to be checked first. This applies to all attributes that have a hasXXX method.

hueplusplus::Sensor genericSensor = bridge.sensors().get(1);
if (genericSensor.hasOn())
{
// Now can check whether it is on
if (genericSensor.isOn())
{
// ...
}
}
Class for generic or unknown sensor types.
Definition Sensor.h:60
bool isOn() const
check whether the sensor is turned on
Definition Sensor.cpp:65
bool hasOn() const
Check whether the sensor has an on attribute.
Definition Sensor.cpp:60

It is easiest to compare the sensor type to the existing ones (typeStr on the specific sensor classes) and then convert the sensor to that type.

hueplusplus::Sensor genericSensor = bridge.sensors().get(1);
{
// ...
}
virtual std::string getType() const
Const function that returns the device type.
Definition BaseDevice.cpp:39
T asSensorType() const &
Convert sensor to a specific type.
Definition Sensor.h:210
static constexpr const char * typeStr
ZLLSwitch sensor type name.
Definition ZLLSensors.h:148

ZLL sensors vs. CLIP sensors

ZLL sensors (defined in ZLLSensors.h) are physical device sensors which send their data to the bridge using ZigBee. They are added in the same way as lights are, using search().

CLIP sensors (in CLIPSensors.h) are added using create() with CreateSensor for parameters. In general, which config and state attributes exist is specified when the sensor is created. The values of CLIP sensors can be changed using requests, unlike ZLL sensors. They can also have a URL to query from.

Creating conditions

The most important use for sensors is in Rules, to trigger changes. Conditions can be created from the specific sensor types using makeCondition().

These functions return a helper class with methods for the possible operators valid for the state.

For some sensors, which have multiple possible states, there exist multiple variations of makeCondition.

hueplusplus::sensors::ZLLSwitch switchSensor = bridge.sensors().getAsType<hueplusplus::sensors::ZLLSwitch>(2);
// ZLLSwitch conditions operate on `buttonEvent`, use makeConditionLastUpdate()
// to trigger on the last update time.
// Some examples:
= makeCondition(switchSensor).eq(hueplusplus::sensors::ZLLSwitch::c_UP_INITIAL_PRESS);
hueplusplus::Condition buttonChanged = makeCondition(switchSensor).dx();
hueplusplus::time::TimeInterval interval(std::chrono::hours(12), std::chrono::hours(13));
hueplusplus::Condition updatedAtNoon = makeConditionLastUpdate(switchSensor).in(interval);
Condition for a Rule.
Definition Condition.h:38
static constexpr int c_UP_INITIAL_PRESS
Button 2 (DIM UP) pressed.
Definition ZLLSensors.h:123
Time interval repeated daily to weekly.
Definition TimePattern.h:261

For generic sensors, the conditions must be created manually using the Condition constructor with a proper address to the sensor state.