You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • What happens if you just have a single button_event and change what you put in for v?

    I just tried this and it seems to work ok for me - short presses are press and long presses are long_press:

    var slowTimeout; //< After 60s we revert to slow advertising
    
    // Update the data we're advertising here
    function updateAdvertising(buttonState) {
      NRF.setAdvertising(require("BTHome").getAdvertisement([
        {
          type : "battery",
          v : E.getBattery()
        },
        {
          type : "temperature",
          v : E.getTemperature()
        },
        {
          type: "button_event",
          v: buttonState
        },
      ]), {
        name : "Sensor",
        interval: (buttonState!="none")?20:2000, // fast when we have a button press, slow otherwise
        // not being connectable/scannable saves power (but you'll need to reboot to connect again with the IDE!)
        //connectable : false, scannable : false,
      });
      /* After 60s, call updateAdvertising again to update battery/temp
      and to ensure we're advertising slowly */
      if (slowTimeout) clearTimeout(slowTimeout);
      slowTimeout = setTimeout(function() {
        slowTimeout = undefined;
        updateAdvertising("none" /* no button pressed */);
      }, 60000);
    }
    
    // When a button is pressed, update advertising with the event
    setWatch(function(e) {
      var buttonState = ((e.time - e.lastTime) > 0.5) ? "long_press" : "press";
      updateAdvertising(buttonState);
    }, BTN, {edge:"falling", repeat:true})
    
    // Update advertising now
    updateAdvertising("none");
    
    // Enable highest power advertising (4 on nRF52, 8 on nRF52840)
    NRF.setTxPower(4);
    

    I'm struggling to get Home assistant logs at the moment but this is definitely advertising the correct data.

    You could extend the setWatch call so it stores the last time it was called, and then if it was called again within ~1 second you can send "double_press" instead?

About

Avatar for Gordon @Gordon started