-
Notifications
You must be signed in to change notification settings - Fork 4
/
teslamate_mqtt_device_creation_script.yaml
381 lines (381 loc) · 17.8 KB
/
teslamate_mqtt_device_creation_script.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
## Teslamate Entity Autodiscovery Script
# v2.0.0 - Complete re-write; inspired by FrnchFrgg: https://community.home-assistant.io/t/why-cant-we-create-custom-devices/472371/14
#
# Instructions:
# 1. Edit line 21 with your discovery prefix
# 2. Add your vehicle on lines 27-33. If you have multiple cars, make additional list entries.
# (Optional:)
# 3. Modify, add, or remove any sensor starting on line 51. All entries under the "config:" key are documented here:
# https://www.home-assistant.io/integrations/sensor.mqtt/#configuration-variables
# In order to pass a template, the brackets must be commented out. Use the following example as a reference:
# value_template: "{{ '{{' }} value | float(0) * 1.234 {{ '}}' }}"
# 4. Create an automation to re-run this script whenever the software sensor is updated. This ensures the correct
# software version is shown on the MQTT device page for each vehicle.
#
script:
teslamate_entity_creator:
alias: 'Teslamate Entity Creator'
sequence:
- variables:
#Change this to your mqtt discovery prefix. This will be "homeassistant" unless you changed it in your HA config.
discovery_prefix: homeassistant
- repeat:
for_each:
## Add each car as a list here with the device_name, base_topic, and device parameters. The device_name and base_topic must be unique.
# Under the 'device:' key, enter any parameters desired except name, model, sw_version, and identifiers. Those will be created automatically.
# List of available parameters here: https://www.home-assistant.io/integrations/sensor.mqtt/#device
- device_name: Tesla Model 3
base_topic: teslamate/cars/1
device:
manufacturer: Tesla
suggested_area: Garage
configuration_url: https://teslamate.example.com
serial_number: VIN_NUMBER
sequence:
- variables:
base_topic: "{{ repeat.item.base_topic }}"
common: >
{{ dict(
[("device", dict(
repeat.item.device.items() | list
+ [("identifiers", [repeat.item.base_topic])]
+ [("name", repeat.item.device_name)])
)]
)
}}
model_sensor_entity_id: "{{ 'sensor.' ~ common.device.name | slugify ~ '_model' }}"
trim_badge_sensor_entity_id: "{{ 'sensor.' ~ common.device.name | slugify ~ '_trim_badge' }}"
software_version_sensor_entity_id: "{{ 'sensor.' ~ common.device.name | slugify ~ '_software_version' }}"
- repeat:
for_each:
- type: sensor
config:
name: Model
state_topic: "{{ base_topic ~ '/model' }}"
icon: mdi:earth
- type: sensor
config:
name: Software Version
state_topic: "{{ base_topic ~ '/version' }}"
icon: mdi:numeric
- type: sensor
config:
name: Trim Badge
state_topic: "{{ base_topic ~ '/trim_badging' }}"
icon: mdi:shield-star-outline
## Start of Binary Sensors ##
- type: binary_sensor
config:
name: Charger
state_topic: "{{ base_topic ~ '/state' }}"
device_class: battery_charging
value_template: '{{ "{{" }} "ON" if value == "charging" else "OFF" {{ "}}" }}'
- type: binary_sensor
config:
name: Climate
state_topic: "{{ base_topic ~ '/is_climate_on' }}"
device_class: running
icon: mdi:fan
payload_off: 'false'
payload_on: 'true'
- type: binary_sensor
config:
name: Charge Cable Connected
state_topic: "{{ base_topic ~ '/plugged_in' }}"
device_class: plug
payload_off: 'false'
payload_on: 'true'
- type: binary_sensor
config:
name: Preconditioning
state_topic: "{{ base_topic ~ '/is_preconditioning' }}"
device_class: running
icon: mdi:fan
payload_off: 'false'
payload_on: 'true'
- type: binary_sensor
config:
name: Charge Port Door
state_topic: "{{ base_topic ~ '/charge_port_door_open' }}"
device_class: door
icon: mdi:ev-plug-tesla
payload_off: 'false'
payload_on: 'true'
- type: binary_sensor
config:
name: Doors
state_topic: "{{ base_topic ~ '/doors_open' }}"
device_class: door
icon: mdi:car-door
payload_off: 'false'
payload_on: 'true'
- type: binary_sensor
config:
name: Frunk
state_topic: "{{ base_topic ~ '/frunk_open' }}"
device_class: door
icon: mdi:car
payload_off: 'false'
payload_on: 'true'
- type: binary_sensor
config:
name: Health
state_topic: "{{ base_topic ~ '/healthy' }}"
device_class: problem
icon: mdi:heart-pulse
payload_off: 'true'
payload_on: 'false'
- type: binary_sensor
config:
name: Locked
state_topic: "{{ base_topic ~ '/locked' }}"
device_class: lock
payload_off: 'true'
payload_on: 'false'
- type: binary_sensor
config:
name: Occupied
state_topic: "{{ base_topic ~ '/is_user_present' }}"
device_class: occupancy
icon: mdi:account
payload_off: 'false'
payload_on: 'true'
- type: binary_sensor
config:
name: Sentry Mode
state_topic: "{{ base_topic ~ '/sentry_mode' }}"
icon: mdi:cctv
payload_off: 'false'
payload_on: 'true'
- type: binary_sensor
config:
name: Trunk
state_topic: "{{ base_topic ~ '/trunk_open' }}"
device_class: door
icon: mdi:car
payload_off: 'false'
payload_on: 'true'
- type: binary_sensor
config:
name: Update Available
state_topic: "{{ base_topic ~ '/update_available' }}"
device_class: update
payload_off: 'false'
payload_on: 'true'
icon: mdi:alarm
- type: binary_sensor
config:
name: Windows Open
state_topic: "{{ base_topic ~ '/windows_open' }}"
icon: mdi:car-door
device_class: window
payload_off: 'false'
payload_on: 'true'
## Start of Sensors ##
- type: sensor
config:
name: Charge Energy Added
state_topic: "{{ base_topic ~ '/charge_energy_added' }}"
device_class: energy
unit_of_measurement: kWh
- type: sensor
config:
name: Charge Limit
state_topic: "{{ base_topic ~ '/charge_limit_soc' }}"
state_class: measurement
icon: mdi:battery-charging-90
unit_of_measurement: '%'
- type: sensor
config:
name: Charger Current
state_topic: "{{ base_topic ~ '/charger_actual_current' }}"
device_class: current
unit_of_measurement: A
- type: sensor
config:
name: Charger Power
state_topic: "{{ base_topic ~ '/charger_power' }}"
device_class: power
unit_of_measurement: kW
- type: sensor
config:
name: Charger Voltage
state_topic: "{{ base_topic ~ '/charger_voltage' }}"
device_class: voltage
unit_of_measurement: V
- type: sensor
config:
name: Scheduled Charging Start Time
state_topic: "{{ base_topic ~ '/scheduled_charging_start_time' }}"
device_class: timestamp
- type: sensor
config:
name: Last Update
state_topic: "{{ base_topic ~ '/since' }}"
device_class: timestamp
icon: mdi:clock-outline
- type: sensor
config:
name: Time Until Full Charge
state_topic: "{{ base_topic ~ '/time_to_full_charge' }}"
device_class: duration
unit_of_measurement: h
icon: mdi:timer
- type: sensor
config:
name: Inside Temperature
state_topic: "{{ base_topic ~ '/inside_temp' }}"
device_class: temperature
state_class: measurement
unit_of_measurement: °C
- type: sensor
config:
name: Outside Temperature
state_topic: "{{ base_topic ~ '/outside_temp' }}"
device_class: temperature
state_class: measurement
unit_of_measurement: °C
- type: sensor
config:
name: Elevation
state_topic: "{{ base_topic ~ '/elevation' }}"
unit_of_measurement: m
device_class: distance
icon: mdi:image-filter-hdr
- type: sensor
config:
name: Geofence
state_topic: "{{ base_topic ~ '/geofence' }}"
icon: mdi:earth
- type: sensor
config:
name: Heading
state_topic: "{{ base_topic ~ '/heading' }}"
icon: mdi:compass
unit_of_measurement: "°"
- type: sensor
config:
name: Latitude
state_topic: "{{ base_topic ~ '/latitude' }}"
icon: mdi:crosshairs-gps
unit_of_measurement: "°"
- type: sensor
config:
name: Longitude
state_topic: "{{ base_topic ~ '/longitude' }}"
icon: mdi:crosshairs-gps
unit_of_measurement: "°"
- type: sensor
config:
name: Power
state_topic: "{{ base_topic ~ '/power' }}"
device_class: power
unit_of_measurement: kW
- type: sensor
config:
name: Speed
state_topic: "{{ base_topic ~ '/speed' }}"
unit_of_measurement: km/h
device_class: speed
icon: mdi:speedometer
- type: sensor
config:
name: Battery Level
state_topic: "{{ base_topic ~ '/battery_level' }}"
state_class: measurement
unit_of_measurement: "%"
device_class: battery
- type: sensor
config:
name: Odometer
state_topic: "{{ base_topic ~ '/odometer' }}"
state_class: total_increasing
device_class: distance
unit_of_measurement: km
- type: sensor
config:
name: Battery Range
state_topic: "{{ base_topic ~ '/rated_battery_range_km' }}"
icon: mdi:map-marker-distance
state_class: measurement
device_class: distance
unit_of_measurement: km
- type: sensor
config:
name: State
state_topic: "{{ base_topic ~ '/state' }}"
icon: mdi:car-connected
- type: sensor
config:
name: Tire Pressure (Front Left)
state_topic: "{{ base_topic ~ '/tpms_pressure_fl' }}"
icon: mdi:car-tire-alert
state_class: measurement
device_class: pressure
unit_of_measurement: bar
- type: sensor
config:
name: Tire Pressure (Front Right)
state_topic: "{{ base_topic ~ '/tpms_pressure_fr' }}"
icon: mdi:car-tire-alert
state_class: measurement
device_class: pressure
unit_of_measurement: bar
- type: sensor
config:
name: Tire Pressure (Rear Left)
state_topic: "{{ base_topic ~ '/tpms_pressure_rl' }}"
icon: mdi:car-tire-alert
state_class: measurement
device_class: pressure
unit_of_measurement: bar
- type: sensor
config:
name: Tire Pressure (Rear Right)
state_topic: "{{ base_topic ~ '/tpms_pressure_rr' }}"
icon: mdi:car-tire-alert
state_class: measurement
device_class: pressure
unit_of_measurement: bar
## Start of Device Trackers ##
- type: device_tracker
config:
name: Location
state_topic: "{{ base_topic ~ '/geofence' }}"
icon: mdi:car
json_attributes_topic: "{{ base_topic ~ '/location' }}"
source_type: gps
value_template: "{{ '{{' }} 'home' if value | lower == 'home' else 'not_home' {{ '}}' }}"
sequence:
- if:
- condition: template
value_template: "{{ repeat.last == true }}"
then:
- wait_template: "{{ has_value(model_sensor_entity_id) and has_value(trim_badge_sensor_entity_id) and has_value(software_version_sensor_entity_id) }}"
timeout: "00:00:10"
continue_on_timeout: true
- variables:
common: >
{% if not repeat.last %}
{{ common }}
{% else %}
{{ dict(
[("device", dict(
common.device.items() | list
+ [("model", 'Model ' ~ states(model_sensor_entity_id) ~ ' ' ~ states(trim_badge_sensor_entity_id) )]
+ [("sw_version", states(software_version_sensor_entity_id) )])
)])
}}
{% endif %}
- service: mqtt.publish
data:
topic: >
{{ discovery_prefix }}/{{ repeat.item.type }}/{{ base_topic | slugify }}/{{ repeat.item.config.name | slugify }}/config
payload: >
{{ dict(
common.items() | list
+ repeat.item.config.items() | list
+ [("unique_id", repeat.item.config.state_topic ~ '//' ~ repeat.item.config.name | slugify)]
) | to_json
}}
retain: true