nebhale/teslamate-discovery

not_home and home in device_tracker usage ?

Closed this issue · 6 comments

Is the state topic latitude or location. I am confused. Please help with correct syntax

- id: '1694297530599'                                                                                                                                                                                       
  alias: WhiteTeslaGarage1EntersHome                                                                                                                                                                        
  description: WhiteTeslaTessYEntersHome                                                                                                                                                                    
  trigger:                                                                                                                                                                                                  
  - platform: state                                                                                                                                                                                         
    entity_id:                                                                                                                                                                                              
    - device_tracker.whitey                                                                                                                                                                                  
    attribute: location                                                                                                                                                                                     
    from: not_home                                                                                                                                                                                          
    to: home                                                                                                                                                                                                
  condition:                                                                                                                                                                                                
  - condition: state                                                                                                                                                                                        
    entity_id: sensor.whitey_shift_state                                                                                                                                                                     
    state: D                                                                                                                                                                                                
  action:                                                                                                                                                                                                   
  - device_id: 77cdb9bf620a4cfcb9988d358179b632                                                                                                                                                             
    domain: cover                                                                                                                                                                                           
    entity_id: 8489b221a3f9d853a4f101b3022e32ab                                                                                                                                                             
    type: open                                                                                                                                                                                          	

Is it possible that the state topic and Attribute topic should be /location instead of /latitude ?

ha.DeviceTracker{
			Device:                 device,
			Icon:                   "mdi:car",
			JSONAttributesTemplate: fmt.Sprintf(`{{ { "latitude": value | float(0), "longitude": states("sensor.%s_longitude") | float(0) } | to_json }}`, strcase.ToSnake(device.Name)),
			JSONAttributesTopic:    StateTopic(device, "/latitude"),
			Name:                   "",
			SourceType:             "gps",
			StateTopic:             StateTopic(device, "/latitude"),
			UniqueId:               UniqueId(device, "/location"),
			ValueTemplate:          fmt.Sprintf(`{{ "home" if "home" in (states("sensor.%s_geofence") | lower) else "not_home" }}`, strcase.ToSnake(device.Name)),
		},

This is possibly the most complicated thing in the whole utility, so not surprised this seems confusing. Some explanation:

