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

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.

Hi - should this work with the flic twist?
I can see it is sending push button events, but not sure it is picking up any twist events?

No HubLR SDK support for the twist action yet :frowning:

Message malformed: required key not provided @ data[‘action’]

Sorry for beeing a spamming useless noob :slight_smile:
But I copied the code exatly and even called the button “button1” but I get the error message shown on the top.

Can you post the file that you put the code into, there is probably a YAML formatting problem.

I’m having exactly the same issue when I try saving the automation. Here’s the YAML code I pasted into the new automation window:

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: abcd1234-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

When I try to save, it generates the error:
Message malformed: required key not provided @ data[‘action’]

If you are pasting it i to a new automation it probs it doesn’t need the

Automation:

Line at the top.

That is there because I use “packages” to manage my yaml.

Try removing it.

Hi Bruce
Thanks for these instructions. I created an automation with your coding but home assistant changes it to

alias: Flic button events from Flic Hub
description: Flic button commands Receive as Webhook
triggers:
  - webhook_id: [mywebhook]
    trigger: webhook
    allowed_methods:
      - POST
      - PUT
      - GET
      - HEAD
    local_only: false
    id: flic
actions:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ button_name == 'Office' and click_type == 'click' }}"
        sequence:
          - action: light.turn_on
            metadata: {}
            data:
              brightness_pct: 100
              kelvin: 6500
            target:
              entity_id: light.bedroom_office_light
      - conditions:
          - condition: template
            value_template: "{{ button_name == 'Office' and click_type == 'double_click' }}"
        sequence:
          - action: light.turn_off
            target:
              entity_id:
                - light.bedroom_office_light
            data: {}
variables:
  button_name: "{{ trigger.json.button_name }}"
  click_type: "{{ trigger.json.click_type }}"
mode: restart

and the automation is not triggered. Can you provide any pointers?

Did you create the code on the flic hub to send the webhook ?

Yes, when I click my button the flic hub SDK console reports that the webhook has been sent

// main.js
var buttonManager = require("buttons");
var http = require("http");
var url = "http://[home assistant url]:8123/api/webhook/[webhook1]";
var url2 = "http://[home assistant url]:8123/api/webhook/[webhook2]-";

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");