Flic hub http post --》 extract battery from header

I have managed to integrate the flic buttons presses via the flic hub http requests (api/events) :slight_smile: thx to alot of posts in this community.

I want to take it a step further and read that the battery level of the buttons are in the header of the post from the hub.
Any one succeeded in that?
"
The HTTP header does contain the following information: button-serial-number; button-battery-level; button-name.
"

I can only see the body of the request when listening to the event.

Thx

I would also like that +1

I got “Red Node” up and running as I hoped to be able to extract the header information from the http request fromt he flic hub. But with no luck. The msg.headers does not exists and the other "id"s does not contain what I hoped to find.

To make sure that the battery level actually are in the header information I put up at python-very-simple-http-server to listent to the requests from the http request from the flic-hub. I got the following result:

10.0.0.99 - - [31/Jan/2021 21:50:02] "GET /favicon.ico HTTP/1.1" 200 -
INFO:root:POST request,
Path: /api/events/test
Headers:
Accept-Encoding: gzip
Authorization: Bearer --censored---
User-Agent: Google-HTTP-Java-Client/1.23.0 (gzip)
button-serial-number: Bearer_censored
button-battery-level: 100
button-name: Test
timestamp: 2021-01-31T20:50:10Z
Content-Type: json
Cache-Control: no-cache
Pragma: no-cache
Host: 10.0.0.99:8080
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: close
Content-Length: 49

As it is seen above header does indeed include the battery level, but I do not know how to extract that information when the HA receives the request from the flic hub via /api/events/test .
Does anyone have an idea to extract the header information on received requests?

I hope you get it working +1

I actually made it work.
It was a long shot and probably not the optimal solution.

What I did was:

  1. create each Button battery from template in the configuration.yaml
  2. created a dockerized webservice that split the http request into an api/evnets/flic and another one which posts an update to /api/state/sensor.flic_button1 (example). The webserver change the original flic post battery level from the header to state in the body.

That’s basically it.
It took some time and there probably a lot easier way through but I have not enough knowledge about the HA frame work.
In the end of the day I made it, but it is difficult to share…

I’ll give it a go, thanks a lot!

I found a way in Node Red to extract the battery information, I believe it’s similar to your approach?

Using the webhook node. If you change the URL of your requests to “http://your-ha-local-ip:8123/api/webhook/flic_click” (example), set your webhook id to “flic_click” and set the headers to wherever you wanna place it, it does yield that information.

Hi Obaldius
Not at all like my solution. My solution is (unnecessarily) way more complicated. Your solution seems like an easier solution. I did not go down the webhook path - didn’t even try. Will look into this soon. It is easier to get the battery directly out of flic-post in HA and not make a webserver to extract it. Great tip.

I did notice that some of my old buttons does not have the battery values - but that is more an observation if anyone wants to replicate the solution. I would guess that that battery level will not show up in the webhook solution as well. My guess is that the information is not extracted/measured at all.

Would like to see you node red flow for updated the battery level variable (complete NR flow)

It’ll be something like that, I haven’t tested it yet although it should work. Dont forget to change the endpoint in your flic hub from /api/events/test to /api/webhook/yourwebhookid (flic_click in my example)

With this webhook node it’s also possible to use the payload information so it can also be used to distinguish among single/double/hold pushes.

If there’s some error let me know, I’ll be able to test it this weekend

[{"id":"d244a896.154dc8","type":"ha-webhook","z":"d4450f17.60952","name":"","server":"9405c3fe.d0a6c","outputs":1,"webhookId":"flic_click","payloadLocation":"payload","payloadLocationType":"msg","headersLocation":"headers","headersLocationType":"msg","x":200,"y":760,"wires":[["60b790ab.9ba84"]]},{"id":"9ece2f9a.87b8f","type":"comment","z":"d4450f17.60952","name":"Battery level","info":"","x":210,"y":640,"wires":[]},{"id":"60b790ab.9ba84","type":"switch","z":"d4450f17.60952","name":"Which button?","property":"headers.button-name","propertyType":"msg","rules":[{"t":"eq","v":"Button A friendly name","vt":"str"},{"t":"eq","v":"Button B friendly name","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":370,"y":760,"wires":[["3af552f2.8e547e"],["891a23e3.6346d"]]},{"id":"3af552f2.8e547e","type":"ha-entity","z":"d4450f17.60952","name":"Button A battery","server":"9405c3fe.d0a6c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"button a battery"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"%"}],"state":"headers.button-battery-level","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":580,"y":740,"wires":[[]]},{"id":"891a23e3.6346d","type":"ha-entity","z":"d4450f17.60952","name":"Button B battery","server":"9405c3fe.d0a6c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"button b battery"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"%"}],"state":"headers.button-battery-level","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":580,"y":780,"wires":[[]]},{"id":"9405c3fe.d0a6c","type":"server","name":"Home Assistant","addon":true}]

