-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolf75.yaml
130 lines (125 loc) · 4.6 KB
/
golf75.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
- id: track_golf_75
alias: Golf 7.5 - Update locatie
description: |
This automation does update a car location.
Requirements:
- An iPhone
- Focus while driving should be set, either automatically or via Bluetooth.
- Shortcuts app should be installed and 2 automations should be created:
1. When focus Driving is turned on -> select Home Assistant event: GOLF_75_START
2. When focus Driving is turned off -> select Home Assistant event: GOLF_75_STOP
Every {throttle_seconds} the location of the person who sent the start event is used as the car location. This is done until the stop event is sent.
This does only work if a defined Home Assistant person is driving the car, so no theft protection.
mode: restart
triggers:
- trigger: event
event_type: GOLF_75_START
id: start
- trigger: event
event_type: GOLF_75_STOP
id: stop
- trigger: homeassistant
event: start
id: restore
variables:
device_tracker_id: golf_7_5
throttle_seconds: 30
actions:
- alias: Collect some variables
variables:
user_id: >-
{% if trigger.event.context is defined %}
{{ trigger.event.context.user_id }}
{% else %}
null
{% endif %}
person: >-
{{
states.person
| selectattr("attributes.user_id", "eq", user_id)
| map(attribute="entity_id")
| first
| default("none")
}}
- choose:
- alias: Start event, person should be set as well
conditions:
- '{{ person != "none" }}'
- condition: trigger
id: start
sequence:
- alias: Repeat location updates until the automation is triggered with a stop event
repeat:
while: "{{ True }}"
sequence:
- alias: Set car location based on the location of the person
action: device_tracker.see
data:
dev_id: "{{ device_tracker_id }}"
gps:
- '{{ state_attr(person, "latitude")}}'
- '{{ state_attr(person, "longitude")}}'
gps_accuracy: >-
{{ state_attr(person, "gps_accuracy") }}
- alias: Throttle updates
delay:
seconds: "{{ throttle_seconds }}"
- alias: Stop event, person should be set as well
conditions:
- '{{ person != "none" }}'
- condition: trigger
id: stop
sequence:
- alias: Set final car location based on the location of the person
action: device_tracker.see
data:
dev_id: "{{ device_tracker_id }}"
gps:
- '{{ state_attr(person, "latitude")}}'
- '{{ state_attr(person, "longitude")}}'
gps_accuracy: >-
{{ state_attr(person, "gps_accuracy") }}
- alias: Restore event
conditions:
- condition: trigger
id: restore
sequence:
- alias: Set car location based on last known location from SQL sensor
action: device_tracker.see
data:
dev_id: "{{ device_tracker_id }}"
gps:
- '{{ states("sensor.golf_7_5_last_known_location").split(", ")[0] }}'
- '{{ states("sensor.golf_7_5_last_known_location").split(", ")[1] }}'
gps_accuracy: >-
{{ states("sensor.golf_7_5_last_known_location").split(", ")[2] }}
# sensor.golf_7_5_last_known_location is a SQL sensor with the following query:
#
# SELECT
# CONCAT(
# JSON_VALUE(state_attributes.shared_attrs, '$.latitude'),
# ', ',
# JSON_VALUE(state_attributes.shared_attrs, '$.longitude'),
# ', ',
# JSON_VALUE(state_attributes.shared_attrs, '$.gps_accuracy')
# ) as location
# FROM
# states
# LEFT JOIN
# state_attributes
# ON ( states.attributes_id = state_attributes.attributes_id )
# WHERE
# metadata_id =
# (
# SELECT
# metadata_id
# FROM
# states_meta
# WHERE
# entity_id = 'device_tracker.golf_7_5'
# )
# AND JSON_VALUE(state_attributes.shared_attrs, '$.latitude') IS NOT NULL
# AND JSON_VALUE(state_attributes.shared_attrs, '$.longitude') IS NOT NULL
# AND JSON_VALUE(state_attributes.shared_attrs, '$.gps_accuracy') IS NOT NULL
# ORDER BY
# state_id DESC LIMIT 1;