I have a VW e-GOLF and I wanna get the odometer figure via mqtt & Meatpi Wican dongle into HA & evcc.
That has to be done in configuration.yaml in the mqtt section.
If have tried the jinja2 template engine but I could not figure it out for hours.
i have found out that the conversion to hex can be achieved this way
{{ "%0x" | format(226) }} => Result: e2
{{ "%0x" | format(153) }} => Result: 99
combined in jinja2 by this with the proper result of hex “e299”
{{ "%0x" | format(226) }}{{ "%0x" | format(153) }} => Result: e299
And that result “e299” can then be converted as a whole to decimal by
{{("e2a5" | int(base=16))}} => Result 58009 km which is right
So far so good,
but how can I achieve that in my configuration.yaml in its mqtt section ?
The WiCAN odb2 dongle sends this reply in canbus format
{"bus":"0","type":"rx","ts":33190,"frame":[{"id":1918,"dlc":8,"rtr":false,"extd":false,"data":[6,98,34,3,0,226,153,170]}]}
Key data are here in the brackets the elements marked as V1 (226 dec) and V2 (153 dec) here below
V1 V2
[6,98,34,3,0,226,153,170]
I have tried it in ninja2 evaluation template but it didnt work out even with figures 226 and 153 - the right pane went into white and I got an error in the evaluation page.
Here is my mqqt sensor from the configuration.yaml where I am struggling for hours.
- name: "GOLF KM V"
unique_id: "golf_km_v"
state_topic: "wican/12345678901234/can/rx"
# state_class: "measurement"
# unit_of_measurement: "km"
value_template: >-
{% if value_json.frame[0].id == 1918 %}
{% set PID = value_json.frame[0].data[3] %}
{% if PID == 3 %}
{% set v1 = {{ "%0x" | format(value_json.frame[0].data[5])}} %}
{% set v2 = {{ "%0x" | format(value_json.frame[0].data[6])}} %}
{% set tmphex = {{ (v1v2) }} %}
{% set v = {{ (tmphex | int(base=16) }} %}
{{ (v) }}
{% endif %}
{% endif %}
I hope someone could the issue and explain to what went wrong.
The whole thing is triggered by an automation that detects if the Dongle is in the WLAN (online). From then onwards the automation hammers every 10 seconds the request for SOC, RANGE and ODOMETER.
Thanks a lot
BTW: The code is an example of getting VW GOLF Mark VII or e-GOLF data into HomeAssistant and then also into EVCC (charging / wallbox control).
For example SOC and range are already working via mqtt (they do not require any hex decimal conversions cause the values are already proper decimals), but the odometer is a nightmare compared to the extraction