Hello, I need some help in using split on a return_variable so that I can use it to provide input for a helper. This is part of a process to force charge my battery to 30% before the higher rate tariff kicks in. In other words it gets me over the high rate tariff hump.
I have a HA script that runs a python script, whose output is the value of the option (in minutes) to force charge the battery. I want to use that output to input to a helper so that I can use that later.
This is my current code in the HA script:
alias: force_test
sequence:
- action: pyscript.force_charge
metadata: {}
data:
soc: "{{states('sensor.givtcp_ce2249g687_soc' )}}"
response_variable: ftime
- action: input_number.set_value
data:
value: "{{ ftime.split('option:')[1] | int }} "
target:
entity_id: input_number.force_option
enabled: true
When I run the script I get an error from the second action:
Error: Error rendering data template: UndefinedError: 'dict object' has no attribute 'split'
The python script has to return a dictionary(value). In my case that is “option: n”, where “n” is an integer. So, for example “option: 15”. It is the “15” part that I want to use as input to the helper, so hence using split.
My understanding is that split only works on strings, so I’m assuming I need to convert the contents of ftime into a string as it is a dictionary item?
Most of the examples that I have looked at already are using states(‘sensor’), which is a string output.
Other examples use the response_variable directly as input to a helper or entity. I can use the contents of ftime ({{ ftime }}) as direct input to the battery to force charge it, but I want to use the helper method for use later.
I know that the split will give me the desired result because I ran it in the Developer-Templates tab.
{% set ftime = "option: 15" %}
{% set option = ftime.split('option:')[1] | int %}
{{option}}
Result: 15 (number)
Obviously ftime is a string here.
So I’m struggling to get the syntax correct for the split function, assuming that it can be done.
The reason why I’m using a python script to calculate the force time and not jinja2 is because I want to get some experience in running python scripts in HA (scripts).
Thanks in advance for any help.