Templating Help - Separate values with multiple commas

I’m needing a little help on a template to separate one sensor into two.

This data is how long is available on the utility - Gas/Electric.
Electric: 18 Hours left
Gas: 74 days, 17 hours left
image
as you can see, everything is separated by a comma.
So sometimes the format might be

Electric | Gas

  • days,hours, separate, days,hours
  • hours, separate, days,hours
  • hours, separate, hours
  • days,hours, separate, hours

From this, I’m wanting two new sensors
Electirc_Expt: (how many days/hours are remaining)
Gas_Expt: (how many days/hours are remaining)

It’s a little unclear what format you want the final sensors to be in, but the basic template sensor for this is as follows:

template:
  - sensor:
      - name: "Electric Expt"
        state: "{{ states('utilita_test_dates')[0] }}" 
      - name: "Gas Expt"
        state: "{{ states('utilita_test_dates')[1] }}"

I’m wanting to separate the two values, but they all use commas.

so with the above
Electric: 18 hours left
Gas: 74 days, 17 hours left

But now I have updated my data
image

Electric: 25 days, 7 hours left
Gas: 103 days, 11 hours left

So using this

{{ states('sensor.utilita_test_dates')}}

{{ states('sensor.utilita_test_dates').split(',')[0] |replace("'", '')|replace("[", '')}}
{{ states('sensor.utilita_test_dates').split(',')[1] |replace("'", '')|replace("", '')}}
{{ states('sensor.utilita_test_dates').split(',')[2] |replace("'", '')}}
{{ states('sensor.utilita_test_dates').split(',')[3] |replace("'", '')|replace("]", '')}}

I can format it to look like this…
image

But this won’t work if the “days” drop off one of the sensors…

1 Like

I should have seen that… it’s not a real list, it’s a list-shaped string. That makes the template a little more involved because you have to filter out all the extraneous characters. There’s probably an easier way to do this, but I think the following should work:

template:
  - sensor:
      - name: "Electric Expt"
        state: >
          {% set x = states('utilita_test_dates') %}
          {%- set x =  ((x | replace(","," ") | replace("]","")
          | replace("[","") | replace("'","")).split('left')) %} 
          {{x[0] | replace('  ', ', ') }} 
      - name: "Gas Expt"
        state: >
          {% set x = states('utilita_test_dates') %}
          {%- set x =  ((x | replace(","," ") | replace("]","")
          | replace("[","") | replace("'","")).split('left')) %} 
          {{ x[1] | replace('  ', ', ') }}
1 Like
template:
  - sensor:
      - name: Electric Expt
        state: >
          {{ states('sensor.utilita_test_dates')[2:-2].replace("','", "!").split('!')[0] }}
      - name: Gas Expt
        state: >
          {{ states('sensor.utilita_test_dates')[2:-2].replace("','", "!").split('!')[1] }}

This almost has it!

There’s just one Comma remaining…
image

I’m sorry this one isn’t working…

The ‘Elec’ outputs like this…
image

The ‘Gas’ outputs like this…
image

That’s because ', ' contains a space and I thought it didn’t.

['25 days, 7 hours left', '103 days, 11 hours left']
                         ^
                         |
          I didn't know there's a space here

I added a space to the template and it should work now.

template:
  - sensor:
      - name: Electric Expt
        state: >
          {{ states('sensor.utilita_test_dates')[2:-2].replace("', '", "!").split('!')[0] }}
      - name: Gas Expt
        state: >
          {{ states('sensor.utilita_test_dates')[2:-2].replace("', '", "!").split('!')[1] }}
3 Likes

That does the trick!

Many thanks :slight_smile:

2 Likes

You can take some of the fragility out of this by using the from_json filter. That needs the string to be double-quoted, though, so:

{{ (states('sensor.utilita_test_dates')|replace("'",'"')|from_json)[1] }}

should achieve the same thing without caring whether there are spaces there or not.

3 Likes

I just need the value “15”. How do I remove spaces and “l”?

3.  {{ states('sensor.mlpaliwo')[29:-2].replace("', '", "!").split('!')[0] }}

{{ states('sensor.mlpaliwo') | regex_findall('([0-9]+) l') | first }}

2 Likes