Template an entity from arrays

I’m looking for some help with some complex templating.

My Solar Inverter has “time of use” support, which uses periods of time to determine how much energy to import/export from the grid.

Integration provided entities look like:

number.prog1_capacity : 20%

sensor.time_slot_2 : 1:00 Pm
number.prog2_capacity : 10%

sensor.time_slot_3 : 5:00Pm
number.prog3_capacity : 20%

sensor.time_slot_4 : 11:00Pm
number.prog4_capacity : 100%

Request: I’m looking for help answering the question: What is the current target capacity?

IE, Take the current system time, determine the correct program time, then extract the capacity.

I don’t think I’ve seen a similar templating example. Any Jinja Ninjas who can help?

At 8pm, is it 20% (holding 5pm value), 60% (linear change) or something else?

What are the actual states of those entities, from Developer Tools / States?

Based solely on the info provided, here’s a quick-and-dirty option:

template:
  - trigger:
      - trigger: template
        value_template: "{{ now().strftime('%-I:%M%p') == states('sensor.time_slot_1') | replace(' ','') |upper }}"
        variables:
          cap: "{{ states('number.prog1_capacity') }}"
      - trigger: template
        value_template: "{{ now().strftime('%-I:%M%p') == states('sensor.time_slot_2') | replace(' ','') |upper }}"
        variables:
          cap: "{{ states('number.prog2_capacity') }}"
      - trigger: template
        value_template: "{{ now().strftime('%-I:%M%p') == states('sensor.time_slot_3') | replace(' ','') |upper }}"
        variables:
          cap: "{{ states('number.prog3_capacity') }}"
      - trigger: template
        value_template: "{{ now().strftime('%-I:%M%p') == states('sensor.time_slot_4') | replace(' ','') |upper }}"
        variables:
          cap: "{{ states('number.prog4_capacity') }}"
    sensor:
      - name: Current Target Capacity
        state: "{{ cap }}"

As stated, it’s dirty… There are no guards against restarts that span the start of a time slot, so the value could be inaccurate for hours.

Yes, at 8pm it would hold 20%. (allow discharge of battery to 20% during medium time-of-use).

Actual values:

{% set caps = ['number.sol_ark_prog1_capacity', 'number.sol_ark_prog2_capacity', 'number.sol_ark_prog3_capacity',
'number.sol_ark_prog4_capacity', 'number.sol_ark_prog5_capacity', 'number.sol_ark_prog6_capacity'] | map('states') | list%}
{% set times = ['sensor.sol_ark_time_slot_1', 'sensor.sol_ark_time_slot_2', 'sensor.sol_ark_time_slot_3',
'sensor.sol_ark_time_slot_4','sensor.sol_ark_time_slot_5','sensor.sol_ark_time_slot_6',] | map('states') | map('today_at') | list %}

{{ (zip(times, caps) | selectattr(0, 'le', now()) | reverse | first)[1] }}
1 Like

Very nice. Thank you, and as a plus, I get to learn about zip().