How to replace text

I have an entity (sensor.p2000) which gives the status:

“P 2 BAD-04 BR gerucht (Broei) Ams Container Terminal Ruijgoordweg Amsterdam 13IBGBA 13ASGBA”

Is there a possibility to have text replaced automatically in this status?

  • ‘P’ should be replaced by ‘PRIO’
  • ‘BAD-04’ must be removed.
  • ‘BR’ must be replaced by ‘Brand’

I am fairly new to Home Assistant and have no idea how to go about this. Could you help me on my way?

{{ states('sensor.p2000') | replace("P", "PRIO",1) | replace("BAD-04 ", "") | replace("BR", "Brand", 1) }}
6 Likes

Ik heb deze code in mijn config.yaml geplaatst. Ik blijf echter een foutmelding krijgen. Wat doe ik fout?

  • platform: template
    sensors:
    monitorp2000:
    friendly_name: “monitor p2000”
    value_template:
    {{ states(‘sensor.p2000’) | replace(“P”, “PRIO”,1) |
    replace("BAD-03 ", “”) |
    replace("DV ", "Dienstverlening ") |
    replace("BAD-03 ", “”) |
    replace(“BR”, “Brand”, 1) }}

Ik heb het vervangen van text in de melding werkend gekregen. Probeer nu alleen nog de voertuig nummers er uit te krijgen die met TTS worden uitgesproken. Iemand dat al voor elkaar gekregen? In regio23 gaat het om 6 nummers.

English is not my first language either but this is an english forum.
Don’t expect someone to come, understand what you wrote and answer your question :wink:

Well, i’m trying to get some numbers out of the imported line from the plugin. I managed to remove some text as it is frequent information. The numbers change each time as a new line comes in. At the configured settings i get always a set of 6 numbers in the end, as i use TTS it pronounce the numbers aswell as it is unnecessary. I need a way to get rid of them.

You can probably use the regex_findall_index

| regex_findall_index(...)

But you’ll need some knowledge in regex.
This topic is closed by the way.
Have a look here

Thanks Olivier,

Well, i implented this, and now i get only the extracted number that i want to get rid of haha

regex is not easy :slight_smile:

When i use

{{ states('sensor.limburgnoordamb') | replace("A1", "Hoge Spoed",1) | regex_findall_index('\d+') | replace("MKA", "meldkamer ambulance", 1) }}

I get return

119775

as that is a number in the last imported line.

If i use

{{ states('sensor.limburgnoordamb') | replace("A1", "Hoge Spoed",1) | replace("regex_findall_index('\d+')", "") | replace("MKA", "meldkamer ambulance", 1) }}

i’ve got

Hoge Spoed Steenweg Wessem 119775

As return, including the number. Anything else i can try to get the number removed, as it changes each new import?

is it always at the end?

9 out of 10 times there are always numbers on the end indeed.

{{ 'Hoge Spoed Steenweg Wessem 119775' | regex_replace(find='[0-9]+$', replace='', ignorecase=False) }}

Hero! Thanks Olivier

Is it also possible to replace text for attributes instead of state? how would an example code of that look like?

Just replace the states() with state_attr()

states('something')state_attr('something','attribute')