Integration with Home Assistant

Posted on
of 2
/ 2
  • Looks good, although I guess you could keep advertising battery/temperature even when the button is pressed, and should probably advertise 0x0F/btnData.state all the time so home assistant knows the button is there all the time, just not pressed?

    the advertised count reverting to 0 after the button is pressed doesn't seem fast enough

    What exactly isn't it fast enough for?

    It's actually a bit unfortunate that they chose to do buttons this way, because it feels like either you have a short timeout and you risk the button press getting never received, or you have a long one and potentially other devices think the button is pressed more than once. If the button had been advertised as a 'press count' that just incremented when the button was pressed things would have been much easier - no timeout would be needed.

  • Thanks for the tips - as always very helpful - and again thanks for the bthome example.

  • Getting back to this with all the Home Assistant updates and saw that BTHome now has button events. Works lovely

    var SWBtn = require("https://raw.githubusercontent.c­om/muet/EspruinoDocs/master/modules/SWBu­tton.js");
    var buttonState = 0;
    var btnData = {};
    var idleTimeout;
    if (!Puck.bleAdvert) Puck.bleAdvert = {};
    // these get created with a puck button press
    var mySWBtn = new SWBtn(function(k){
      if (k === "S"  ) { // single button press
        buttonState = !buttonState;
        btnData.state = buttonState;
        btnData.btn = 0x01; = + 0; 
      else if (k === "L" ) { // long button press = 1; 
        btnData.btn = 0x04;
      else if (k === "SS") { // double short press = 2; 
        btnData.btn = 0x02;
    function initBTHome() {
      tempF = [(E.getTemperature()*9/5)+32];
      Puck.bleAdvert[0xFCD2] = [ 0x40, /* BTHome Device Information
                  bit 0: "Encryption flag"
                  bit 1-4: "Reserved for future use"
                  bit 5-7: "BTHome Version" */
                  0x01, // Battery, 8 bit
                  0x02, // Temperature, 16 bit
                  0x3A, // button event none
    setInterval(function() {
    //}, 1*60*1000); // 1 min
    }, 10000); // 10 sec
    function updateBTHome(btnData) {  
      Puck.bleAdvert[0xFCD2] = [ 0x40, /* BTHome Device Information
                  bit 0: "Encryption flag"
                  bit 1-4: "Reserved for future use"
                  bit 5-7: "BTHome Version" */
                  0x09, // count1 button press, 1 bit
                  0x0F, // binary button state, uint8 (1 byte)
                  0x3A, // button event
      if (idleTimeout) clearTimeout(idleTimeout);
      idleTimeout = setTimeout(function() {
        idleTimeout = undefined;
        Puck.bleAdvert[0xFCD2][2] = 0;
  • That's great - thanks for letting us know! Are all those elements needed?

    Looking at I see the Events heading and it looks like you need 0x3A (event), and then something before to say what it was (0x0F for generic bool?). But is the 0x09 at the start just for backwards compatibility with older BTHomes?

    I should make a bthome library to make this easy to use. Do you have any thoughts on what should be in it?

  • Just added a library for this:

    I'd be interested to see if it all works ok for you.

    I notice that button events on their own don't seem to appear though, and in your code you have defined a button - do you have any ideas if that is needed? I'm struggling to find info on it...

  • Actually I take that back - it works fine, I just didn't spot how the events come in (they're there when you look at the log for the device)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

Integration with Home Assistant

Posted by Avatar for Weiming @Weiming