A DeviceTracker in Home Assistant is a combination of latitude, longitude, and altitude. TeslaMate does not put all three of those things into a single MQTT topic so we have to synthesize that value ourselves. What we do is we pick one of them that will change and cause the re-calculation to happen; we set the StateTopic (which is the input to the DeviceTracker) to /latitude (it could have been /longitude, it doesn't really matter) and then use the JSONAttributesTemplate to take that value and combine it with the value of the other sensor (in this case /longitude) to create the information necessary for the DeviceTracker. That is then put in the UniqueId of /location which serves as the output.

To summarize, StateTopic (/latitude) is the input to do the calculation while UniqueId (/location) is the output where we put the result of the calculation. Does this make sense?

@nebhale
Thanks for the nice explanation. I am still struggling with the automation yml syntax on how to access the /location output so I can detect the transition from not_home to home (and viceversa)
I have two tesla modely -- one is called grayy and the other is called whitey

Here is the output from your discovery script. Can you help with the automation.yml syntax. Thanks

config/teslamate-discovery -u user1 -P pass1 -p 1883 -s tcp
Connecting to tcp://127.0.0.1:1883
Listing Vehicles
Configuring grayy
  homeassistant/sensor/teslamate_cars_1/charge_energy_added/config
  homeassistant/sensor/teslamate_cars_1/limit/config
  homeassistant/sensor/teslamate_cars_1/charger_current/config
  homeassistant/binary_sensor/teslamate_cars_1/charging/config
  homeassistant/binary_sensor/teslamate_cars_1/plug/config
  homeassistant/sensor/teslamate_cars_1/charger_power/config
  homeassistant/sensor/teslamate_cars_1/charger_voltage/config
  homeassistant/sensor/teslamate_cars_1/start_time/config
  homeassistant/sensor/teslamate_cars_1/time_to_charged/config
  homeassistant/sensor/teslamate_cars_1/inside_temp/config
  homeassistant/binary_sensor/teslamate_cars_1/climate/config
  homeassistant/binary_sensor/teslamate_cars_1/preconditioning/config
  homeassistant/sensor/teslamate_cars_1/outside_temp/config
  homeassistant/sensor/teslamate_cars_1/elevation/config
  homeassistant/sensor/teslamate_cars_1/geofence/config
  homeassistant/sensor/teslamate_cars_1/heading/config
  homeassistant/sensor/teslamate_cars_1/latitude/config
  homeassistant/device_tracker/teslamate_cars_1/location/config
  homeassistant/sensor/teslamate_cars_1/longitude/config
  homeassistant/sensor/teslamate_cars_1/power/config
  homeassistant/sensor/teslamate_cars_1/speed/config
  homeassistant/sensor/teslamate_cars_1/battery/config
  homeassistant/binary_sensor/teslamate_cars_1/charge_port/config
  homeassistant/binary_sensor/teslamate_cars_1/doors/config
  homeassistant/binary_sensor/teslamate_cars_1/frunk/config
  homeassistant/binary_sensor/teslamate_cars_1/health/config
  homeassistant/binary_sensor/teslamate_cars_1/locked/config
  homeassistant/binary_sensor/teslamate_cars_1/occupied/config
  homeassistant/sensor/teslamate_cars_1/odometer/config
  homeassistant/sensor/teslamate_cars_1/range/config
  homeassistant/binary_sensor/teslamate_cars_1/sentry_mode/config
  homeassistant/sensor/teslamate_cars_1/state/config
  homeassistant/sensor/teslamate_cars_1/tire_pressure_front_left/config
  homeassistant/sensor/teslamate_cars_1/tire_pressure_front_right/config
  homeassistant/sensor/teslamate_cars_1/tire_pressure_rear_left/config
  homeassistant/sensor/teslamate_cars_1/tire_pressure_rear_right/config
  homeassistant/binary_sensor/teslamate_cars_1/trunk/config
  homeassistant/binary_sensor/teslamate_cars_1/update/config
  homeassistant/binary_sensor/teslamate_cars_1/windows/config
  homeassistant/sensor/teslamate_cars_1/version/config
Configuring whitey
  homeassistant/sensor/teslamate_cars_2/charge_energy_added/config
  homeassistant/sensor/teslamate_cars_2/limit/config
  homeassistant/sensor/teslamate_cars_2/charger_current/config
  homeassistant/binary_sensor/teslamate_cars_2/charging/config
  homeassistant/binary_sensor/teslamate_cars_2/plug/config
  homeassistant/sensor/teslamate_cars_2/charger_power/config
  homeassistant/sensor/teslamate_cars_2/charger_voltage/config
  homeassistant/sensor/teslamate_cars_2/start_time/config
  homeassistant/sensor/teslamate_cars_2/time_to_charged/config
  homeassistant/sensor/teslamate_cars_2/inside_temp/config
  homeassistant/binary_sensor/teslamate_cars_2/climate/config
  homeassistant/binary_sensor/teslamate_cars_2/preconditioning/config
  homeassistant/sensor/teslamate_cars_2/outside_temp/config
  homeassistant/sensor/teslamate_cars_2/elevation/config
  homeassistant/sensor/teslamate_cars_2/geofence/config
  homeassistant/sensor/teslamate_cars_2/heading/config
  homeassistant/sensor/teslamate_cars_2/latitude/config
  homeassistant/device_tracker/teslamate_cars_2/location/config
  homeassistant/sensor/teslamate_cars_2/longitude/config
  homeassistant/sensor/teslamate_cars_2/power/config
  homeassistant/sensor/teslamate_cars_2/speed/config
  homeassistant/sensor/teslamate_cars_2/battery/config
  homeassistant/binary_sensor/teslamate_cars_2/charge_port/config
  homeassistant/binary_sensor/teslamate_cars_2/doors/config
  homeassistant/binary_sensor/teslamate_cars_2/frunk/config
  homeassistant/binary_sensor/teslamate_cars_2/health/config
  homeassistant/binary_sensor/teslamate_cars_2/locked/config
  homeassistant/binary_sensor/teslamate_cars_2/occupied/config
  homeassistant/sensor/teslamate_cars_2/odometer/config
  homeassistant/sensor/teslamate_cars_2/range/config
  homeassistant/binary_sensor/teslamate_cars_2/sentry_mode/config
  homeassistant/sensor/teslamate_cars_2/state/config
  homeassistant/sensor/teslamate_cars_2/tire_pressure_front_left/config
  homeassistant/sensor/teslamate_cars_2/tire_pressure_front_right/config
  homeassistant/sensor/teslamate_cars_2/tire_pressure_rear_left/config
  homeassistant/sensor/teslamate_cars_2/tire_pressure_rear_right/config
  homeassistant/binary_sensor/teslamate_cars_2/trunk/config
  homeassistant/binary_sensor/teslamate_cars_2/update/config
  homeassistant/binary_sensor/teslamate_cars_2/windows/config
  homeassistant/sensor/teslamate_cars_2/version/config

Specifically how to I access the derived device_tracker.whitey /location as a state entity?I do not see it listed in the list of entities

Ah, I see the confusion: the output you’re showing are the names of the MQTT topics that I’m putting data into, not the names of the sensors that are created by those MQTT config payloads. Might be a reasonable improvement to put that in too.

If, inside of Home Assistant, you go to Settings -> Devices & services -> Entities and put ‘device_tracker’ into the seach field, you should see the name of the sensor you’re looking for:

image

My prediction is that they’ll be called device_tracker.grayy and device_tracker.whitey.

I suspect the script only partially succeeded in that I see the "input" data using the homeassistant mqtt explorer add-on but don't see the "output" device_tracker.whitey entity in the GUI you referenced above. It never got created. Any idea why it worked for you and not me?

I had the wrong username/password pair in the mqtt integration. Once I fixed that, I can see the output entity is created. Thanks @nebhale for your help clarifying things

My pleasure! Good luck and I hope this works well for you.