Mqtt messages with differen ID's, how to filter and avoid sensors without value

I have mqtt messages with differen ID’s and depending on the ID’s I want to give certain sensors a value or not.

Example:{“SN”:"00001"“v0”:“10”,“v1”:“20”,“v2”:“30”}
Example:{“SN”:"00002"“v0”:“10”,“v1”:“20”,“v2”:“30”}

I did something like this:

 - platform: mqtt
    name: "Message_SN_1"
    state_topic: "mqttsensor"
    value_template: |-
      {% if value_json["SN"] == "0001" %}
        {{ value_json["v1"]}}
      {% endif %}

problem is that this adds an empty value if it doesn’t match what is a problem in graphs

Is there an option to filter in a different way so I don’t create a sensor without a value ?

Please edit your post and format it correctly as per point 11 here.

But in answer to your question yes, you can retain the last value like this:

value_template: >
  {% if value_json["SN"] == "0001" %}
    {{ value_json["v1"] }}
  {% else %}
    {{ states('sensor.message_sn_1') }}
  {% endif %}

Ok thanks. But in this way I add a datapoint with the previous values if I understand correctly. Is there a way not to add a datapoint ?
If I would have SN 1 to 20 and I do the same for all different sensors I will add 19 fake datapoints for each sensor.

Try adding this then:

value_template: "{{ value_json["v1"] }}"
availability_template: "{{ value_json["SN"] == "0001" }}"

I get an error
Invalid config for [sensor.mqtt]: [availability_template] is an invalid option for [sensor.mqtt].

You currently have a single topic that receives payloads intended for several sensors. I suggest you use a “demultiplexer” automation to re-publish the payloads to separate topics. That allows each sensor to receive payloads via its own topic. The data points it receives will be exclusively the ones intended for it.

This automation receives a payload from mqttsensor and republishes it to the appropriate new topic corresponding to the value in SN.

- alias: 'mqttsensor demultiplexer'
  trigger:
  - platform: mqtt
    topic: mqttsensor
  action:
  - service: mqtt.publish
    data:
      topic: "mqttsensor/{{ trigger.payload_json.SN }}"
      payload: "{{ trigger.payload }}"

The sensor’s configuration is reduced to this:

 - platform: mqtt
    name: "Message_SN_1"
    state_topic: mqttsensor/0001
    value_template: '{{ value_json.v1 }}'
 - platform: mqtt
    name: "Message_SN_2"
    state_topic: mqttsensor/0002
    value_template: '{{ value_json.v2}}'

NOTE
I made an assumption that the SN_2 sensor uses the value of v2 in the payload. If that’s incorrect, change it to whatever it should be.

Ok thanks a lot will try this but looks a nice and clean solution.

If you find my proposal works to your satisfaction, I suggest you add retain: true to the automation’s service call. This will make the MQTT Broker store the published value (i.e. employ “retained messages”).

  action:
  - service: mqtt.publish
    data:
      topic: "mqttsensor/{{ trigger.payload_json.SN }}"
      payload: "{{ trigger.payload }}"
      retain: true

Whenever Home Assistant is restarted, at the moment it reconnects to the Broker it will receive the stored values that were published to mqttsensor/0001 and mqttsensor/0002.

If the values are not stored on the Broker, at the moment Home Assistant reconnects to the Broker, it will receive nothing for any of the sensors and report the value as unknown. It will continue to be unknown until the next payload is received. To avoid this undesirable behavior, it’s best to use retained messages.

Any progress to report?