Extract value from state entity?

:wave:

I would like to extract the raw_two_days values from here, and pick the value which has the highest number (it’s the price for electricity). When I know the value, I would like to get the time for that occurrence, and that time should be the output of the template.

The reason is that I would like to start discharging my batteries when the price of the day peaks.

Sensor None has device class 'None', state class 'None' unit 'öre' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: '[{'value': 26.927, 'start': datetime.datetime(2023, 10, 30, 0, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 1, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 18.251, 'start': datetime.datetime(2023, 10, 30, 1, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 2, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 17.192, 'start': datetime.datetime(2023, 10, 30, 2, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 3, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 16.771, 'start': datetime.datetime(2023, 10, 30, 3, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 4, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 25.114, 'start': datetime.datetime(2023, 10, 30, 4, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 5, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 36.502, 'start': datetime.datetime(2023, 10, 30, 5, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 6, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 86.048, 'start': datetime.datetime(2023, 10, 30, 6, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 7, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 111.916, 'start': datetime.datetime(2023, 10, 30, 7, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 8, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 118.909, 'start': datetime.datetime(2023, 10, 30, 8, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 9, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 112.075, 'start': datetime.datetime(2023, 10, 30, 9, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 10, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 107.404, 'start': datetime.datetime(2023, 10, 30, 10, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 11, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 104.923, 'start': datetime.datetime(2023, 10, 30, 11, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 12, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 103.051, 'start': datetime.datetime(2023, 10, 30, 12, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 13, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 105.648, 'start': datetime.datetime(2023, 10, 30, 13, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 14, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 117.719, 'start': datetime.datetime(2023, 10, 30, 14, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 15, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 128.76, 'start': datetime.datetime(2023, 10, 30, 15, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 16, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 138.262, 'start': datetime.datetime(2023, 10, 30, 16, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 17, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 156.789, 'start': datetime.datetime(2023, 10, 30, 17, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 18, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 146.184, 'start': datetime.datetime(2023, 10, 30, 18, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 19, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 125.539, 'start': datetime.datetime(2023, 10, 30, 19, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 20, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 90.023, 'start': datetime.datetime(2023, 10, 30, 20, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 21, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 66.708, 'start': datetime.datetime(2023, 10, 30, 21, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 22, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 49.705, 'start': datetime.datetime(2023, 10, 30, 22, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 30, 23, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}, {'value': 40.507, 'start': datetime.datetime(2023, 10, 30, 23, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm')), 'end': datetime.datetime(2023, 10, 31, 0, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Stockholm'))}]' (<class 'str'>)

The above is from attempting to use this:

{{ state_attr('sensor.ev_smart_charging_charging', 'raw_two_days') }}

These are all the states better formatted:

current_price: 105.64816625
EV SOC: 55
EV target SOC: 55
Charging is planned: false
Charging start time: null
Charging stop time: null
Charging number of hours: 0
raw_two_days: 
- value: 26.927
  start: '2023-10-30T00:00:00+01:00'
  end: '2023-10-30T01:00:00+01:00'
- value: 18.251
  start: '2023-10-30T01:00:00+01:00'
  end: '2023-10-30T02:00:00+01:00'
- value: 17.192
  start: '2023-10-30T02:00:00+01:00'
  end: '2023-10-30T03:00:00+01:00'
- value: 16.771
  start: '2023-10-30T03:00:00+01:00'
  end: '2023-10-30T04:00:00+01:00'
- value: 25.114
  start: '2023-10-30T04:00:00+01:00'
  end: '2023-10-30T05:00:00+01:00'
- value: 36.502
  start: '2023-10-30T05:00:00+01:00'
  end: '2023-10-30T06:00:00+01:00'
- value: 86.048
  start: '2023-10-30T06:00:00+01:00'
  end: '2023-10-30T07:00:00+01:00'
- value: 111.916
  start: '2023-10-30T07:00:00+01:00'
  end: '2023-10-30T08:00:00+01:00'
- value: 118.909
  start: '2023-10-30T08:00:00+01:00'
  end: '2023-10-30T09:00:00+01:00'
- value: 112.075
  start: '2023-10-30T09:00:00+01:00'
  end: '2023-10-30T10:00:00+01:00'
- value: 107.404
  start: '2023-10-30T10:00:00+01:00'
  end: '2023-10-30T11:00:00+01:00'
- value: 104.923
  start: '2023-10-30T11:00:00+01:00'
  end: '2023-10-30T12:00:00+01:00'
- value: 103.051
  start: '2023-10-30T12:00:00+01:00'
  end: '2023-10-30T13:00:00+01:00'
- value: 105.648
  start: '2023-10-30T13:00:00+01:00'
  end: '2023-10-30T14:00:00+01:00'
- value: 117.719
  start: '2023-10-30T14:00:00+01:00'
  end: '2023-10-30T15:00:00+01:00'
- value: 128.76
  start: '2023-10-30T15:00:00+01:00'
  end: '2023-10-30T16:00:00+01:00'
- value: 138.262
  start: '2023-10-30T16:00:00+01:00'
  end: '2023-10-30T17:00:00+01:00'
- value: 156.789
  start: '2023-10-30T17:00:00+01:00'
  end: '2023-10-30T18:00:00+01:00'
- value: 146.184
  start: '2023-10-30T18:00:00+01:00'
  end: '2023-10-30T19:00:00+01:00'
- value: 125.539
  start: '2023-10-30T19:00:00+01:00'
  end: '2023-10-30T20:00:00+01:00'
- value: 90.023
  start: '2023-10-30T20:00:00+01:00'
  end: '2023-10-30T21:00:00+01:00'
- value: 66.708
  start: '2023-10-30T21:00:00+01:00'
  end: '2023-10-30T22:00:00+01:00'
- value: 49.705
  start: '2023-10-30T22:00:00+01:00'
  end: '2023-10-30T23:00:00+01:00'
- value: 40.507
  start: '2023-10-30T23:00:00+01:00'
  end: '2023-10-31T00:00:00+01:00'

charging_schedule: 
- start: '2023-10-30T00:00:00+01:00'
  end: '2023-10-30T01:00:00+01:00'
  value: 0
- start: '2023-10-30T01:00:00+01:00'
  end: '2023-10-30T02:00:00+01:00'
  value: 0
- start: '2023-10-30T02:00:00+01:00'
  end: '2023-10-30T03:00:00+01:00'
  value: 0
- start: '2023-10-30T03:00:00+01:00'
  end: '2023-10-30T04:00:00+01:00'
  value: 0
- start: '2023-10-30T04:00:00+01:00'
  end: '2023-10-30T05:00:00+01:00'
  value: 0
- start: '2023-10-30T05:00:00+01:00'
  end: '2023-10-30T06:00:00+01:00'
  value: 0
- start: '2023-10-30T06:00:00+01:00'
  end: '2023-10-30T07:00:00+01:00'
  value: 0
- start: '2023-10-30T07:00:00+01:00'
  end: '2023-10-30T08:00:00+01:00'
  value: 0
- start: '2023-10-30T08:00:00+01:00'
  end: '2023-10-30T09:00:00+01:00'
  value: 0
- start: '2023-10-30T09:00:00+01:00'
  end: '2023-10-30T10:00:00+01:00'
  value: 0
- start: '2023-10-30T10:00:00+01:00'
  end: '2023-10-30T11:00:00+01:00'
  value: 0
- start: '2023-10-30T11:00:00+01:00'
  end: '2023-10-30T12:00:00+01:00'
  value: 0
- start: '2023-10-30T12:00:00+01:00'
  end: '2023-10-30T13:00:00+01:00'
  value: 0
- start: '2023-10-30T13:00:00+01:00'
  end: '2023-10-30T14:00:00+01:00'
  value: 0
- start: '2023-10-30T14:00:00+01:00'
  end: '2023-10-30T15:00:00+01:00'
  value: 0
- start: '2023-10-30T15:00:00+01:00'
  end: '2023-10-30T16:00:00+01:00'
  value: 0
- start: '2023-10-30T16:00:00+01:00'
  end: '2023-10-30T17:00:00+01:00'
  value: 0
- start: '2023-10-30T17:00:00+01:00'
  end: '2023-10-30T18:00:00+01:00'
  value: 0
- start: '2023-10-30T18:00:00+01:00'
  end: '2023-10-30T19:00:00+01:00'
  value: 0
- start: '2023-10-30T19:00:00+01:00'
  end: '2023-10-30T20:00:00+01:00'
  value: 0
- start: '2023-10-30T20:00:00+01:00'
  end: '2023-10-30T21:00:00+01:00'
  value: 0
- start: '2023-10-30T21:00:00+01:00'
  end: '2023-10-30T22:00:00+01:00'
  value: 0
- start: '2023-10-30T22:00:00+01:00'
  end: '2023-10-30T23:00:00+01:00'
  value: 0
- start: '2023-10-30T23:00:00+01:00'
  end: '2023-10-31T00:00:00+01:00'
  value: 0
- start: '2023-10-31T00:00:00+01:00'
  end: '2023-10-31T01:00:00+01:00'
  value: 0
- start: '2023-10-31T01:00:00+01:00'
  end: '2023-10-31T02:00:00+01:00'
  value: 0
- start: '2023-10-31T02:00:00+01:00'
  end: '2023-10-31T03:00:00+01:00'
  value: 0
- start: '2023-10-31T03:00:00+01:00'
  end: '2023-10-31T04:00:00+01:00'
  value: 0
- start: '2023-10-31T04:00:00+01:00'
  end: '2023-10-31T05:00:00+01:00'
  value: 0
- start: '2023-10-31T05:00:00+01:00'
  end: '2023-10-31T06:00:00+01:00'
  value: 0
- start: '2023-10-31T06:00:00+01:00'
  end: '2023-10-31T07:00:00+01:00'
  value: 0
- start: '2023-10-31T07:00:00+01:00'
  end: '2023-10-31T08:00:00+01:00'
  value: 0
- start: '2023-10-31T08:00:00+01:00'
  end: '2023-10-31T09:00:00+01:00'
  value: 0
- start: '2023-10-31T09:00:00+01:00'
  end: '2023-10-31T10:00:00+01:00'
  value: 0
- start: '2023-10-31T10:00:00+01:00'
  end: '2023-10-31T11:00:00+01:00'
  value: 0
- start: '2023-10-31T11:00:00+01:00'
  end: '2023-10-31T12:00:00+01:00'
  value: 0
- start: '2023-10-31T12:00:00+01:00'
  end: '2023-10-31T13:00:00+01:00'
  value: 0
- start: '2023-10-31T13:00:00+01:00'
  end: '2023-10-31T14:00:00+01:00'
  value: 0
- start: '2023-10-31T14:00:00+01:00'
  end: '2023-10-31T15:00:00+01:00'
  value: 0
- start: '2023-10-31T15:00:00+01:00'
  end: '2023-10-31T16:00:00+01:00'
  value: 0
- start: '2023-10-31T16:00:00+01:00'
  end: '2023-10-31T17:00:00+01:00'
  value: 0
- start: '2023-10-31T17:00:00+01:00'
  end: '2023-10-31T18:00:00+01:00'
  value: 0
- start: '2023-10-31T18:00:00+01:00'
  end: '2023-10-31T19:00:00+01:00'
  value: 0
- start: '2023-10-31T19:00:00+01:00'
  end: '2023-10-31T20:00:00+01:00'
  value: 0
- start: '2023-10-31T20:00:00+01:00'
  end: '2023-10-31T21:00:00+01:00'
  value: 0
- start: '2023-10-31T21:00:00+01:00'
  end: '2023-10-31T22:00:00+01:00'
  value: 0
- start: '2023-10-31T22:00:00+01:00'
  end: '2023-10-31T23:00:00+01:00'
  value: 0
- start: '2023-10-31T23:00:00+01:00'
  end: '2023-11-01T00:00:00+01:00'
  value: 0

icon: mdi:flash

Anyone know how to do that?

Thank you in advance!

EDIT:

OK, so I can now get 1 of the values, but I would like to either:

  1. Get all the values to be able to do some math
  2. Get the top value directly as an output
  3. Get the time when the max value occurs
{{ state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[1].value }}

cc @123 here since you seem to be an expert.

Copy-paste the following template into the Template Editor and confirm it reports the desired start time.

{{ (state_attr('sensor.ev_smart_charging_charging', 'raw_two_days') | sort(attribute='value') | last).start }}

BTW, you didn’t post the configuration of your Template Sensor but the error message indicates it’s using unit_of_measurement: 'öre' which requires the template to report a numeric value. However, this Template Sensor isn’t meant to report a numeric value, it reports a datetime string. Either remove the entire unit_of_measurement option or replace its value with timestamp.

1 Like

Quick question, how would the similar template look for the highest price?

The template I use right now is this one, and it seems like it doesn’t update;

{% set sensors = [ 
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[1].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[2].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[3].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[4].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[5].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[6].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[7].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[8].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[9].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[10].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[11].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[12].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[13].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[14].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[15].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[16].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[17].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[18].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[19].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[20].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[21].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[22].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[23].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[24].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[25].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[26].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[27].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[28].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[29].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[30].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[31].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[32].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[33].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[32].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[35].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[36].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[37].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[38].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[39].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[40].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[41].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[42].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[43].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[44].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[45].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[46].value|float,
            state_attr('sensor.ev_smart_charging_charging', 'raw_two_days')[47].value|float,
            ]
          %}
  
          {{ sensors|max }}
{{ state_attr('sensor.ev_smart_charging_charging', 'raw_two_days') | map(attribute='value') | max }}
1 Like

You sir, are the best!

1 Like