Help with writing a YAML Automation - Combining to variables into a single string, surely it can't be that hard

I’m trying to write my first Automation, but am consistently getting stuck with simple combining two variables to create a string in the format “HH:MM-HH:MM”.

The idea is to check periodically to see whether the car is on charge, if it is, charge the home battery at the same time by modifying the slot 3 charge window to go from now, until 15 minutes from now, effectively nudging the time along until the charging stops, where I’ll have another automation, or a condition in this one that sets the time back to 00:00-00:00

Rather than it working how I thought it would, it’s giving me the following error:

Error: Value now().strftime(‘%H:%M’)-(now() + timedelta(minutes=15)).strftime(‘%H:%M’) for text.inverter_control_slot3_charge_time is too long (maximum length 11)

Which makes it look as if rather than my variables being stored as their values, they’re instead being represented as the coded string?

Any help would be appreciated.


alias: Charge the house batteries if the car is charging
description: ""
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.myenergi_power_charging
    above: 300
conditions:
  - condition: time
    before: "23:30:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
actions:
  - variables:
      from: now().strftime('%H:%M')
      until: (now() + timedelta(minutes=15)).strftime('%H:%M')
  - variables:
      combined: "{{ from~'-'~until }}"
  - action: text.set_value
    metadata: {}
    target:
      entity_id: text.inverter_control_slot3_charge_time
    data:
      value: "{{combined | string}}"
mode: single


My confusion in part comes from my thinking this was working just about fine in the Template editor within devloper tools, with the following writting out 19:26-19:41, just like I expected it to.

{% 
  set range = {
    "from": now().strftime('%H:%M'),
    "until": (now() + timedelta(minutes=15)).strftime('%H:%M')
  } 
%}

{{range.from~"-"~range.until}}

Your variables section. Contains string literal rather than single line templates. So you are getting the string - not the result

"{{ - - -}}"

Get used to typing that ^

A LOT

:sunglasses:

P. S. Everything else looks good.

1 Like

Do you mean doing the following? As this is what I’ve tried, but then I get an unsupported operand exception:

Error: TypeError: unsupported operand type(s) for -: 'NodeStrClass' and 'NodeStrClass'

variables:
  combined: '{{ from - until }}'

You want to subtract a from b? (it’s treating hyphen inside the template marker as… Subtract then looking for number types. You actually have formatted strings

Try something like
"{{ foo1}} - {{ foo2}}"

Or if drew says do anything different you do what Drew says.

2 Likes

You need delimiters where you are defining the other variables, from and until.

Your concatenation was fine, though the second variables section and the string filter are unnecessary:

actions:
  - variables:
      from: "{{ now().strftime('%H:%M') }}"
      until: "{{ (now() + timedelta(minutes=15)).strftime('%H:%M') }}"
      combined: "{{ from~'-'~until }}"
  - action: text.set_value
    metadata: {}
    target:
      entity_id: text.inverter_control_slot3_charge_time
    data:
      value: "{{ combined }}"
2 Likes

That’s it, thank you! I’ve spent a good few hours banging my head against this, so the help is very much appreciated.

To double check, the main problem is that I was doing:

- variables:
      from: now().strftime('%H:%M')
      until: (now() + timedelta(minutes=15)).strftime('%H:%M')

Instead of:

- variables:
      from: {{ now().strftime('%H:%M') }}
      until: {{ (now() + timedelta(minutes=15)).strftime('%H:%M') }}

The ommission of the {{ }} around the variables meant that they’re written literally to the variable, instead of the value I expected to come out of them.

Yes, but you need quotes around the templates as well… that’s literally Rule #1.

  - variables:
      from: "{{ now().strftime('%H:%M') }}"
      until: "{{ (now() + timedelta(minutes=15)).strftime('%H:%M') }}"
1 Like