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.
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.
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”).
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.