I just spent too long trying to figure this out since the documentation is not up to date and the chatGPT bot is just wrong.
Anyways I thought I would share how I am handling my first Flic Duo device to trigger different Home Assistant automations using the different buttons and webhooks . Hopefully this might save other people some time and effort.
main.js
var buttonManager = require("buttons");
var http = require("http");
buttonManager.on("buttonSingleOrDoubleClickOrHold", function(obj) {
var button = buttonManager.getButton(obj.bdaddr);
var clickType = obj.isSingleClick ? "click" : obj.isDoubleClick ? "double_click" : "hold";
var payload = JSON.stringify({
button_name: button.name,
click_type: clickType,
});
var targetUrl = null;
if (button.name === "lock") {
targetUrl = "http://XXX.XXX.XX.XX:8123/api/webhook/flic-lock";
} else if (button.name === "auto1") {
targetUrl = "http://XXX.XXX.XX.XX:8123/api/webhook/flic-leavingHome";
} else if (button.name === "duo1") {
if (obj.buttonNumber === 0) {
targetUrl = "http://XXX.XXX.XX.XX:8123/api/webhook/flic-duo1-big";
} else if (obj.buttonNumber === 1) {
targetUrl = "http://XXX.XXX.XX.XX:8123/api/webhook/flic-duo1-small";
}
}
if (targetUrl) {
http.makeRequest({
url: targetUrl,
method: "POST",
headers: {"Content-Type": "application/json"},
content: payload,
}, function(err, res) {
console.log("");
console.log("Sent to: " + targetUrl);
console.log("Name: " + button.name);
console.log("ID (bdaddr): " + button.bdaddr);
console.log("Serial: " + button.serialNumber);
console.log("Click Type: " + clickType);
console.log("uuid: " + button.uuid);
console.log("key: " + button.key);
//console.log("Button #: " + obj.buttonNumber);
console.log("Status: " + res.statusCode);
});
}
});
console.log("Script started");
obj.buttonNumber returns 0 for the upper bigger button and 1 for the smaller lower button
You can also perform different automations depending on click type using an if statement where you change the keyword click using something like (YAML):
conditions:
- condition: template
value_template: "{{ trigger.json.click_type == 'click' }}"
The HA automation webhook trigger YAML looks something like:
triggers:
- allowed_methods:
- POST
- PUT
local_only: true
webhook_id: flic-leavingHome
trigger: webhook
This might not be the best way to go about this but I would be interested to hear suggestions for improvements. Now do add the rest of the new buttons and model the dimensions as a 3D STL file. It would be nice if flic also provided this as well.