Help with custom sensor

I’m currently working on a project to get my car’s data into HA based on mqtt.

The problem is that the VIN of the car is in the mqtt message: eg
weconnect/0/vehicles/VIN/domains/readiness/readinessStatus/connectionState/batteryPowerLevel

I would like to define the VIN as a variable in the mqtt message. My first try:

platform: template

  # substitutions:
  {% set vin = "VIN"%}
  {% set car = "id4"%}

  sensor:
    - platform: mqtt
      name: {% car %}_display_name
      state_topic: "weconnect/0/vehicles/" + {% vin %} + "/nickname"
      icon: mdi:car

I would like to define 2 variables: vin and car. The car variable will be used if people have 2 cars, so they can give each sensor a unique name. Otherwise we’ll have duplicate sensor names.
But my code gives errors and I have no clue.

Any idea how this can be accomplished?

It’s not possible. You have to configure them separately.

Sorry for the confusion: I know that you have to configure them separately in case when you have 2 cars.
So you have to copy everything and modify vin and car for the second car.

But my code is not even working with 1 car; I always get yaml errors and I don’t know the right syntax.

Your code is not possible, you can’t template like that. Templating can only be done inside a yaml field. You have to configure each sensor without variables, separately.

The only way to get a dynamic configuration like that to work is to implement an automation that outputs mqtt discovery information on startup to the correct mqtt discovery topics.