i know that this kind of question is flying around a lot, and i read now at least 5h of training like the templating tutorials or watched videos for those stuff.
But it seems that i am to stupid to write my own code so that it works. Maybe somebody of you are capable of helping me out.
The Idea and Problem
I do have a sensor for Energy Exported (sold) and Importet (bought)…
I created a sensor (utility meter) that grabs the information from those sensors with an YTD renewal so that i see where i am at per YTD.
No i wanted to perform a mathematical solution to calculate the prices.
But still my Yaml is creating errors and telling me that “value_template” is not allowed for templates etc.
Therefor, maybe somebody can help me out with this situation:
What i want:
A variable of how many days passed (e.g.: Its the 153 day of the year) = days_passed
A variable of how many month passed or the actual month ( eg. Its month 4) = month_passed
Variables for the different prices (e.g.: Price for selling energy, price for buying energy, price per year etc.) = price_sell; price_sell_advance; price_buy; price_year;
A calculation that says: Earnings = {{ ((states(‘sensor.stromverkauf_ytd’) | float * price_sell) | round(2)) - (month_passed * price_sell_advance) }}
A calculation that says: Spending = {{ ((states(‘sensor.stromzukauf_ytd’) | float * price_buy) | round(2)) + (price_year / 365* days_passed) }}
All that in my configuration.yaml becuase i do not have splitted them yet.
The variables would be done with a helper on the gui.
Is there anyone hwo can tell me where my error is in these two formulas?
Error =TypeError: can’t multiply sequence by non-int of type ‘float’
Now i am still not sure what to do. I tried to set ‘input_number.price_sell’ with a | float so that i do not get this error, but it made it even worse XD.
first make sure you are using the correct type of quotes. it’s hard to describe in words but there are two types of quotation marks (both single and double). I call them “fancy” and “normal”
if I use code blocks you should see the difference:
“these are fancy”
"these are normal"
then you need to use the states function to use your input_numbers.
Then you need to convert those states to floats for multiplication.
You should probably get into the habit of providing defaults for stuff too
First of all, you made my day
Even my wife was laughing regarding the “fancy” and “normal” qoutation marks.
But i understand that there are differences but not how to “produce” them. My keyboard just has these " but thats it. So maybe there is a trick to change them.
Then second:
it did work out with one state but the other do still have the same error:
i did try to find out where a float is missing, but couldn’t figure it out.
The only two posible things where the “365” or the “timestamp_custom(‘%-j’)”.
But overall, thank you so so so much for your help. You really helped me out a lot.
Usually the fancy ones are made by document editors (word, openoffice, etc) that do rich text formatting. Even the forum posts that aren’t enclosed in the three backticks representing “code blocks” will sometimes create the fancy ones.
Basic text editors (notepad, notepad++) make the normal ones, or again, if you enclose the quotes in backticks (```) in the forum it knows to create the normal ones too.
the other issue is the last item in the template isn’t actually a number even tho it looks like one.
the template for datetimes converted using timestamp_custom will produce a string data type.
ex, now() is a datetime object. when you manipulate it (as with using timestamp_custom) it’s a string and not actually a “number”.
so you still need to convert the string into a number (either a float or an int) before you can do math on it.