beffin
(beffin)
December 4, 2021, 4:50pm
1
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?
Hellis81
(Hellis81)
December 4, 2021, 5:13pm
2
{{ states('sensor.p2000') | replace("P", "PRIO",1) | replace("BAD-04 ", "") | replace("BR", "Brand", 1) }}
7 Likes
beffin
(beffin)
December 5, 2021, 1:16pm
3
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) }}
Poes
(David)
December 23, 2022, 10:45am
4
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.
Olivier1974
(Olivier Toussaint)
December 23, 2022, 1:08pm
5
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
Poes
(David)
December 23, 2022, 1:34pm
6
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.
Olivier1974
(Olivier Toussaint)
December 23, 2022, 1:40pm
7
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
Poes
(David)
December 23, 2022, 4:06pm
8
Thanks Olivier,
Well, i implented this, and now i get only the extracted number that i want to get rid of haha
Poes
(David)
December 23, 2022, 4:49pm
10
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?
Poes
(David)
December 23, 2022, 4:52pm
12
9 out of 10 times there are always numbers on the end indeed.
Olivier1974
(Olivier Toussaint)
December 23, 2022, 4:57pm
13
{{ 'Hoge Spoed Steenweg Wessem 119775' | regex_replace(find='[0-9]+$', replace='', ignorecase=False) }}
Is it also possible to replace text for attributes instead of state? how would an example code of that look like?
Hellis81
(Hellis81)
January 15, 2023, 11:35am
16
Just replace the states()
with state_attr()
Olivier1974
(Olivier Toussaint)
January 17, 2023, 2:08pm
17
states('something')
→ state_attr('something','attribute')
jasongh
(Jason Greve Holbech)
September 11, 2024, 7:53pm
18
Hi, this is my first time trying to use yaml.
I am trying to remove “-” from a date, since the integration with Homey isn´t accepting “-” in a date.
I used your script above and edited it with my entity etc. But Home Assistant returns with missing integrations. Template, platform and sensors. I added it directly to the configuration file. What am I missing?
Thanks in advance
´#Remove - from ecowater date
platform: template
sensors:
ecowater_out_of_salt_on:*
friendly_name: “Ecowater Out Of Salt On”*
value_template: "{{states(‘sensor.ecowater_out_of_salt_on’) replace(‘-’, ‘’)}}´*