MQTT value_template sensor between 2 values

Hi everyone,

I have devices which post their current state to an MQTT topic e.g. temperature. Every now and again the temperature state it unrealistic (e.g. 999 degrees) and it negatively impacts my historical data.

I use my own MQTT discovery sensors for a lot of things and would like to get the value_template entry correct so values between a certain range e.g. 0-50 degrees are accepted but if not it writes 0 if the value is less than 0 and 50 if the value is above 50.

Been trying for a few hours but stumped.

value_template: "{{ ([0,value_json,50]|sort)[1] }}"
1 Like

Genius! Thanks very much @tom_l

1 Like