SOLVED: Help with a strip() method

Hi,

I have following problem with the strip() method:

I want to remove the domain input_number. and _slider from the entity input_number.shutter_room_slider to get shutter_room, but I get an output without first letter hutter_room for shutter or nput_number.shutter_room.

DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=cover, service=set_cover_position, service_data=entity_id=cover.hutter_room_level, position=85>

the same behavior in in template editor

{% set entity_id = 'input_number.shutter_room_slider' %}

1: {{entity_id}}
2: {{entity_id.split('.')[1]}}
3: {{entity_id.split('.')[1].strip('_slider')}}
4: {{entity_id.strip('_slider')}}

image

you want to use replace not strip. strip strips leading and trailing characters in order that you provided. Which is why your stripping s or i or random characters from the front or back of your string.

{{ entity_id.replace('_slider','') }}
2 Likes

also if you really want to use strip, you’d only want to strip from the right, so you’d use rstrip

{{entity_id.rstrip('_slider')}}
1 Like

thanks, replace() method is even better! then I can replace it directly with _level.

{{ entity_id.replace('_slider','_level') }}  #for shutter_room_level

yah just realize that if you use replace and your entity_id looks like this input_number.shutter_room_slider_slider your output will be input_number.shutter_room_level_level