Skip to content

Commit

Permalink
Add mqtt trigger example (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real authored Jan 5, 2025
1 parent 510ec1d commit 5e93315
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions docs/user/hass_model/hass_model_subscribe_to_triggers.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,44 @@ The easiest way is to inject a NetDaemon client interface to log all messages th
Now it should log every message received. Check for the `TriggerElement` property. It has the response. It is recommended to use a single app for this or use `[Focus]` flag so you do not get flooded with messages.

From the results it should be fairly easy to construct a record that allows for deserializing the result and then use the `RegisterTrigger<T>` method instead.

## Example of using a MQTT trigger button

If you are using Zigbee2Mqtt newer versions it will only support using triggers to get actions on buttons. Below is an example
of a extension method that will subscribe to button events using MQTT triggers:

```csharp
public static class TriggerManagerExtensions
{
public static IObservable<String?> RegisterMqttButtonhActionTrigger(this ITriggerManager triggerManager, object mqttDeviceName)
{
var triggerTopic = triggerManager.RegisterTrigger(new
{
platform = "mqtt",
topic = $"zigbee2mqtt/{mqttDeviceName}/action"
});
return triggerTopic.Select(e => e.GetProperty("payload").GetString());
}
}
```
:::note

Add proper error handling as you see fit.

:::

Usage:
```csharp
[NetDaemonApp]
public class TestDeviceTriggerApp
{
public TestDeviceTriggerApp(ITriggerManager triggerManager, ILogger<TestDeviceTriggerApp> logger)
{
var trigger = triggerManager.RegisterMqttButtonActionTrigger("my_button_mqtt_device_name");
trigger.Subscribe(e =>
{
logger.LogInformation("Received event {event}", e);
});
}
}
```

0 comments on commit 5e93315

Please sign in to comment.