Flic Hub SDK -> Web Hook (step by step)

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.

5 Likes

Thank You, This was very helpful and I got the basics working! I’m just starting with Home Assistant and was surprised my flic buttons didn’t have better integration, but this solution will work fine for me.

Thank You for the effort.

I bought several flic buttons and the hub that not being used, because I could not figure out how to make it go in Home Assistant. This looks very promising.

Unfortunately, my Flic hub is a couple of years old. As a result, I probably have to update my FLIC hub firmware. FLIC documentation on firmware updates is sparse to say the least.

After I figure out how to update the FLIC hub’s firmware, I will try again…

1 Like

Hi,

The first part of your instructions were clear and I am ready to create my Automation. I have setup the FLIC SDK module. It was a very easy cut and paste operation once I updated my FLIC firmware to version 3.012

Subsequently, I blindly attempted to cut, paste, and adjust my automations.Yaml from your example. I am getting multiple errors.

Question: Is your automation example designed to be cut, pasted into the Automations.Yaml , and subsequently user adjusted in a manner similar the method used in setting up the SDK module?

Regards,
Craig

Thank you so much for this! I had found a roundabout way to trigger HA stuff via Alexa and Alexa HA nodes in Node Red but this is so much easier.

One question. Do you know if/how this works with the new Flic Twist? I can think of so many applications for it if integration is this easy.

When my flic twist arrives I’ll let you know.

1 Like

This just stopped working out of the blue today. No changes made, but HA doesn’t ever show the web hook received. I re-copied and pasted your code and changed the webhook and it works again, but I can’t imagine why it stopped. Any thoughts?

You could try going into the flic SDK and restarting it.
You can set it for auto re-start I think

I have it set to Restart after crash, but it happened again today. The only thing that fixes it is to delete the module and build a new one exactly the same. Seems odd.

Thank you so much for this. I just got my Flic hub and buttons today and ran through all this per your instructions. I got everything going except that the double click automation is not running. Single click does what it’s supposed to do; double click does nothing. I see in the HA automation trigger section that it is receiving the double click trigger input, but it’s not doing any action. Any thoughts? Also, what about the press-and-hold flic action? Thanks!

Update: Forgot to post my automation yaml.

description: ""
trigger:
  - platform: webhook
    webhook_id: 5011moretto-flic1
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ button_name == 'Kitchen' and click_type == 'click' }}"
        sequence:
          - type: toggle
            device_id: 959a65c585a4b35c874a3508a6b6a239
            entity_id: light.kitchen_island
            domain: light
      - conditions:
          - condition: template
            value_template: "{{ Kitchen == 'button1' and click_type == 'double_click' }}"
        sequence:
          - type: toggle
            device_id: 001599bacdb9364faaa5a01a06f46b8f
            entity_id: light.kitchen_lights
            domain: light
mode: parallel
variables:
  button_name: "{{ trigger.json.button_name }}"
  click_type: "{{ trigger.json.click_type }}"

type or paste code here

You have written

Kitchen = button1

When you meant.

Button name = kitchen

Ok ?

I have created a similar solution, with more front-end editing capabilities and only 1 webhook call at the flic hub level to update click_type and battery_level attributes. If you are interested, you can check it out here:

https://community.home-assistant.io/t/flic-hub-flic-buttons-integration-using-flic-hub-sdk-and-ha-binary-sensors-automations/477146

Great article but console output in main.js is not an accurate account (for newbies) of what is happening. Please consider replacing the code block with this one.

// main.js
// Webhook trigger fires when a web request is made to the webhook endpoint: /api/webhook/<webhook_id>.

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("sent URL: " + url);
		console.log("content: " + JSON.stringify({"button_name": button.name, "click_type": clickType, "battery_status": button.batteryStatus }))
		console.log("request status-1: " + res.statusCode);	
	});
	
	// sends a 2nd separate 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 URL: " + url2 + button.name);
		console.log("content: " + JSON.stringify({"button_name": button.name, "click_type": clickType, "battery_status": button.batteryStatus }))
		console.log("request status-2: " + res.statusCode);
	});
});

console.log("Started");

Also could you add to next Create a home assistant automation the words in configuration.yaml or a more specific instruction, again for me. If this is actually where it goes - because I get duplicate key here!!!
Best Regards

Thank you soo much @BruceH5200 I have been trying to get this to work.
Do you know if it cares about SSL certs? I hope not.
I keep seeing this error:

I seem to see no action happening in HA i think it might be because of my SSL
Heads up I am going one step further and attempting to update a helper for each button.

ok seemed to work using this method:

I just purchased a new set of buttons. Would I add a new set of variables and a new webhook to the same module to allow for using multiple buttons or a new module for each button?

You don’t need to change or add anything.
The code sends the button name.
So it will just send new button names.
So in your automation that accepts the webhook you create a choose based on the value.

So enjoying this! I now have 9 active buttons controlling a myriad of things. One question. Is there a way to test battery levels (say at a certain time of day) automatically rather that physically pushing the buttons? It seems this shouldn’t be difficult but I can’t wrap my head around it.

They only transmit when you press the button.
The batteries generally won’t go down unless you are using the buttons.
So getting the battery level when they are clicked is more than enough.