Is there a way to filter mqtt data based on value upon reception (before further processing)?

Hello everyone,
I would like to filter out useless data upon reception.

The data is being received from Tasmota devices.

The idea is to ignore all incoming payloads if the value changes are small.

Here is an example rule on one of the tasmota devices, but I would like to process in Home Assistant instead as the processing in tasmota is not as “clean” as I would like.

I am currently filtering in tasmota using

Rule1 ON energy#power[1] DO IF (%value%>150) PowerDelta 130 ELSEIF (%value%>100) PowerDelta 120 ELSEIF (%value%>50) PowerDelta 110 ELSEIF (%value%>20) PowerDelta 105 ELSEIF (%value%<=20) PowerDelta 102 ENDIF ENDON

So depending on the energy range I am in, larger or smaller powerdeltas are needed before a payload is sent.

In Home Assistant I would instead need to do something similar.

Would that be possible? This should happen on-the-fly before the data is being written to the recorder (or at all visible to the system, so e.g. also not available to the graphs etc.). So ideally between the broker and the system.

Is that achievable somehow?

Thank you all for your ideas
Alex

I’m not sure if this is what you are after, but for an MQTT sensor, you can process the incoming single value payload using a value_template, so in your case, it might look something like:

     value_template: >-
       {% if value > 150 -%} 
         130
       {%- elif value >100 -%} 
         120
       {%- elif value >50 -%} 
         110
       {%- elif value >20 -%} 
         105
       {%- elif value <20 -%} 
         102
       {%- endif %}

If your payload is in JSON format, it will look a little different of course.

Thank you Tommy for your suggestion.
The expressions would in fact not be values because PowerDelta is a delta threshold between last and current reading. So any change smaller than x Watts will be ignored.

One of the problems is that my sensors are pblished via MQTT but since they are in fact Tasmota devices, they are added automatically by the Tasmota integration. So I do not define them manually.

So I think the processing needs to be before the recorder integration.