ESP32 NUTmini BLE Presence (home/not_home) & Home Assistant

I configured my ESP32 (DOIT ESP32 DEVKIT V1) by loading the bootloader (.bin file) and the “main.ino” file via arduinoIDE.
In the file “User_config.h” I uncommented the line “#define ZgatewayBT” BT “// ESP8266, ESP32” to use my ESP32 as a BLE gateway for NUT and the line // # define ZmqttDiscovery “HADiscovery” // ESP8266, Arduino , ESP32, Sonoff RF Bridge

The process was successful and I can see the RSSI of the NUTmini in the MQTT broker

Having to use the NUTmini for home presence, how can I enable the payload that sees the status of the NUTmini in home and not_home (or on and off) to integrate it into Home Assistant?

These are the values I currently see in MQTT:

{“Id”: “xx: xx: xx: xx: xx: xx”, “name”: “nut”, “rssi”: - 66, “distance”: 2.246586, “servicedata”: “xxxxxxxxxxxxx”}

{“Id”: “xx: xx: xx: xx: xx: xx”, “name”: “nut”, “manufacturerdata”: “59000003”, “rssi”: - 66, “distance”: 2.246586}

Thank you

There is an home presence topic used for home assistant.
OMG publish in this topic the different beacons nearby.

Nevertheless I’m not expert on this HASS function, if someone has ideas do not hesitate to share.

I think the home presence option is also only valid for using different room presence detection, but not for presence and non-presence detection. However, I’m not using HA here myself.

For a home and not_home a similar routine might be needed as I’m also using in my openHAB setup.

Regularly check if the beacon is detected/present to have the home status, for this only the unique “Id”: “xx: xx: xx: xx: xx: xx” of the payload is required.

If this regular check in HA does NOT detect the beacon during the regular interval check it sets the not_home status for this beacon.

This allows to set home and not_home for various beacons with their unique Bluetooth MAC addresses (Id in payload)

Or just looking at some other HA configuration.yaml conversation here, does this work for HA with setting

…
value_template: '{{ value_json["Id"] }}'
payload_on: "xx: xx: xx: xx: xx: xx"
payload_off: ""

Just a thought :wink:

1 Like

Thanks @1technophile @DigiH

I try the posted code.
I believe it can work in Home Assistant to check for presence in the house.

Is it possible to set a parameter in the sketch or in the .yaml file to make the beacon detect more frequently? It seems to me that it now takes over a minute to check the status.

Thanks again!

Yes, in config_BT.h with platformio

#ifndef TimeBtwRead
#  define TimeBtwRead 55555 //define default time between 2 scans
#endif

or by sending a shorter interval via MQTT command

1 Like

Thank you!!!
Is there a minimum below which it is better not to go down?
How much have you set the scan frequency for the devices you track?

The actual scan takes 10 seconds

#ifndef Scan_duration
# define Scan_duration 10000 //define the time for a scan --WARNING-- changing this value can lead to instability on ESP32
#endif

So add the interval and you get an update every 10+interval seconds.

For my setup I use an interval of 10000, so every 20 seconds I get an update.

1 Like

I had noticed that it updated the MQTT data every minute or so with the base configuration

Thanks again

Yes, as the default interval, as stated above, is 55555, so 55 seconds +10 seconds scan = every 65 seconds an update :wink:

1 Like

Now it is clear!
Thank you

Hi @DigiH,
I did some tests and I adapted the code like this because the payload_on only works for switches:

sensor:

  • platform: mqtt
    name: “nut_openmqttgateway”
    state_topic: ‘home / OpenMQTTGateway / BTtoMQTT / FD1210ER823E’
    value_template: ‘{{value_json [“id”]}}’
    payload_available: “HOME”
    payload_not_available: “NOT HOME”

but in the sensor status the id is always reported (FD: 12: 10: ER: 82: 3E)

I solved it like this:

  • platform: mqtt
    state_topic: “home / OpenMQTTGateway / BTtoMQTT / FD1210ER823E”
    name: “nut_openmqttgateway_on_off”
    value_template:>
    {% if value_json.id == ‘FD: 12: 10: ER: 82: 3E’%}
    HOME
    {% else%}
    NOT HOME
    {% endif%}
1 Like

Great to hear that you got it all working, @SmartM-ui :slight_smile:

Could be a great addition to the Docs’ HA integration page, similar to the openHAB presence example. What do you think @1technophile?

Yes it would help others in the same case !

Hi @DigiH and @1technophile,
I have reviewed the code because the one posted previously did not work well as it did not update the change of state on not_home.

I created these two sensors to be able to monitor presence in Home Assistant.
In my case, I set the status change to NOT HOME after two minutes of inactivity of the RSSI sensor.


sensor:

  - platform: mqtt
    name: "rssi_nut_openmqttgateway"
    unit_of_measurement: 'dBm'
    state_topic: 'home/OpenMQTTGateway/BTtoMQTT/XXXXXXXXXX'
    value_template: '{{ value_json.rssi | is_defined }}'

  - platform: template
    sensors:
      nut_mini_esp32_openmqttgateway_presence:
        friendly_name: "Nut Mini ESP32 Open MQTT Gateway Presence"
        value_template: >-
          {% set time = states('sensor.time')%}
          {% if ( ( ( ( ( as_timestamp(now()) ) - ( as_timestamp( states.sensor['rssi_nut_openmqttgateway'].last_changed) ) | int ) | round(0) ) / 60 ) | int ) <2%}
            HOME
          {% else %}
            NOT HOME
          {% endif %}
2 Likes