Mqtt messages with differen ID's, how to filter and avoid sensors without 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 %}