String help (please)

Y’all gave me a hand earlier with a string formatting issue with an attribute being longer that 255 characters. Your suggestion was:

spacex_next_text:
  value_template: "{{ states.sensor.spacex.attributes['details'] | truncate(255) }}"
  entity_id: sensor.spacex

Which works superbly for the first part of the string, thankyou.
So how do I format a second value_template for capturing the second half of this string? (which can occasionally reach 500 characters)

I don’t know what you are trying to achieve here, but what I would do is assign the number of characters in the truncated text to a variable and then get the rest of the characters from there to the end like this:

{% set string_length = states.sensor.spacex.attributes['details'] | truncate(255) | length - 3 %}
{{ states.sensor.spacex.attributes['details'][string_length: ] }}

I am substracting 3 from the string length because truncate(255) will add an ellipsis sign (…) to the end of the string if it was truncated.

This is what I would use with built in safety

spacex_next_text:
  value_template: >
    {% set details = state_attr('sensor.spacex', 'details') %}
    {{ details[255:] if details | length > 255 else '' %}
  entity_id: sensor.spacex

It grabs all characters after the 255th character, if the string is longer than 255.

And just to explain the code, mystring is a 10 character string.

{% set mystring = '0123456789' %}

If I want to grab the 7th character in my string, call out the 7th index. Which happens to be 6 because lists start at zero. 0 = 1st item, 1 = second item, … 6 = 7th item. So in our case, the 7th item is the number 6. Use brackets after your string to grab that item [index].

Now, i want to grab everything before the 7th item, but not the 7th item. Well using the : character tells the brackets to grab everthing. Depending on what side you place the : on is what you get.

EDIT: Truncate is only useful when you want the pretty … at the end of your truncated string. If you don’t care about that, just cut the string at the index you want.

4 Likes

Thank you sir!

how to set first 9 letters from source:

source: TVN TURBO HD - Mistrzowie wpadek
i need to set first 9 letters: TVN TURBO

value_template: "{{ is_state_attr('media_player.vu_uno_4k', '{{source[:9]}}', 'TVN TURBO' ) }}" 

not working…

this one works but its channel name and program name

value_template: "{{ is_state_attr('media_player.vu_uno_4k', 'source', 'KINO POLSKA HD - Człowiek z marmuru' ) }}"
value_template: "{{ state_attr('media_player.vu_uno_4k', 'source')[:9] == 'TVN TURBO' }}"
1 Like

you are master :slight_smile:

this one works perfect:

  • id: sport led eleven
    alias: sport led eleven
    trigger:
    platform: template
    value_template: “{{ state_attr(‘media_player.vu_uno_4k’, ‘source’)[:6] == ‘Eleven’ }}”
    action:
    entity_id: light.led_tv
    data:
    rgb_color: [38, 80, 17]
    brightness: 80
    service: light.turn_on

but can i have a comeback to white leds on other any channels?

only on eleven green leds, and when change to any other channel (not eleven) i going back to white?

action:
entity_id: light.led_tv
data:
rgb_color: [255, 214, 170]
brightness: 175
service: light.turn_on

i trying to use template with if and elseif but its not easy…

  • id: sport led eleven
    alias: sport led eleven
    trigger:
    platform: template
    value_template: “{{ state_attr(‘media_player.vu_uno_4k’, ‘source’)[:6] == ‘Eleven’ }}”
    action:
    • data:
      entity_id:
      • script.tv_sport
        service: script.turn_on
  • id: sport led normal
    alias: sport led normal
    trigger:
    platform: template
    value_template: “{{ state_attr(‘media_player.vu_uno_4k’, ‘source’)[:6] != ‘Eleven’ }}”
    action:
    • data:
      entity_id:
      • script.tv_normal
        service: script.turn_on

works
scripts:

tv_normal:
alias: Led white TV1
sequence:
- service: light.turn_on
data:
entity_id: light.led_tv
rgb_color: [255, 214, 170]
brightness: 80

tv_sport:
alias: Led green TV1
sequence:
- service: light.turn_on
data:
entity_id: light.led_tv
rgb_color: [38, 80, 17]
brightness: 80

white\green

Thanks @petro I used this to create two sensors to split the state of a weather forecast into two sensors.

  - platform: template
    sensors:
      weather_report_conditions:
        entity_id: sensor.weather_report_conditions
        friendly_name: "Weather Report Conditions"
        value_template: >-
          {{'. Conditions are'}}
          {% set details = state_attr('sensor.devonport_extended_text_0', 'state') %}
          {{ details[:230] if state | length < 230 else '' -}}
      weather_report_conditions_2:
        entity_id: sensor.weather_report_conditions_2
        friendly_name: "Weather Report Conditions_2"
        value_template: >-
          {%- set details = state_attr('sensor.devonport_extended_text_0', 'state') %}
          {{- details[230:] if state | length < 230 else '' }}

HOW TO: Full Text Weather Report, BOM Australia Integration, Extract Weather Warning Data & Other Text

TTS: Full Text Weather Report, BOM Australia Integration, Extract Weather Warning Data & Other Text