Trying to set up presence detection with bluetooth tracker

Hi everyone. I am new to home assistant. Just managed to set up HASSIO on Raspberry Pi 4. I’m trying to use the Pi’s bluetooth to track presence in the living room and turn on some lights. Since the seating area in the living room is close to the pi, the rssi value shows between 0 to -5. So I have setup the following automation:-

Here is my automation code:-

  alias: Hall Presence 2
  trigger:
  - platform: template
    value_template: '{{ states.device_tracker.mi_phone.attributes.rssi | int > -5   }}'
  condition:
  - condition: time
    after: '19:00'
    before: '07:00'
    
  action:
  - data:
      entity_id: switch.sonoff_10008a346b
    service: switch.turn_on
  - data:
      entity_id: switch.sonoff_10008a62a6
    service: switch.turn_on

Not sure why this is not working. Sometimes the automation shows its triggered, but the switches don’t turn on. Most of the times, it just don’t trigger at all.
I hope someone experienced can help me out.

check your condition. I’m not sure / can’t remember if you can have a condition like this as time cannot be after 19 and before 7 at the same time.
Try and see if you can split into 2 conditions:

  condition:
  - condition: time
    before: '07:00'
  - condition: time
    after: '19:00'

If that still doesn’t work, remove the conditions and see if it triggers at all

Other thing I’d do is replace

    value_template: '{{ states.device_tracker.mi_phone.attributes.rssi | int > -5   }}'

with

    value_template: '{{ state_attr("device_tracker.mi_phone","attributes.rssi") | int > -5   }}'

This way it will not error on null or not available

1 Like

Hi lolouk44. Thanks for the help.
I tried your suggestion and replaced my code with

value_template: '{{ state_attr("device_tracker.mi_phone","attributes.rssi") | int > -5 }}' and also changed the time as you suggested

It didn’t work.

Then I removed the conditions. The actions were triggered when i moved further away from the tracker.

So what happens is, when I move away from the area, the rssi value shows -19 and the automation got triggered.

So i decided to change the code a little. I replaced " int > -5 " to "int < -5 ". Then it doesntt get triggered at all.

I’m getting somewhere, but not there yet.

Hi. I have been digging a little and tried testing the template in http://hassio.local:8123/developer-tools/template.

I am really really confused as to how home assistant calculations work. For an example :
example 1 :
{{ state_attr("device_tracker.mi_phone", "rssi") }} returns -29
{{ state_attr("device_tracker.mi_phone", "rssi") | int > -10 }} returns true

example 2 :
{{ state_attr("device_tracker.mi_phone", "rssi") }} returns -26
{{ state_attr("device_tracker.mi_phone", "rssi") | int > -40 }} returns true

according to this. -29 is greater than -10. but -26 is also greater than -40.

how does this work???

p.s - just incase i wasn’t clear. i edited the code as follows : -

1. rssi value is {{state_attr("device_tracker.mi_phone", "rssi") }}
2. is it greater than -40 ? {{state_attr("device_tracker.mi_phone", "rssi") | int > -40 }}
3. is it greater than -5 ? {{state_attr("device_tracker.mi_phone", "rssi") | int > -5 }}

The result i got from the above is :-

  1. rssi value is (-19,)
  2. is it greater than -40 ? True
  3. is it greater than -5 ? True

p.s. 2 -

went on to test simpler instructions which seems correct.

simpler calculations :-
{{ 5 > 1}}
{{ 5 > 10}}
{{ -5 > -10}}
{{ -50 > -10}}

results in :
True
False
True
False

All I can think of at the moment is that there’s something else that’s not right in the data returned by your sensor.
Just did a test with my Freezer sensor:
{{states("sensor.kitchen_freezer_temperature")}} returns -15.3
{{states("sensor.kitchen_freezer_temperature") | int > -10}} returns False
{{states("sensor.kitchen_freezer_temperature") | int > -20}} returns True
I wonder if you somehow have a trailing space in your value that screws the conversion to an int?

What result do you get for this?
{{ state_attr("device_tracker.mi_phone", "rssi") | length }}
If your rssi value is -29, above should return 3…

1 Like

Hi. I managed to solve it. The problem was as follows:-

{{ state_attr("device_tracker.mi_phone","attributes.rssi") | int }} returns 0 irrespective of the value {{ state_attr("device_tracker.mi_phone","attributes.rssi") }} returns.
. so 0 is > than any negative numbers. Hence my solution is as follows:-

  alias: Hall Presence 2
  trigger:
  - platform: template
    value_template: >
      {% set rssi = state_attr("device_tracker.mi_phone", "rssi") | replace( ',)' , '' ) | replace( '(' , '')  %}
      {{ rssi | int > -8  }}
  action:
  - data:
      entity_id: switch.sonoff_10008a346b
    service: switch.turn_on
  - data:
      entity_id: switch.sonoff_10008a62a6
    service: switch.turn_on

I’m really confused with this.

Yes it would as your attribute is called rssi, not attribute.rssi, therefore you can put whatever you want in that 2nd quotes, if it’s not a valid attribute, it’ll always return 0…

Looks like you’ve edited your value _template as I was replying :wink:
This would suggest state_attr("device_tracker.mi_phone", "rssi") doesn’t not return a number but a string composed of a number between brackets, e.g. (-26)
If so then yes your value_template now makes sense and explains why your earlier tests failed as (-26) | int would return 0 all the time

1 Like

This would suggest state_attr("device_tracker.mi_phone", "rssi") doesn’t not return a number but a string composed of a number between brackets, e.g. (-26).

You’re right. Now i just have to figure out how to add more device trackers to the template.

you probably want to look at a bayesian binary sensor for this.
It takes a bit to understand, but it’s really powerful.
This article helped me understand how it works.

This might also be of interest to you

Very interesting. So you are using the Bluetooth built in on the raspberry pi to track presence in the living room, correct? It detects your phone, right?
How did you set that up? Can you please point me in the other direction.
I tried adding Bluetooth integration to Hassio, but I get nothing.

Hi. I gave up on it. Im no longer using hassio due to a network issue.

The Bluetooth detection is slow. If you expect your light to on as soon as you enter the room, forget it. You will need to sit in the dark room for about a minute or so before it detects.

And its really difficult to get the correct strength to distance.

Ah! I had my hopes high.
I guess I can try room assistnat with multiple Pis around the house instead. Thank you Anand.