Flic Hub (Flic buttons) integration using Flic Hub SDK and HA binary sensors/automations

Hi, recently joined the HA hype and had some Flic buttons and a Flic Hub laying around, so I decided to come up with a solution to integrate them to Home Assistant in a, what I would call, more natural way to interact with the button presses. Also I realize there is the flicd integration, but upon testing it, I found the coverage of using the integrated BT in the RPi4 was very weak, less than half of the Flic Hub as per my testing.

So Flic Hub has an SDK, which you can access sitting on your local network and accessing the url:

https://hubsdk.flic.io/

In there, login and click on “Create Module” and give it a name (whichever you want), go to main.js and paste the code below. From here onwards, modify only what is in [SQUARE BRACKETS AND CAPS]

// main.js
var buttonManager = require("buttons");
var http = require("http");
var url = "http://[LOCAL IP OF YOUR HA INSTANCE]:8123/api/webhook/[NAME OF YOUR WEBHOOK]-";

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 + button.name,
		method: "POST",
		headers: {"Content-Type": "application/json"},
		content: JSON.stringify({"button_name": button.name, "serial_number": button.serialNumber, "click_type": clickType, "battery_status": button.batteryStatus }),
	}, function(err, res) {
		console.log("request status: " + res.statusCode);
	});
});

console.log("Started");

Click on the “Restart after crash” so it will restart after every reboot, and then click the Play button. This will save your code and execute it, you should see it in the console.

Now it is time to move to HA. You will then add to your configuration.yaml the following snippet. Please note, you must know the names of the buttons, which it is assumed you do since you first added them to your hub as normal. Also, if you have more than one button, just have one “template:” definition on top, and just copy/paste as of the “- trigger:” line below as many times as needed

template:
  - trigger:
      - platform: webhook
        webhook_id: [NAME OF YOUR WEBHOOK]-[NAME OF YOUR BUTTON]

    binary_sensor:
      - unique_id: "[NAME OF YOUR BUTTON]_button"
        name: "[NAME OF YOUR BUTTON] Button"
        state: "on"
        auto_off: 0.1
        attributes:
          click_type: "{{ trigger.json.click_type }}"
          battery: "{{ trigger.json.battery_status }}"

Finally, to interact with your button presses or holds, you can now go and create automations based on the binary sensor state and attributes, as follows:

- id: '[UNIQUE ID SO YOU CAN EDIT FROM THE UI IF YOU WISH TO]'
  alias: [NAME OF YOUR AUTOMATION]
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.[NAME OF YOUR BUTTON]_button
    from: 'off'
    to: 'on'
  condition:
  - condition: state
    entity_id: binary_sensor.[NAME OF YOUR BUTTON]_button
    attribute: click_type
    state: [POSSIBLE VALUES ARE: click, double_click, hold]
  action:
  - [YOUR ACTION, TURN OFF THE TV, PLAY MUSIC, CLOSE THE FRONT DOOR, ETC]
  mode: single

Create an automation for each button and press type combination, example: Button1 - Click, Button1 - Double Click, Button1 - Hold, etc etc, until you have all your buttons mapped out. You can also do the same with the battery_status attribute, should you wish to be notified when your buttons are running low on juice.

Have fun clicking away!

6 Likes

Thanks for the share, tried it, and it work !
Now I finally can do something more with the buttons then just turning on and of my LiFX bulbs :slight_smile:

2 Likes

i coped this exactly as you have, and cant get it to work. the sdk part works fine, i can see the requests piling in but i cant get any thing to work in HA

do i need to tell my button to send a request anywhere specifically?

i did it :smiley: !!!

thank you for the above, now i understand what i did and how i can go forward :grinning_face_with_smiling_eyes:

1 Like

I am receiving this error and unsure of how to troubleshoot. Any ideas?

TypeError: cannot read property 'statusCode' of undefined
    at [anon] (duktape.c:56634) internal
    at [anon] (root/Home Assistant/main.js:16)
    at handleResponse (http.js:62)
    at handlePacket (pipe_communication.js:56)
    at readCallback (pipe_communication.js:93) preventsyield

found that I was using the ip address instead of my HA link which is different since I use duckDNS. All is good and works. Thanks for your work!

Hi all.

I’m getting the same error but can’t figure out why. I’m using the local IP address of the server. My HA install isn’t accessible from outside my network so there’s no DuckDNS or similar to worry about. Everything else appears to be working. Any suggestions would be welcome.

Edit: No, wait. I was using the wrong IP. :man_facepalming: Looks like it’s working now. Thanks for the integration!

Check you are not omitting the last dash on the URL, most people are eating it up and creating a concatenation error

Thanks, I got it sorted out. Turns out when I use the correct IP address it works perfectly! :smiley:

1 Like

Hello everybody.

I’m having trouble understanding the wehook thing. Could someone help me with a pointer on how to set it up or where to find it?

Thanks in advance, this is my first post in the community :slight_smile:

1 Like

Hello, I try to copy exactly the template on my configuration.yaml but, i have the line
template: !include templates.yaml

and im recieving the error DUPLICATED KEY, how can I fix this?

1 Like

so with the flic twist coming soon, will we be able to use this work around with those devices?

1 Like

This is just excellent work. Thank you so much for improving the original idea and making it so easy and accessible. This opens up a world of better opportunities with the flic buttons.

I can get single click and hold to work on both of my buttons. However double_click is NOT registering. Any ideas?

image

how do we fix this?

2 Likes

I would also like to know!

1 Like

add:

local_only: true

to your webhook trigger definition did it for me…

1 Like

Hi guys

Total HA newbie here. The last piece of my setup jigsaw, for now, is to get my Flic integrated.

This page and instructions look great, thanks for taking the time to share. I’m not getting any success though. I’ve added the automation in automation.YAML file but nothing happening.

Would anyone be happy to help me?

Hello everyone,

I got my Flic Buttons working with flicd they are working most of the time just fine. The range is very bad and my Bluetooth dongle only supports 5 Buttons max. I got a flic hub laying around.

Is the methode with the flic hub with the sdk and webhooks working localy?

I’m not that advanced, can someone help me out?

Hi all. what is the webhook?

1 Like

This is great! Thanks @diegomorales17

This is absolutely awesome! :smiley:
I am still trying to figure out how to manage automation based on click types. I only use one help but it seems to switch a light on on click and off on hold without any click type config… which is neat but I want to add a double click function.

I edited line 16 of the flic hub code to make a more detailed log output so you can see the button name and press types. (handy for when you do not know what button it is and what the name you gave it was…)

		console.log("Activity log - request status: " + res.statusCode +", " + button.name +", " + clickType);

Thanks again, this has made flic so much more awesome.