I am trying to figure out to round up by quarters
code:
{% set charge_time = (4 * (('number.tesla_ten_wolde_charge_limit' - 'sensor.tesla_ten_wolde_battery') / 0.16) | round(1, "ceil") / 4) | float %}
Error:
TypeError: unsupported operand type(s) for -: 'str' and 'str'
Troon
(Troon)
January 22, 2024, 8:20am
2
Entity states are always strings: and you can’t subtract strings. However, you’re not even referencing the entities, but trying to subtract one name from the other.
You need to use these in your calculations:
states('number.tesla_ten_wolde_charge_limit')|float(0)
and
states('sensor.tesla_ten_wolde_battery')|float(100)
I show 100
as the default for the second one as it’s being used as a denominator: if the sensor is unavailable, you don’t want a dvide-by-zero error.
You also don’t need the float
at the end of your calculation: by the time you get there, you already have a numeric value.
Thank you for the suggestions. I am a novel. What goes wrong here?
Error rendering data template: ValueError: Template error: float got invalid input ‘23:00’ when rendering template
{% set charge_time = 4 * ((states('number.tesla_ten_wolde_charge_limit')|float(0) - states('sensor.tesla_ten_wolde_battery')|float(100)) / 0.16) | round(1, "ceil") / 4 %} {% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %} {% set today = cheapest_energy_hours('sensor.nordpool', time_format='time24', start="14:00", end="13:00", include_tomorrow=true ) %} {% set price_today = (cheapest_energy_hours('sensor.nordpool', time_format='time24', start="14:00", end="13:00", include_tomorrow=true) | float) | round(2) %} Begin tijd: {{ today }} (€ {{ "{:.2f}".format(price_today) }})
but no default was specified
Troon
(Troon)
January 22, 2024, 10:09am
4
Please use line breaks to make your templates more readable.
{% set charge_time = 4 * ((states('number.tesla_ten_wolde_charge_limit')|float(0) - states('sensor.tesla_ten_wolde_battery')|float(100)) / 0.16) | round(1, "ceil") / 4 %}
{% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
{% set today = cheapest_energy_hours('sensor.nordpool', time_format='time24', start="14:00", end="13:00", include_tomorrow=true ) %}
{% set price_today = (cheapest_energy_hours('sensor.nordpool', time_format='time24', start="14:00", end="13:00", include_tomorrow=true) | float) | round(2) %}
Begin tijd: {{ today }} (€ {{ "{:.2f}".format(price_today) }})
You need to look at the docs for the cheapest energy macro . Your error is almost certainly coming from this line (wrapped for even better readability):
{% set price_today =
(cheapest_energy_hours('sensor.nordpool',
time_format='time24',
start="14:00",
end="13:00",
include_tomorrow=true)
| float) | round(2)
%}
I’d suggest cheapest_energy_hours
is not returning a number, but a time or list of times — looks from the error like it’s returning 23:00
. The template editor is complaining that float
can’t convert that to a number.
You’ll have to take that time and look up the corresponding price in sensor.nordpool
: if you can paste the output as formatted code from this template, we can help with that:
{% set charge_time = 4 * ((states('number.tesla_ten_wolde_charge_limit')|float(0) - states('sensor.tesla_ten_wolde_battery')|float(100)) / 0.16) | round(1, "ceil") / 4 %}
{% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
{% set today = cheapest_energy_hours('sensor.nordpool', time_format='time24', start="14:00", end="13:00", include_tomorrow=true ) %}
{{ today }}
{{ states.sensor.nordpool }}