I got it working, I post it just in case it’s usefull for you.

I find it unnecessary to create a sensor for each battery. Instead, I receive a persistent notification when the battery drops below 10%. Notifications are rate limited to one every other day for every button individually, so I don’t get a notification every time I push the button.

I guess it will require some tweaking but it’s a decent starting point.

[{"id":"3318325e.33cf2e","type":"ha-webhook","z":"ea371c1c.7f63c","name":"Flic click","server":"9405c3fe.d0a6c","outputs":1,"webhookId":"flic_click","payloadLocation":"payload","payloadLocationType":"msg","headersLocation":"headers","headersLocationType":"msg","x":360,"y":180,"wires":[["80f63d9f.81a9f"]]},{"id":"80f63d9f.81a9f","type":"switch","z":"ea371c1c.7f63c","name":"battery < 10%?","property":"headers.button-battery-level","propertyType":"msg","rules":[{"t":"lte","v":"10","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":520,"y":180,"wires":[["5f7baf1a.53fd9"]]},{"id":"c2f83546.d1ff68","type":"delay","z":"ea371c1c.7f63c","name":"","pauseType":"queue","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"2","rateUnits":"day","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":930,"y":180,"wires":[["c7b19bb5.efba58"]]},{"id":"5f7baf1a.53fd9","type":"change","z":"ea371c1c.7f63c","name":"","rules":[{"t":"change","p":"topic","pt":"msg","from":"topic","fromt":"msg","to":"headers.button-name","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":180,"wires":[["c2f83546.d1ff68"]]},{"id":"c7b19bb5.efba58","type":"api-call-service","z":"ea371c1c.7f63c","name":"Persistent","server":"9405c3fe.d0a6c","version":1,"debugenabled":false,"service_domain":"persistent_notification","service":"create","entityId":"","data":"{\"title\":\"La batería del flic {{topic}} está al {{headers.button-battery-level}} %\",\"message\":\" \"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1100,"y":180,"wires":[[]]},{"id":"9405c3fe.d0a6c","type":"server","name":"Home Assistant","addon":true}]

EDIT

Just in case, what the flow does is: every time a flic is pushed it checks whether the battery of that button is below 10%. If it is, the delay/rate limit node will only allow the flow on if it hasn’t notified you for at least 5 days. The name of your button (the name according to the Flic Hub) is stored in msg.topic. The battery level is stored in msg.headers.button-battery-level.

I believe I’d rate limit it at 1 msg every 5 days. Every 2 days is gonna be too many notifications.

I ended up creating all the entities, much better. Didn’t like the last approach. I did it with the first flow. Set device class to “battery” in every entity node.

Tonight I tested you setup. Much better than create a Docker. Can you please provide a screen shot of the finished setup. I have difficulties figuring out how is is best to create them and update them afterwards.
It seems like I get them twice and they are not updated correctly.
From your picture I can see that you are using a icon template. Can you shared that one too?
The import function in node red is really cool to share stuff.

Atm I’m not home. I’ll be able to help you in a few weeks. I’ll be back :blush:

I’m not using icon template. If you set the device class to battery under the entity node, it’ll automatically do the job for you.

I basically have one webhook endpoint for all my buttons. With a switch node I filter which button has been pushed, and every output of that switch node leads to a different entity node, one for each button. Set device class to battery.

If you still need help, I’ll be back with clearer information in a few weeks.

There you go. If you still have problems let me know. I basically created one webhook endpoint for all my flics. Every time any of them is clicked, the flow is triggered. So every time any flic is clicked, its battery value gets updated.

[{"id":"48af02cb.28f8dc","type":"switch","z":"ea371c1c.7f63c","name":"Qué botón?","property":"payload.button_name","propertyType":"msg","rules":[{"t":"eq","v":"Campana","vt":"str"},{"t":"eq","v":"Mando TV","vt":"str"},{"t":"eq","v":"Cocina","vt":"str"},{"t":"eq","v":"entrada pamen","vt":"str"},{"t":"eq","v":"mesita pamen","vt":"str"},{"t":"eq","v":"entrada eduardo","vt":"str"},{"t":"eq","v":"mesita eduardo","vt":"str"},{"t":"eq","v":"lavabo","vt":"str"},{"t":"eq","v":"espejo","vt":"str"},{"t":"eq","v":"ducha","vt":"str"},{"t":"eq","v":"vater","vt":"str"}],"checkall":"false","repair":false,"outputs":11,"x":410,"y":260,"wires":[["713ee52a.cab09c"],["2e0667f9.e836b8"],["e2d2737c.316fb"],["a31876df.f4ef98"],["dc03d53b.b7dcc8"],["f5880515.31aa88"],["5a4058e8.3cf808"],["98208470.38fc48"],["a6a758c9.e92348"],["4afd30e9.aae91"],["d43a9c3.262c06"]]},{"id":"713ee52a.cab09c","type":"ha-entity","z":"ea371c1c.7f63c","name":"Campana","server":"9405c3fe.d0a6c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"bateria del flic campana"},{"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":680,"y":80,"wires":[[]]},{"id":"2e0667f9.e836b8","type":"ha-entity","z":"ea371c1c.7f63c","name":"Mando TV","server":"9405c3fe.d0a6c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"bateria del flic mando TV"},{"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":690,"y":120,"wires":[[]]},{"id":"e2d2737c.316fb","type":"ha-entity","z":"ea371c1c.7f63c","name":"Cocina","server":"9405c3fe.d0a6c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"bateria del flic cocina"},{"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":680,"y":160,"wires":[[]]},{"id":"a31876df.f4ef98","type":"ha-entity","z":"ea371c1c.7f63c","name":"entrada pamen","server":"9405c3fe.d0a6c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"bateria del flic entrada pamen"},{"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":700,"y":200,"wires":[[]]},{"id":"dc03d53b.b7dcc8","type":"ha-entity","z":"ea371c1c.7f63c","name":"mesita pamen","server":"9405c3fe.d0a6c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"bateria del flic mesita pamen"},{"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":700,"y":240,"wires":[[]]},{"id":"f5880515.31aa88","type":"ha-entity","z":"ea371c1c.7f63c","name":"entrada eduardo","server":"9405c3fe.d0a6c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"bateria del flic entrada eduardo"},{"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":700,"y":280,"wires":[[]]},{"id":"5a4058e8.3cf808","type":"ha-entity","z":"ea371c1c.7f63c","name":"mesita eduardo","server":"9405c3fe.d0a6c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"bateria del flic mesita eduardo"},{"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":700,"y":320,"wires":[[]]},{"id":"98208470.38fc48","type":"ha-entity","z":"ea371c1c.7f63c","name":"lavabo","server":"9405c3fe.d0a6c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"bateria del flic lavabo"},{"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":670,"y":360,"wires":[[]]},{"id":"a6a758c9.e92348","type":"ha-entity","z":"ea371c1c.7f63c","name":"espejo","server":"9405c3fe.d0a6c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"bateria del flic espejo"},{"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":670,"y":400,"wires":[[]]},{"id":"4afd30e9.aae91","type":"ha-entity","z":"ea371c1c.7f63c","name":"ducha","server":"9405c3fe.d0a6c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"bateria del flic ducha"},{"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":670,"y":440,"wires":[[]]},{"id":"d43a9c3.262c06","type":"ha-entity","z":"ea371c1c.7f63c","name":"vater","server":"9405c3fe.d0a6c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"bateria del flic vater"},{"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":670,"y":480,"wires":[[]]},{"id":"3318325e.33cf2e","type":"ha-webhook","z":"ea371c1c.7f63c","name":"Flic click","server":"9405c3fe.d0a6c","outputs":1,"webhookId":"flic_click","payloadLocation":"payload","payloadLocationType":"msg","headersLocation":"headers","headersLocationType":"msg","x":160,"y":260,"wires":[["9f5f18a9.07b058","48af02cb.28f8dc"]]},{"id":"9405c3fe.d0a6c","type":"server","name":"Home Assistant","addon":true}]

is it possible to do all this even without node red? I’m a beginner btw

there are other options without NR in this thread

Hi thanks for the response, I’ve had a setup for a few years now where I have input booleans in the configuration.yaml and each of those have their own automation command. And then in flic, I have an http request setup to call the specific input booleans but it doesn’t return any battery info.

Any chance I could modify my setup to extract the battery info?

I can’t help you sorry… Node red is pretty newbie proof. In fact it’s an easier option than YAML for a newcomer. I wouldn’t be scared, you won’t regret it.

Ah gotcha, I would love to use node red but, I’m running homeassistant through docker in Synology and I don’t seem to have the supervisor tab available.

If you have a flic hub, take a look at: