As @cgtobi said, it’s a local variable and is basically just a copy of the value of sensor.charging_time_left, converted from a string to an integer. And, yes, you could just use states('sensor.charging_time_left) | int everywhere in the template, but that can make the template much less compact and harder to type and read, leading to potential mistakes. That’s generally why I would use a variable. But @cgtobi is definitely right about the value of the sensor potentially changing during the execution of the statement. After 40 years of programming, I can tell you, no matter how unlikely something is, if it’s possible it will happen. Ever hear of the term “glitch”? Well, this type of thing is exactly what’s behind most of them!
I guss it is then necassary to create a complete new template for this. Can I use the same internal variable “ct” as in the first template or must it have another name?
I would also like to integrate {% if ct == 0 %} Unavailable into the new template but when doing so it returns an error.
The “scope” of these variables is the template they are in. So you can use the same variable name in multiple templates.
It helps if you provide the actual text of the error. But, in this case, if you haven’t left something out when copying into this post, it looks like you haven’t finished the second template. Also, right now the way you wrote it you can’t tell the difference between ‘unavailable’ and ‘0’. I would think you want something like this:
2018-07-06 21:44:04 ERROR (MainThread) [homeassistant.components] Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: Unexpected end of template. Jinja was looking for the following tags: ‘elif’ or ‘else’ or ‘endif’. The innermost block that needs to be closed is ‘if’.) for dictionary value @ data[‘sensors’][‘charging_finished’][‘value_template’]. Got ‘{% set ct = states(‘sensor.charging_time_left’) | int %} {% if ct == 0 %}\n Unavailable\n{{ (as_timestamp(now()) + ct * 60) | timestamp_custom("%H:%M") }}\n’. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/
In the second template I want to show the actual time when charging is complete but when ‘sensor.charging_time_left’ is “0” it should display “unavailable” like in the first template.
What is the binary_sensor template exactly doing?
Ok, I misunderstood. From the name of the second sensor I thought you wanted it to indicate whether or not charging was finished, not when it will finish. So maybe:
Here I provide separate indications of unavailable and done. If you want to show the time it finished (i.e., in the past), then you’ll need an input_datetime to record the finish time in and an automation to trigger the recording.
Modify the template so that it first checks if the total number of minutes exceeds 1440 (total minutes in a day = 24 * 60). If it does, uses integer division to divide the total minutes by 1440 to get the number of days (days = total minutes // 1440). Then continue processing to get the hours and minutes.
This topic and one or two others has been extremely helpful for this newbie to calculate time remaining to charge an EV from basic input paramaters. ie enter a start and target percent, and this calculates required kWh, then current charge rate divided into the required kWh gives a time remaining (decimal hours) and then that gets added to the current time in minutes and seconds to give an end time in local time zone. Lastly when the target kWh is delivered, the wifi circuit breaker turns off terminating the charge at the specified battery level.
Losses through the chargring process and temperature can make some variances, but generatlly I am within 1-2% state of charge of the EV battery over a full charge cycle. I’ve done this because the Renault Zoe can’t terminate the charge at a given battery charge level, so this was my solution
It was a journey but with the community I have been able to get it working and without needing to bother anyone for assistance. Kudos to all!
This sensor seems to be exactly what i need but for some reason it does not work for me…
In my code i replaced ‘sensor.charging_time_left’ with my own value and changed out the ‘Unavailable’ with ‘Unkown’ and placed the whole thing in my config.
But i get the following error message when I check my configuration:
Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected ')') for dictionary value @ data['sensors']['charge_finish_time']['value_template']. Got '{% if is_state(\'sensor.zoe_charging_remaining_time\',\'Unknown\') %}\n Unknown\n{% else %}\n {% set ct = states(\'sensor.zoe_charging_remaining_time\') | int %}\n {% if ct == 0 %)\n Done\n {% else %}\n {{ (as_timestamp(now()) + ct * 60) | timestamp_custom("%H:%M") }}\n {% endif %}\n{% endif %}\n'. (See ?, line ?).