Cost calculation errors

Hello,
i am new to HA, but i did some changes, for example i actually changed the configuration structure from configuration.yaml to folder.

So i have a folder named sensor and a folder named template.
There i have my waste_collection in it in different .yaml files and they are working perfect.
I added them with the following lines to my configuration.yaml
sensor: !include_dir_list sensor
template: !include_dir_list template

So far, so good.

Now i started creating a new sensor for my eg export “cost/earning”.

So i added a earn.yaml to my sensor folder with the following lines:

  - platform: template
    sensors:
      entity_1:
        friendly_name: "STR1"
        value_template: "{{ states('sensor.growatt_today_s_grid_export1') | float }}"  # Wert von Entity 1
      entity_2:
        friendly_name: "STR2"
        value_template: "{{ states('input_number.aktueller_strompreis_export') | float }}"  # Wert von Entity 2
      multiplication_result:
        friendly_name: "STRResult"
        value_template: "{{ state_attr('sensor.entity_1', 'value') * state_attr('sensor.entity_2', 'value') | round(2) }}"

But i receive a warning when i do the .yaml check.

Konfigurationswarnungen
Invalid config for 'sensor' at sensor/strreult.yaml, line 1: expected a dictionary '', got [{'platform': 'template', 'sensors': {'entity_1': {'friendly_name': 'STR1', 'value_template': "{{ states('sensor.growatt_today_s_grid_export1') | float }}"}, 'entity_2': {'friendly_name': 'STR2', 'value_template': "{{ states('input_number.aktueller_strompreis_export') | float }}"}, 'multiplication_result': {'friendly_name': 'STRResult', 'value_template': "{{ state_attr('sensor.entity_1', 'value') * state_attr('sensor.entity_2', 'value') | round(2) }}"}}}]

I have created a helper with “input_number.aktueller_strompreis_export” there i have a value “0,081 EUR” inside.

Then i have the entit from my inverter named “sensor.growatt_today_s_grid_export” with the Value 59,1 kWh eg for today.

I dont get it working to multiply the two numbers and getting a new entity to use for my dashboard.

Does anyone have an idea what is wrong here ?

Thank you in advance.

You basically create 2 sensors, with state, then you create a “result-sensor” from state_attr

try
state:>
states * states

Template - Home Assistant.

PS: Test your templates in
/Developer-Tools/Templates

Templating - Home Assistant.

I changed it too:

  - platform: template
    sensors:
      entity_1:
        friendly_name: "STR1"
        value_template: "{{ state_attr('sensor.growatt_today_s_grid_export1') | float }}"  # Wert von Entity 1
      entity_2:
        friendly_name: "STR2"
        value_template: "{{ state_attr('input_number.aktueller_strompreis_export') | float }}"  # Wert von Entity 2
      multiplication_result:
        friendly_name: "STRResult"
        value_template: "{{ state_attr('sensor.entity_1', 'value') * state_attr('sensor.entity_2', 'value') | round(2) }}"

In Developer-Tools i get:

TypeError: state_attr() missing 1 required positional argument: 'name'

Right, read the url’s i gave you, as you apparently don’t followed my advice, and you don’t seems to understand the difference of state VS state_attributes

PS: and don’t use friendly_name

Yes you are right, i dont understand, i tried the information you gave me, i am new to HA and the full doc is in english, i try my best to understand, believe me.

Ok, well as it did look like you had “copied” something, you didn’t understand, i suggested reading the docs( Which you can’t have read by now :wink: , or you have “missed” the examples etc

Providing you with a solution, will not help you understanding

So take each template 1 by 1, in the Template-editor, don’t start with state_attr, start with simple “state”, until you get familiar with providing/retrieving “states”

Not sure where you got the “value_template” from, but skip this in this context
You want to create sensors with a “state”

PS: If you use i.e FireFox you can click the menu-list in top-right, then click translate-page(from english to spanish) … i.e as i dont understand spanish, i can’t say how well this translation works :slight_smile:

Read your first URL then — the OP is using the legacy format for template sensors, under the top level sensor.

Right, and you believe he found and used this “Option” as his first attempt, regardless of the “not recommended” note ?

No, it will have been copied from old posts or tutorials, or generated by AI.

“durbar”?! Should have been “format”, stupid autocorrect.

1 Like