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}}
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.
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.