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!

1 Like

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:

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?