karesz
(Karesz)
June 26, 2025, 12:20pm
1
Hi Folks!
I’ve a sensor wich can has such values as:
B1.7
A3.5
M10.2
B2
etc
So i have:
a letter - 1-2 number - maybe a dot - if there was a dot an other number
How can i separate the letter as a value of a new sensor and
how can i separate the number as an other new sensor?
I think .split will not work because the place of the dot is not sure
truncate may cut the first character as a sensor value but i can not make it working.
Do you have any idea please?
tom_l
June 26, 2025, 12:28pm
2
Is it always one letter in front?
If so, just slice the string. Have a play with this in the template editor.
{% set value = 'M10.2' %}
{{ value[:1] }} # first character only
{{ value[1:] }} # everything after the first character
karesz
(Karesz)
June 26, 2025, 3:44pm
3
Thank you tom_l for your fast answer.
This solution is exactly what i need. In template editor it works as you wrote but in configuration.yaml it is ‘unknown’
template:
# szenzor szeletelő
- trigger:
- platform: homeassistant
event: start
- platform: state
entity_id: sensor.betuszam
sensor:
- name: "betu"
state: >
{% set value = states('sensor.betuszam') | float %}
{{ value[:1] }}
- name: "szam"
state: >
{% set value = states('sensor.betuszam') | float %}
{{ value[1:] }}
What did i miss?
Hellis81
(Hellis81)
June 26, 2025, 5:22pm
4
Remove
| float
From both templates.
You need strings not floats.
123
(Taras)
June 26, 2025, 6:18pm
5
template:
# szenzor szeletelő
- trigger:
- platform: state
entity_id: sensor.betuszam
sensor:
- name: "betu"
state: "{{ trigger.to_state.state[:1] }}"
- name: "szam"
state: "{{ trigger.to_state.state[1:] }}"
There’s no need for the homeassistant
trigger detecting startup. A Trigger-based Template Sensor’s value is automatically restored on startup.
karesz
(Karesz)
June 27, 2025, 5:22am
6
Thank you for your help tom_l , Hellis81 and Taras
It is working!
I mark Taras ’s answare as solution because it has code too
1 Like