Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- alias: Virtuel PHEV Tracker
- description: >-
- Kombinerer bilens egen GPS med telefoners GPS (ved kørsel) for at sikre
- hurtigere opdateringer. Bruger Bluetooth/WiFi til at detektere føreren og en
- Bayesian-sensor til at bekræfte 'Hjemme'-status for at undgå GPS-drift.
- # ------------------------------------------------------------------------------
- # VIRTUAL PHEV TRACKER - ADVANCED VERSION
- # ------------------------------------------------------------------------------
- # This automation creates a virtual device_tracker that intelligently switches
- # between multiple GPS sources (Car native GPS vs. Driver phones).
- #
- # PREREQUISITES:
- # 1. A Bayesian binary_sensor (e.g., binary_sensor.yeti_presence) for home detection.
- # 2. A native car integration (e.g., device_tracker.yeti_location).
- # 3. Bluetooth MAC addresses for the car's dongles/headunit.
- # ------------------------------------------------------------------------------
- triggers:
- # 1. Trigger when "Car is Home" confidence changes (Bayesian)
- - trigger: state
- entity_id: binary_sensor.yeti_presence
- to: null
- id: bayesian
- # 2. Trigger when Primary Driver's phone moves
- - trigger: state
- entity_id: device_tracker.jacob_nothing
- attribute: latitude
- id: jacob
- # 3. Trigger when Native Car GPS updates
- - trigger: state
- entity_id: device_tracker.yeti_location
- attribute: latitude
- id: car
- conditions: []
- actions:
- - variables:
- # ========================================================================
- # CONFIGURATION SECTION - EDIT THIS
- # ========================================================================
- name: yeti # The name of this virtual device
- fallback_entity: device_tracker.yeti_location # The car's native integration
- target_macs: # Bluetooth MACs indicating "In Car"
- - 00:92:A5:5D:E2:E1
- - 68:8F:C9:8B:B0:99
- - 30:95:E3:E9:9C:FF
- # ========================================================================
- # LOGIC & CALCULATIONS (DRY)
- # ========================================================================
- # --- Dynamic Entity ID ---
- # Constructs 'device_tracker.yeti' automatically from the name variable
- this_entity_id: "{{ 'device_tracker.' ~ name }}"
- # --- Home Presence ---
- is_confirmed_home: "{{ is_state('binary_sensor.yeti_presence', 'on') }}"
- # --- Driver Detection (Bluetooth) ---
- jacob_bt_devices: >
- {{ state_attr('sensor.jacob_nothing_bluetooth_connection', 'connected_paired_devices') }}
- jacob_driving: >
- {{ jacob_bt_devices | select('search', target_macs | join('|')) | list | length > 0 }}
- # --- Driver Detection (WiFi SSID) ---
- rikke_driving: "{{ is_state('sensor.iphone_3_ssid', 'CAR2-ED79') }}"
- # --- Timestamps for Comparison ---
- # Uses the dynamic 'this_entity_id' to check self-state
- virtuel_last_updated: |
- {{ as_timestamp(states[this_entity_id].last_updated) | default(0) }}
- car_last_updated: |
- {{ as_timestamp(states[fallback_entity].last_updated) | default(0) }}
- # --- Source Selection Strategy ---
- # 1. If driving (phone connected to car) -> Use Phone GPS
- # 2. If parked -> Use Car GPS (fallback_entity) if it has new data
- # 3. Else -> Keep current state (this_entity_id)
- gps_source_entity: |
- {% if jacob_driving %}
- device_tracker.jacob_nothing
- {% elif rikke_driving %}
- device_tracker.iphone_3
- {% else %}
- {{ fallback_entity if car_last_updated > virtuel_last_updated else this_entity_id }}
- {% endif %}
- # --- Data Normalization ---
- # Handles differences between iOS 'Location' list and Standard 'latitude' attributes
- latitude: >
- {{ state_attr(gps_source_entity, 'Location')[0] if rikke_driving else state_attr(gps_source_entity, 'latitude') }}
- longitude: >
- {{ state_attr(gps_source_entity, 'Location')[1] if rikke_driving else state_attr(gps_source_entity, 'longitude') }}
- # Convert m/s to km/h
- current_speed: "{{ (state_attr(gps_source_entity, 'speed') | float(0) * 3.6) | round(1) }}"
- current_course: "{{ state_attr(gps_source_entity, 'course') | default(0) }}"
- current_altitude: "{{ state_attr(gps_source_entity, 'altitude') | default(0) }}"
- # Pass-through sensors
- fuel_level: "{{ states('sensor.yeti_fuel_level') }}"
- ev_battery: "{{ states('sensor.yeti_ev_battery_level') }}"
- alias: Definér variabler
- # --- OPTIMIZATION FILTER ---
- # Only continue if the trigger matches the currently selected source
- # or if the bayesian sensor (Home Logic) triggered the update.
- - condition: template
- value_template: |
- {{ trigger.id == 'bayesian' or
- trigger.entity_id == gps_source_entity or
- gps_source_entity == fallback_entity }}
- enabled: true
- # --- UPDATE VIRTUAL TRACKER ---
- - action: device_tracker.see
- data:
- dev_id: "{{ name }}"
- # Force location name "home" if Bayesian sensor confirms it
- location_name: "{{ 'home' if is_confirmed_home else omit }}"
- source_type: gps
- # Force zone.home coordinates if confirmed home (Anti-Drift)
- gps: |
- {% if is_confirmed_home %}
- {{ [state_attr('zone.home', 'latitude'), state_attr('zone.home', 'longitude')] }}
- {% else %}
- {{ [latitude, longitude] }}
- {% endif %}
- gps_accuracy: "{{ 10 if is_confirmed_home else state_attr(gps_source_entity, 'gps_accuracy') | default(50) }}"
- battery: "{{ states('sensor.yeti_car_battery_level') }}"
- attributes:
- speed: "{{ current_speed }}"
- course: "{{ current_course }}"
- altitude: "{{ current_altitude }}"
- fuel_level: "{{ fuel_level }}"
- ev_battery: "{{ ev_battery }}"
- last_source: "{{ gps_source_entity }}"
- # Status logic: If source is 'self', we are waiting for external data
- status: "{{ 'Parkeret (Venter på bil-data)' if gps_source_entity == this_entity_id else 'Opdateret' }}"
- mode: restart
Advertisement
Add Comment
Please, Sign In to add comment