Getting Rav4 Data in Home Assistant
Prerequisites
A Linux sever running python 3.6+
An (apache) web server
Pulling https://github.com/calmjm/tojota to the linux server
Changing the tojota code
I will not go into setting up the https://github.com/calmjm/tojota as such, but as the code does make date-marked files, I am changing the code to make a static filename instead. This must be done every time a new version is pulled from guthub.
These lines are mine (replacing existing lines):
parking_file = parking_path / 'parking.json'
remote_control_file = remote_control_path / 'remote_control.json'
making sure is a static file each time, that is just being overwritten.
I am running this code from cron to get the data from MyT:
cd /home/bnp
source /home/bnp/tojota/bin/activate
export PYTHONIOENCODING=utf8
cd /home/bnp/tojota
python tojota.py
Exposing data for the HA Rest service
I am making the files available on the local apache server; same server where Home Assitant runs:
➜ mytoyota ls -l
total 4
lrwxrwxrwx 1 root root 45 May 4 19:35 odometer.json -> /home/bnp/tojota/cache/odometer/odometer.json
lrwxrwxrwx 1 root root 43 May 5 07:26 parking.json -> /home/bnp/tojota/cache/parking/parking.json
lrwxrwxrwx 1 root root 57 May 4 19:35 remote_control.json -> /home/bnp/tojota/cache/remote_control/remote_control.json
➜ mytoyota
Output of code
The output looks like this:
[{"type":"mileage","value":34938,"unit":"km"},{"type":"Fuel","value":77.0}]
{"ReturnCode": "000000", "VehicleInfo": {"AcquisitionDatetime": "2022-05-08T01:17:15Z", "ChargeInfo": {"BatteryPowerSupplyPossibleTime": 16383, "ChargeEndTime": "42:35", "ChargeRemainingAmount": 100, "ChargeStartTime": "42:35", "ChargeType": 15, "ChargeWeek": 0, "ChargingStatus": "chargeComplete", "ConnectorStatus": 5, "EvDistanceInKm": 82.1, "EvDistanceWithAirCoInKm": 79.64, "EvTravelableDistance": 82.1, "EvTravelableDistanceSubtractionRate": 3, "GasolineTravelableDistance": 452, "GasolineTravelableDistanceUnit": 1, "PlugInHistory": 33, "PlugStatus": 45, "RemainingChargeTime": 65535, "SettingChangeAcceptanceStatus": 0}, "RemoteHvacInfo": {"BlowerStatus": 0, "FrontDefoggerStatus": 0, "InsideTemperature": 23, "LatestAcStartTime": "2022-04-27T05:50:09Z", "RearDefoggerStatus": 0, "RemoteHvacMode": 0, "RemoteHvacProhibitionSignal": 1, "SettingTemperature": 19.5, "TemperatureDisplayFlag": 1, "Temperaturelevel": 30}}}
{"event":{"lat":"57.010397","lon":"10.034043","timestamp":"1651936945000"},"tripStatus":"0"}
Fecthing the data via REST, using sensor.yaml:
name: Toyota RAV4 Fuel
resource: http://127.0.0.1/mytoyota/odometer.json
method: GET
value_template: '{{ value_json.1.value }}'
unit_of_measurement: Pct
scan_interval: 3000
- platform: rest
name: Toyota RAV4 Mileage
resource: http://127.0.0.1/mytoyota/odometer.json
method: GET
value_template: '{{ value_json.0.value }}'
unit_of_measurement: Km
scan_interval: 3000
- platform: rest
name: Toyota RAV4 Gasoline Travelable Distance
resource: http://127.0.0.1/mytoyota/remote_control.json
method: GET
value_template: '{{ value_json.VehicleInfo.ChargeInfo.GasolineTravelableDistance }}'
unit_of_measurement: Km
scan_interval: 3000
- platform: rest
name: Toyota RAV4 EV Travelable Distance (with EC)
resource: http://127.0.0.1/mytoyota/remote_control.json
method: GET
value_template: '{{ value_json.VehicleInfo.ChargeInfo.EvDistanceWithAirCoInKm }}'
unit_of_measurement: Km
scan_interval: 3000
- platform: rest
name: Toyota RAV4 EV Travelable Distance (without EC)
resource: http://127.0.0.1/mytoyota/remote_control.json
method: GET
value_template: '{{ value_json.VehicleInfo.ChargeInfo.EvTravelableDistance }}'
unit_of_measurement: Km
scan_interval: 3000
- platform: rest
name: Toyota RAV4 Inside Temperature
resource: http://127.0.0.1/mytoyota/remote_control.json
method: GET
value_template: '{{ value_json.VehicleInfo.RemoteHvacInfo.InsideTemperature }}'
unit_of_measurement: C
scan_interval: 3000
- platform: rest
name: Toyota RAV4 Charging Status
resource: http://127.0.0.1/mytoyota/remote_control.json
method: GET
value_template: '{{ value_json.VehicleInfo.ChargeInfo.ChargingStatus }}'
scan_interval: 3000
- platform: rest
name: Toyota RAV4 Parking
resource: http://127.0.0.1/mytoyota/parking.json
method: GET
value_template: '{{ value_json.result }}'
scan_interval: 3000
json_attributes_path: $.event
json_attributes:
- lat
- lon
Getting the GPS Data on a Device
In known_devices.yaml I create the car:
hide_if_away: false
icon: mdi:car
name: Toyota Rav4
track: true
And an automation set the lat and lon onto the device:
trigger:
- platform: event
event_type: state_changed
event_data:
entity_id: sensor.toyota_rav4_parking
condition: []
action:
- service: device_tracker.see
data:
dev_id: rav4
gps:
- '{{ state_attr(''sensor.toyota_rav4_parking'', ''lat'') }}'
- '{{ state_attr(''sensor.toyota_rav4_parking'', ''lon'') }}'
mode: single