Separate string based on type (letter or numer)

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?

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

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?

Remove

 | float

From both templates.
You need strings not floats.

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.

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