Stripping Info out of a Sensor

Hi All,

I’m wondering if there is a way that I can change the state that is given for a sensor. For example, I have an Allergy Index sensor that returns “6.9 index” and I want to be able to create a template that will show an image based on the value. So I want the state return to be an integer like “6.9” instead of the text return. This way I can use conditions to display the image I want.

Can anyone point me in the right direction?

Thanks in advance!

John

6.9 isn’t an integer :slight_smile:

I get what you mean though. This should replace " index" (including the leading space) in the string with nothing then convert it to a floating point number.

value_template: "{{ states('sensor.your_allergy_sensor') | replace ( ' index', '') | float }}"

You can use it to create a new template sensor, or use the template directly in your conditions.

2 Likes

If the text you wish to remove is always 6 characters long (one space character plus five letters in the word ‘index’) you can slice off the last 6 characters like this:

value_template: "{{ states('sensor.allergy')[:-6] | float }}"
2 Likes

Thank you @tom_l and @123, exactly what I was looking for!