tesla-local-control / tesla_ble_mqtt_core

Core content for the tesla_ble tools
Apache License 2.0
2 stars 2 forks source link

Fix presence detection can not be disabled #117

Open g4rb4g3 opened 1 week ago

g4rb4g3 commented 1 week ago

Since I'm using Tesla Fleet API integration in Home Assistant to detect which vehicle is home and connected I wanted to disable the presence detection. Turned out there was some logic included but in the end it did not work since the default value was taken whenever 0 was specified for PRESENCE_DETECTION_LOOP_DELAY or PRESENCE_DETECTION_TTL. Also it was confusing for me that the PRESENCE_DETECTION_TTL was used to determine if the presence loop has to be started so I changed it to use PRESENCE_DETECTION_LOOP_DELAY since that one is the actual value that is used for the sleep.

raphmur commented 1 week ago

@baylanger please advise I think we can indeed put a general toggle switch which disables presence detection. The rest of the settings would be more "expert oriented"

baylanger commented 1 week ago

The reason why TTL was picked to disable presence detection; IF the TTL is 0 it's worthless to do the detection since this disable caching of the entry. The documentation is pretty clear on this.

Regardless of the doc and make it easier for everyone, we'll introduce a bool strictly used to enable or disable detection.

I'm a bit confused why you say in the end it did not work since the default value was taken whenever 0 was specified for PRESENCE_DETECTION_LOOP_DELAY or PRESENCE_DETECTION_TTL. - @g4rb4g3 Let me know if I missed something.

% unset PRESENCE_DETECTION_LOOP_DELAY
% PRESENCE_DETECTION_LOOP_DELAY=${PRESENCE_DETECTION_LOOP_DELAY:-120}
% echo $PRESENCE_DETECTION_LOOP_DELAY
120
% PRESENCE_DETECTION_LOOP_DELAY=0
% PRESENCE_DETECTION_LOOP_DELAY=${PRESENCE_DETECTION_LOOP_DELAY:-120}
% echo $PRESENCE_DETECTION_LOOP_DELAY
0
g4rb4g3 commented 1 week ago

The reason why TTL was picked to disable presence detection; IF the TTL is 0 it's worthless to do the detection since this disable caching of the entry. The documentation is pretty clear on this.

To me it is confusing to check PRESENCE_DETECTION_TTL but use the PRESENCE_DETECTION_LOOP_DELAY inside the if. So I think it is more clear to use PRESENCE_DETECTION_LOOP_DELAY since it is the one that controls whole detection loop. I did not check it in detail but on a quick look it seems like TTL is used to set EPOCH_EXPIRE_TIME only but does not affect how often the loop is actually running.

Regardless of the doc and make it easier for everyone, we'll introduce a bool strictly used to enable or disable detection.

I'm fine if you add a bool to disable the presence detection, on the other hand it is pretty clear that 0 disables it.

I'm a bit confused why you say in the end it did not work since the default value was taken whenever 0 was specified for PRESENCE_DETECTION_LOOP_DELAY or PRESENCE_DETECTION_TTL. - @g4rb4g3 Let me know if I missed something.

% unset PRESENCE_DETECTION_LOOP_DELAY
% PRESENCE_DETECTION_LOOP_DELAY=${PRESENCE_DETECTION_LOOP_DELAY:-120}
% echo $PRESENCE_DETECTION_LOOP_DELAY
120
% PRESENCE_DETECTION_LOOP_DELAY=0
% PRESENCE_DETECTION_LOOP_DELAY=${PRESENCE_DETECTION_LOOP_DELAY:-120}
% echo $PRESENCE_DETECTION_LOOP_DELAY
0

Whenever PRESENCE_DETECTION_LOOP_DELAY or PRESENCE_DETECTION_TTL is set to 0 the default value was used for me. I'm using tesla-local-control/tesla_ble_mqtt_docker on an RPi 4 and when I provided 0 for both values in the stack.env it used 120/240 instead. Seems like an empty string and 0 are both treated falsy and therefor the default value is used.

This is what I found in the logs when I set them to 0

Configuration Options are:
  BLE_CMD_RETRY_DELAY=5
  DEBUG=true
  MQTT_SERVER=Not Shown
  MQTT_PORT=1883
  MQTT_PASSWORD=Not Shown
  MQTT_USERNAME=tesla-ble
  PRESENCE_DETECTION_LOOP_DELAY=120
  PRESENCE_DETECTION_TTL=240
  VIN_LIST=Not Shown
  ENABLE_HA_FEATURES=true
baylanger commented 1 week ago

Somehow I was positive you were using the addon not the Docker one. I’m guessing there’s someone thing on the docker side. I’m away, will check when I get a chance.

thanx for your feedbacks & response.