I assume serial-number is the one on the hub. Is button.serialnumber litterally button.(the same serialnumber)? Also, where do I get the button name?
The only thing you need to change in the code is the ip address of the hub.
The script will send the actual button name or button serial number into the event it creates.
OK, great. I seem to have it working, but I’m not sure how to get what I need. I’m currently listening to all events and when I click the one button I have connected I get this:
{
"event_type": "persistent_notifications_updated",
"data": {},
"origin": "LOCAL",
"time_fired": "2021-05-12T00:32:35.704310+00:00",
"context": {
"id": "a1bd43cc1967598bf3fc06652662cf83",
"parent_id": null,
"user_id": null
}
}
How do I use this in Home Assistant? There is nothing as a “flic” event. I can see “request status: 401” in the SDK console when I push the button but nothing useful in HA. Please explain how to use this.
I have added this code to the Flic hub API and changed the IP address and Authorization key as needed (based on what I read on the HA RESTful API documentation). Unfortunately, I seem to have hit a bit of a wall at this point, as the console shows this message:
TypeError: cannot read property 'statusCode' of undefined
at [anon] (duktape.c:56634) internal
at [anon] (root/Home Assistant/main.js:17)
at handleResponse (http.js:62)
at handlePacket (pipe_communication.js:56)
at readCallback (pipe_communication.js:93) preventsyield
I’m only guessing, but it seems to me that this is down to something not working properly with the ‘statusCode’ property. Any ideas?
The code goes onto the flux hub.
Not into home assistant.
Once you’ve done this flic events are generated by the button presses.
Sorry I forgot the bearer token bit.
You need to generate a long lived token in home assistant and put that in the code instead of the xxxxxxx bit.
Maybe my post wasn’t too clear, but I did put the code into the Flic hub using the SDK. The message was what was returned after pressing one of the Flic buttons after applying it.
I tried testing the code by commenting out what I presumed was the offending line and reinstating the two lines below it, which results in messages telling me which button I pressed and what type of press I’d used. So your code clearly works, but I think I’ve got a disconnect between what your code is doing and what HA is seeing and I suspect I’m missing something on the HA side.
Can you post your home assistant code.
And your modified flic code ?
Have you created a long lived token in home assistant and added it to the flic code instead of xxxxxx.
FYI, setting a webhook instead of an event with the endpoint /api/webhook/<webhook_id> doesn’t require a long lived token
On top of that it’s possible in NR to extract information from the headers, which contain, among other things, the remaining battery of the button.
Create an internet request in your Flic app
Set “Post”
under URL: http://ha-local-ip:port/api/webhook/your-webhook-id
in Body (at least you need to specify the click type because it’s nowehere else: single, double or hold): {“click_type”: “single”}
Content Type: application/json
Then in NR, this can be done. Battery life can be found in msg.headers.button-battery-level
Hello, thanks for the code you provided. I’m getting the same error as leopold005 when I click a Flic:
Started
TypeError: cannot read property 'statusCode' of undefined
at [anon] (duktape.c:56634) internal
at [anon] (root/Flic Hub Module/main.js:17)
at handleResponse (http.js:62)
at handlePacket (pipe_communication.js:56)
at readCallback (pipe_communication.js:93) preventsyield
I’m very new to all of this, but Im happy to do my best to explain anything else.
I’ve assume you’ve changed the URL to be the URL of your home assistant server ?
var url = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/api/events/flic”
that sounds good, I’ll look at that.
Thanks
I’ve switched by Flic Hub Code to using a webhook, so it now looks like this…
// main.js
var buttonManager = require("buttons");
var http = require("http");
var url = "http://192.168.xx.xxx:xxxx/api/webhook/flic-abcd1234fgh12";
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) {
});
});
and the automation to accept it…
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
webhook_id: flic-abcd1234fgh12
variables:
button_name: "{{ trigger.json.button_name }}"
click_type: "{{ trigger.json.click_type }}"
action:
# Update Battery Level
- service: input_number.set_value
data:
entity_id: "input_number.{{ button_name }}_battery"
value: "{{ trigger.json.battery_status }}"
- do other stuff !
@BruceH5200 thanks for this code, it helped me to integrate with Home Assistant. I made a change to the json that may be useful. To ensure you get a valid entity id I added code to remove spaces and make it lower case and set that with the name:
buttonManager.on("buttonSingleOrDoubleClickOrHold", function(obj) {
var button = buttonManager.getButton(obj.bdaddr);
var clickType = obj.isSingleClick ? "click" : obj.isDoubleClick ? "double_click" : "hold";
var buttonId = button.name.replace(" ", "_").toLowerCase();
http.makeRequest({
url: url,
method: "POST",
headers: {"Content-Type": "application/json"},
content: JSON.stringify({"button_name": button.name, "button_id": buttonId, "click_type": clickType, "battery_status": button.batteryStatus }),
}, function(err, res) {
}
});
});
For anyone else trying this, the automation examples assumes you have setup an input helper and it has and entity id with the name of the button followed by _battery e.g. flic_button1_battery (hence my need for the id).
Thanks for the info on how to use webhook to trigger a node red flow! Much appreciated!
How do you create the entities for the button batteries?
With the entity node in NR you can create an entity from NR to HA. There are several ways of doing it (setting one webhook id for all buttons or setting a different webhook id for every button or even setting a different webhook id for every click type, it’s up to you). I hope you get the idea with this simple example.
[{"id":"38e00c4e.2f57f4","type":"ha-webhook","z":"ea371c1c.7f63c","name":"webhook","server":"9405c3fe.d0a6c","outputs":1,"webhookId":"your_webhook_id","payloadLocation":"payload","payloadLocationType":"msg","headersLocation":"headers","headersLocationType":"msg","x":980,"y":340,"wires":[["46979cc8.5dd7e4"]]},{"id":"46979cc8.5dd7e4","type":"ha-entity","z":"ea371c1c.7f63c","name":"Your flic button","server":"9405c3fe.d0a6c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"my flic battery"},{"property":"device_class","value":"battery"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"%"}],"state":"headers.button-battery-level","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":1160,"y":340,"wires":[[]]},{"id":"9405c3fe.d0a6c","type":"server","name":"Home Assistant","addon":true}]
don’t forget to enable the headers under every webhook node.
That’s great, thanks
I’d given my buttons acceptable names like “bedside_bruce” so I didn’t have the issue.
But nice solution to avoid having to do that.
Hey I feel like a total idiot. I can’t figure out the proper YAML code for HA…
I have the hub setup, in the SDK prompt, its sending data successfully. I think my HA can see it just fine, but I can’t figure out the yaml portion of it. Anything I do in the editor comes up with a bunch of errors. Can ANYONE, PLEASE!!.. post their entire HA yaml config code? To integrate a Flic button to control a light as well as its battery level. So basically all I have to do is copy and paste and change my ip and flic id. Thank you in advance!