Split sensor state with delimiter

Would it be possible to parse a value from a sensor state to two different values that can be used in an automation script?

The sensor state could be as follows:
“88; Your CPU has reach a temperature of 88 degree”
and the delimiter will always be a semicolon

Value 1 should then be “88” and Value 2 should be “Your CPU has reach a temperature of 88 degree”

The script I am working on is the following:

- id: '1541111197749'
  alias: PC Alarm
  trigger:
  - entity_id: sensor.file_value #This is where I would like  Value 1 in numeric format
    platform: state
  condition:
    condition: numeric_state
    entity_id: sensor.file_value
    above: 70
  action:
    - service: media_player.play_media
      entity_id: media_player.kontor
      data_template:
        media_content_type: 'audio/mp3'
        media_content_id: >
          {% set val = trigger.to_state.state|int %}
          {% if val < 80 %}
            http://www.jsc.no/resources/audio/audio1.mp3
          {% elif val < 90 %}
            http://www.jsc.no/resources/audio/audio2.mp3
          {% elif val < 100 %}
            http://www.jsc.no/resources/audio/audio3.mp3
          {% else %}
            http://www.jsc.no/resources/audio/sales10.mp3
          {% endif %}
    - delay: >
          {% set val = trigger.to_state.state|int %}
          {% if val < 80 %}
           00:00:03
          {% elif val < 90 %}
           00:00:04
          {% elif val < 100 %}
           00:00:05
          {% else %}
           00:00:16
          {% endif %}
    - service: tts.google_say
      entity_id: media_player.kontor
      data_template:
       language: 'no'
       message: '{{states(''sensor.file_value'')}}' #This where I would like Value 2 in text format

Any support on how to do this would be highly appreciated

sensor:
  - platform: template
    sensors:
      file_value_number:
        value_template: "{{ states('sensor.file_value').split(';')[0] }}"
      file_value_string:
        value_template: "{{ states('sensor.file_value').split(';')[1].strip() }}"

Then your trigger and condition becomes:

  trigger:
    platform: state
    entity_id: sensor.file_value_number
  condition:
    condition: numeric_state
    entity_id: sensor.file_value_number
    above: 70

And your message becomes:

        message: "{{ states('sensor.file_value_string') }}"
3 Likes

Thanks pnbruckner, you are a star, this was exactly what I needed:)

1 Like