Help with yaml & templates

Hi everybody,

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.

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

Examples:

  • platform: template
    sensors:
    utilita_electric_actual_daily_cost:
    friendly_name: “Utilita Elec Daily Cost”
    unit_of_measurement: ‘£/kWh’
    icon_template: mdi:flash
    value_template: >-
    {% if states(‘sensor.daily_consumption’) | float > states(‘input_number.utilitatariff_dailykwh’) | float %}
    {{ states(“input_text.utilitatariffrate2”) | round(3) }}
    {% else %}
    {{ states(“input_text.utilitatariffrate1”) | round(3) }}
    {%- endif %}

Tariff unit price - Configuration - Home Assistant Community (home-assistant.io)

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:

  1. A variable of how many days passed (e.g.: Its the 153 day of the year) = days_passed
  2. A variable of how many month passed or the actual month ( eg. Its month 4) = month_passed
  3. 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;
  4. A calculation that says: Earnings = {{ ((states(‘sensor.stromverkauf_ytd’) | float * price_sell) | round(2)) - (month_passed * price_sell_advance) }}
  5. 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?

template:

  • sensor:
    • name: Earnings
      unit_of_measurement: ‘€’
      value_template: {{ ((states(‘sensor.stromverkauf_ytd’) | float * price_sell) | round(2)) - (month_passed * price_sell_advance) }}

template:

  • sensor:
    • name: Earnings
      unit_of_measurement: ‘€’
      value_template: {{ ((states(‘sensor.stromzukauf_ytd’) | float * price_buy) | round(2)) + (price_year / 365* days_passed) }}

Is there anyone who can tell me how to get the date information that i need in the variables?

Thank you all in advance Andreas

you don’t use “value_template:” for the new style temp;late sensors, you use “state:”.

And you need to wrap your templates in quotes. Make sure inner and outer quotes are different.

template:
  - sensor:
      - name: Earnings
        unit_of_measurement: ‘€’
        state: "{{ ((states(‘sensor.stromverkauf_ytd’) | float * price_sell) | round(2)) - (month_passed * price_sell_advance) }}"
      - name: Earnings
        unit_of_measurement: ‘€’
        state: "{{ ((states(‘sensor.stromzukauf_ytd’) | float * price_buy) | round(2)) + (price_year / 365* days_passed) }}"
"{{ as_timestamp(now()) | timestamp_custom('%-j') }}"
"{{ now().month }}"

I can’t show you how to create the rest since I don’t have you entities or services but they should be possible based on the info I already provided.

1 Like

Hi finity,

thanks for your reply. i put all your scripts into my configuration.yaml

template: (Line 37)
-sensor: (Line 38)
- name: Einnahmen
unit_of_measurement: ‘€’
state: “{{ (states(‘sensor.stromverkauf_ytd’) | float * input_number.price_sell) | round(2) - ((now().month) * input_number.price_sell_advance) }}”
- name: Ausgaben
unit_of_measurement: ‘€’
state: “{{ ((states(‘sensor.stromzukauf_ytd’) | float * input_number.price_buy) | round(2)) + (input_number.price_year / 365 * (as_timestamp(now()) | timestamp_custom(‘%-j’))) }}”

Now i do get the two sensors in my entitys. But both are not available.
When i check the YAML-Configuration HA tells me everything is fine.

So i did out these information in the developmenttools in the templating test tool.

  1. Step: “{{ (states(‘sensor.stromverkauf_ytd’) | float * input_number.price_sell) | round(2) - ((now().month) * input_number.price_sell_advance) }}”

Error =TemplateSyntaxError: unexpected char ‘‘’ at 12

  1. Step: “{{ (states(‘sensor.stromverkauf_ytd’) | float * ‘input_number.price_sell’) | round(2) - ((now().month) * ‘input_number.price_sell_advance’) }}”

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.

Could you please have a final look?

Thanks a lot Andreas

there are a few things.

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

so the result should look like this I think:

"{{ ((states('sensor.stromverkauf_ytd') | float(0) * states('input_number.price_sell') | float(0)) | round(2)) - ((now().month) * states('input_number.price_sell_advance') | float(0)) }}"
1 Like

First of all, you made my day :slight_smile:
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:

“{{ ((states(‘sensor.stromzukauf_ytd’) | float(0) * states(‘input_number.price_buy’) | float(0)) | round(2)) + (states(‘input_number.price_year’) | float(0) / 365 * (as_timestamp(now()) | timestamp_custom(‘%-j’))) }}”

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.

1 Like

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.

"{{ ((states('sensor.stromzukauf_ytd') | float(0) * states('input_number.price_buy') | float(0)) | round(2)) + (states('input_number.price_year') | float(0) / 365 * (as_timestamp(now()) | timestamp_custom('%-j')| float(0)))  }}"
1 Like

Thank you so much :slight_smile: that was the last missing spot then.
Now my problem is solved.

I should learn python then XD

1 Like