How to split attributes

I am trying to split some values from a attributes.
configuration.yaml is:

  • platform: template
    sensors:
    strompris_i_morgen:
    unique_id: PrisIMorgen
    #value_template: ‘{{ state_attr.sensor.nordpool_kwh_oslo_nok_3_10_025.Tomorrow.split(",")[1]}}’
    value_template: “{{ state_attr(‘sensor.nordpool_kwh_oslo_nok_3_10_025’, 'description [‘tomorrow’] }}”
    friendly_name: “Pris_i_morgen”
    unit_of_measurement: ‘nok’

The attributes is:

The enitle sayes - unknown…

Any ideas?

Eddie

Remove the unit_of_measurement line and see if you get the expected result… Unit of measurement is now reserved exclusively for numeric values.

1 Like

Hi,

Looks like the Attribute is named “Tomorrow” so in that case i think the value_template should be “{{ state_attr(‘sensor.nordpool_kwh_oslo_nok_3_10_025’, ‘tomorrow’ }}". In this case the result should be all those numbers starting with 0,92, 0,91, 0,91… etc

It looks like the Tomorrow value contains a number of other sub-values so If you wanted to futher breakdown Tomorrow you might us something like “{{ state_attr(‘sensor.nordpool_kwh_oslo_nok_3_10_025’, ‘tomorrow’[0] }}". In this case the result should be ‘0,92’.

I hope that’s right?

Cheers

1 Like

Please format your code correctly. This isn’t just to make it look nice — your code could have indentation errors, wrong quote marks (“smart” quotes for example) and we cannot tell with text formatting like that.

There are a number of things we can’t tell from the screenshot:

  • is the attribute called tomorrow or Tomorrow?
  • are the numbers formatted with a European decimal comma or is your front end doing that?

Also recommend you use modern format for template sensors.

Assuming the attribute is called Tomorrow, and you want the first value as a number, this should work if the numbers contain a decimal point. It should go under the top-level template:, not under sensor:.

template:
  - sensor:
      - name: Strompris I Morgen
        state: "{{ state_attr('sensor.nordpool_kwh_oslo_nok_3_10_025', 'Tomorrow')[0] }}"
        unit_of_measurement: "nok"

If that doesn’t work, go to Developer Tools / Templates and paste in:

{{ states.sensor.nordpool_kwh_oslo_nok_3_10_025.attributes }}

Copy and paste the result here, correctly formatted as code.

1 Like

Hi,
tomorrow is “tomorrow” lowchars.

Template sayes:
{{ state_attr(‘sensor.nordpool_kwh_oslo_nok_3_10_025’, ‘tomorrow’) }}

[
1.101,
1.072,
1.058,
1.054,
1.052,
1.057,
1.121,
1.202,
1.2,
1.202,
1.214,
1.22,
1.223,
1.219,
1.22,
1.208,
1.202,
1.17,
1.148,
1.162,
1.19,
1.168,
1.189,
1.145
]

{{ state_attr(‘sensor.nordpool_kwh_oslo_nok_3_10_025’, ‘tomorrow’[1]) }}

null

i guess the split / [1] does not work.

Eddie

Formatting, please!!

{{ state_attr('sensor.nordpool_kwh_oslo_nok_3_10_025', 'tomorrow')[0] }}

[1] is the second item. Us computer people count from 0. Note where the brackets go.

1 Like

I did get it working.

However the [x] does not work, but .x works… like this.

template:

  • sensor:
    • name: StromMin
      state: “{{ (state_attr(‘sensor.nordpool_kwh_oslo_nok_3_10_025’, ‘tomorrow’) | sort).0 }}”
      unit_of_measurement: “nok”
  • sensor:
    • name: StromMax
      state: “{{ (state_attr(‘sensor.nordpool_kwh_oslo_nok_3_10_025’, ‘tomorrow’) | sort).23 }}”
      unit_of_measurement: “nok”

Tnx for very good ideas… i would fix this without help.

Please read the formatting instructions before posting again. I’ve asked several times but you haven’t adapted.

Show me a use of [0] that doesn’t work.

2 Likes