I’ve had a few requests for a step by step guide to setting up Flic Hub to send requests into home assistant.
The method I use requires a one time setup and will then work with all buttons attached to the hub.
Go to the flichub SDK page this connects you to your hub.
If it’s the first time you are using it then enable SDK access by following these instructions.
Press [Create Module] and call it “home assistant”.
This will create a folder with two files.
module.json - you can either leave this alone, or change the name and version number as you wish.
main.js - copy in the code below and change the ip address to the local IP of your home assistant server.
Change the two webhook IDs to random characters of your choice. You’ll need them to match to your automation trigger later.
// main.js
var buttonManager = require("buttons");
var http = require("http");
var url = "http://192.168.xx.xxx:8123/api/webhook/abcd1234abcd123-flic";
var url2 = "http://192.168.xx.xxx:8123/api/webhook/efgh5678efgh5678-";
buttonManager.on("buttonSingleOrDoubleClickOrHold", function(obj) {
var button = buttonManager.getButton(obj.bdaddr);
var clickType = obj.isSingleClick ? "click" : obj.isDoubleClick ? "double_click" : "hold";
http.makeRequest({
url: url,
method: "POST",
headers: {"Content-Type": "application/json"},
content: JSON.stringify({"button_name": button.name, "click_type": clickType, "battery_status": button.batteryStatus }),
}, function(err, res) {
console.log("request status-1: " + res.statusCode);
});
// sends a 2nd seperate request to allow triggered template sensors to have battery status
http.makeRequest({
url: url2 + button.name,
method: "POST",
headers: {"Content-Type": "application/json"},
content: JSON.stringify({"button_name": button.name, "click_type": clickType, "battery_status": button.batteryStatus }),
}, function(err, res) {
console.log("sent: " + url2 + button.name);
console.log("sent: " + button.batteryStatus);
console.log("request status-2: " + res.statusCode);
});
});
console.log("Started");
Choose your new module in the MODULES drop down box and press play. You can tick restart after crash to ensure that your script always runs.
The console.log commands will send debug statements to the console in the flic hub SDK, you can see what is happening when you press a button.
next Create a home assistant automation…
automation:
- id: flichub_click_webhook
alias: Flic button events from Flic Hub - Webhook
mode: parallel
description: >
Flic Button commands from flic hub
Receive as Webhook
trigger:
- platform: webhook
id: "flic"
webhook_id: abcd1234abcd123-flic
variables:
button_name: "{{ trigger.json.button_name }}"
click_type: "{{ trigger.json.click_type }}"
action:
- choose:
- conditions: "{{ button_name == 'button1' and click_type == 'click' }}"
sequence:
- service: counter.increment
target:
entity_id: counter.button_1_clicks
- conditions: "{{ button_name == 'button1' and click_type == 'double_click' }}"
sequence:
- service: counter.increment
target:
entity_id: counter.button1_double_clicks
You can add a conditions block for each button and click type combination you want.
If you want a battery level trigger template sensor creating for each button, you can add an automation like this…
template:
- trigger:
- platform: webhook
webhook_id: efgh5678efgh5678-button1
sensor:
- name: "Flic Battery button1"
state: "{{ trigger.json.battery_status }}"
unit_of_measurement: "%"
device_class: battery
attributes:
last_pressed: "{{ now() }}"
last_press_type: "{{ trigger.json.click_type }}"
Please feel free to ask any questions, or if you can improve my battery sensor code so it works for all buttons without needing seperate scripts, let me know.
My own version of this is in my github.