Wiki source code of Getting Rav4 Data in Home Assistant
Last modified by Normann P. Nielsen on 2022/11/08 15:40
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | {{success}}You can use [[https://github.com/DurgNomis-drol/ha_toyota]] instead, many of the same data (and more) are present. | ||
| 2 | |||
| 3 | As I am not into python programming, this is kind of a workaround; I assume that some small python code could extend the tojota code to a custom Home Assistant AddOn. | ||
| 4 | |||
| 5 | As the output is json, it can be imported into [[Splunk>>http://www.splunk.com/?ac=partner_netic]], [[Humio>>https://www.humio.com/]], Power BI, E(LK), [[DataDog Logs>>https://docs.datadoghq.com/logs/]] etc etc - pretty easy. Most of these just require a collector application/agent on the server.{{/success}} | ||
| 6 | |||
| 7 | |||
| 8 | |||
| 9 | = Prerequisites = | ||
| 10 | |||
| 11 | A Linux sever running python 3.6+ | ||
| 12 | |||
| 13 | An (apache) web server | ||
| 14 | |||
| 15 | Pulling [[https://github.com/calmjm/tojota]] to the linux server | ||
| 16 | |||
| 17 | = Changing the tojota code = | ||
| 18 | |||
| 19 | 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. | ||
| 20 | |||
| 21 | These lines are mine (replacing existing lines): | ||
| 22 | |||
| 23 | |||
| 24 | {{code}} | ||
| 25 | odometer_file = odometer_path / 'odometer.json' | ||
| 26 | |||
| 27 | parking_file = parking_path / 'parking.json' | ||
| 28 | |||
| 29 | remote_control_file = remote_control_path / 'remote_control.json' | ||
| 30 | {{/code}} | ||
| 31 | |||
| 32 | making sure is a static file each time, that is just being overwritten. | ||
| 33 | |||
| 34 | I am running this code from cron to get the data from MyT: | ||
| 35 | |||
| 36 | |||
| 37 | {{code language="bash"}} | ||
| 38 | #!/bin/bash | ||
| 39 | |||
| 40 | cd /home/bnp | ||
| 41 | source /home/bnp/tojota/bin/activate | ||
| 42 | export PYTHONIOENCODING=utf8 | ||
| 43 | cd /home/bnp/tojota | ||
| 44 | python tojota.py | ||
| 45 | {{/code}} | ||
| 46 | |||
| 47 | {{info}}[[https://github.com/calmjm/tojota]] also downloads Trips, but I have found no usage for the currently in Home Assistant..{{/info}} | ||
| 48 | |||
| 49 | = Exposing data for the HA Rest service = | ||
| 50 | |||
| 51 | I am making the files available on the local apache server; same server where [[Home Assitant>>Main.information-technology.home-assistant.home-assistant-on-docker]] runs: | ||
| 52 | |||
| 53 | |||
| 54 | {{code}} | ||
| 55 | ➜ tojota git:(master) ✗ cd /var/www/html/mytoyota | ||
| 56 | ➜ mytoyota ls -l | ||
| 57 | total 4 | ||
| 58 | lrwxrwxrwx 1 root root 45 May 4 19:35 odometer.json -> /home/bnp/tojota/cache/odometer/odometer.json | ||
| 59 | lrwxrwxrwx 1 root root 43 May 5 07:26 parking.json -> /home/bnp/tojota/cache/parking/parking.json | ||
| 60 | lrwxrwxrwx 1 root root 57 May 4 19:35 remote_control.json -> /home/bnp/tojota/cache/remote_control/remote_control.json | ||
| 61 | ➜ mytoyota | ||
| 62 | |||
| 63 | |||
| 64 | {{/code}} | ||
| 65 | |||
| 66 | = Output of code = | ||
| 67 | |||
| 68 | The output looks like this: | ||
| 69 | |||
| 70 | |||
| 71 | {{code language="odometer.json "}} | ||
| 72 | ➜ odometer git:(master) ✗ cat odometer.json | ||
| 73 | [{"type":"mileage","value":34938,"unit":"km"},{"type":"Fuel","value":77.0}] | ||
| 74 | {{/code}} | ||
| 75 | |||
| 76 | |||
| 77 | {{code language="remote_control.json "}} | ||
| 78 | ➜ remote_control git:(master) ✗ cat remote_control.json | ||
| 79 | {"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}}} | ||
| 80 | {{/code}} | ||
| 81 | |||
| 82 | |||
| 83 | {{code language="parking.json"}} | ||
| 84 | ➜ parking git:(master) ✗ cat parking.json | ||
| 85 | {"event":{"lat":"57.010397","lon":"10.034043","timestamp":"1651936945000"},"tripStatus":"0"} | ||
| 86 | {{/code}} | ||
| 87 | |||
| 88 | |||
| 89 | |||
| 90 | Fecthing the data via REST, using sensor.yaml: | ||
| 91 | |||
| 92 | |||
| 93 | {{code}} | ||
| 94 | - platform: rest | ||
| 95 | name: Toyota RAV4 Fuel | ||
| 96 | resource: http://127.0.0.1/mytoyota/odometer.json | ||
| 97 | method: GET | ||
| 98 | value_template: '{{ value_json.1.value }}' | ||
| 99 | unit_of_measurement: Pct | ||
| 100 | scan_interval: 3000 | ||
| 101 | |||
| 102 | - platform: rest | ||
| 103 | name: Toyota RAV4 Mileage | ||
| 104 | resource: http://127.0.0.1/mytoyota/odometer.json | ||
| 105 | method: GET | ||
| 106 | value_template: '{{ value_json.0.value }}' | ||
| 107 | unit_of_measurement: Km | ||
| 108 | scan_interval: 3000 | ||
| 109 | |||
| 110 | - platform: rest | ||
| 111 | name: Toyota RAV4 Gasoline Travelable Distance | ||
| 112 | resource: http://127.0.0.1/mytoyota/remote_control.json | ||
| 113 | method: GET | ||
| 114 | value_template: '{{ value_json.VehicleInfo.ChargeInfo.GasolineTravelableDistance }}' | ||
| 115 | unit_of_measurement: Km | ||
| 116 | scan_interval: 3000 | ||
| 117 | |||
| 118 | - platform: rest | ||
| 119 | name: Toyota RAV4 EV Travelable Distance (with EC) | ||
| 120 | resource: http://127.0.0.1/mytoyota/remote_control.json | ||
| 121 | method: GET | ||
| 122 | value_template: '{{ value_json.VehicleInfo.ChargeInfo.EvDistanceWithAirCoInKm }}' | ||
| 123 | unit_of_measurement: Km | ||
| 124 | scan_interval: 3000 | ||
| 125 | |||
| 126 | - platform: rest | ||
| 127 | name: Toyota RAV4 EV Travelable Distance (without EC) | ||
| 128 | resource: http://127.0.0.1/mytoyota/remote_control.json | ||
| 129 | method: GET | ||
| 130 | value_template: '{{ value_json.VehicleInfo.ChargeInfo.EvTravelableDistance }}' | ||
| 131 | unit_of_measurement: Km | ||
| 132 | scan_interval: 3000 | ||
| 133 | |||
| 134 | - platform: rest | ||
| 135 | name: Toyota RAV4 Inside Temperature | ||
| 136 | resource: http://127.0.0.1/mytoyota/remote_control.json | ||
| 137 | method: GET | ||
| 138 | value_template: '{{ value_json.VehicleInfo.RemoteHvacInfo.InsideTemperature }}' | ||
| 139 | unit_of_measurement: C | ||
| 140 | scan_interval: 3000 | ||
| 141 | |||
| 142 | - platform: rest | ||
| 143 | name: Toyota RAV4 Charging Status | ||
| 144 | resource: http://127.0.0.1/mytoyota/remote_control.json | ||
| 145 | method: GET | ||
| 146 | value_template: '{{ value_json.VehicleInfo.ChargeInfo.ChargingStatus }}' | ||
| 147 | scan_interval: 3000 | ||
| 148 | |||
| 149 | - platform: rest | ||
| 150 | name: Toyota RAV4 Parking | ||
| 151 | resource: http://127.0.0.1/mytoyota/parking.json | ||
| 152 | method: GET | ||
| 153 | value_template: '{{ value_json.result }}' | ||
| 154 | scan_interval: 3000 | ||
| 155 | json_attributes_path: $.event | ||
| 156 | json_attributes: | ||
| 157 | - lat | ||
| 158 | - lon | ||
| 159 | {{/code}} | ||
| 160 | |||
| 161 | |||
| 162 | |||
| 163 | = Getting the GPS Data on a Device = | ||
| 164 | |||
| 165 | Taken from [[https://community.home-assistant.io/t/create-a-device-tracker-from-sensor-template-gps-coordinates-from-rest-api/93993]] | ||
| 166 | |||
| 167 | In known_devices.yaml I create the car: | ||
| 168 | |||
| 169 | |||
| 170 | {{code}} | ||
| 171 | rav4: | ||
| 172 | hide_if_away: false | ||
| 173 | icon: mdi:car | ||
| 174 | name: Toyota Rav4 | ||
| 175 | track: true | ||
| 176 | {{/code}} | ||
| 177 | |||
| 178 | |||
| 179 | |||
| 180 | And an automation set the lat and lon onto the device: | ||
| 181 | |||
| 182 | |||
| 183 | {{code}} | ||
| 184 | alias: Update Car Tracker | ||
| 185 | trigger: | ||
| 186 | - platform: event | ||
| 187 | event_type: state_changed | ||
| 188 | event_data: | ||
| 189 | entity_id: sensor.toyota_rav4_parking | ||
| 190 | condition: [] | ||
| 191 | action: | ||
| 192 | - service: device_tracker.see | ||
| 193 | data: | ||
| 194 | dev_id: rav4 | ||
| 195 | gps: | ||
| 196 | - '{{ state_attr(''sensor.toyota_rav4_parking'', ''lat'') }}' | ||
| 197 | - '{{ state_attr(''sensor.toyota_rav4_parking'', ''lon'') }}' | ||
| 198 | mode: single | ||
| 199 | {{/code}} |