Cost sensor without gui but only yaml

Hello Again,

After spending loads of time reading and re-reading I am still confused.

I prefer yaml - since the gui option is confusing me more as I cannot see what it automatically creates - and recommendation is: you should not mess with .\storage

So after having had something running in the energy dashboard I am back to square one (including ‘you should use new template syntax’)

I have now something showing in the energy dashboard, but I cannot figure out how to put it together using a cost sensor, which is generated from the UI automatically.

This is what I can see right now:
image

image

no helpers anymore, although I cant still see some entities when I look at stats/entities


Btw vodoo is some woox device which was supposed to work with tuya local - they never got back to me (wrong decision at purchase - woox or tuya -well learn by spending money :-D)

There is my yaml (ultility meter set up like mentioned in various posts, maybe not that important, but still - what does it)

"#Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

#DuckDNS setting for certificate
http:
  ssl_certificate: /ssl/fullchain.pem
  ssl_key: /ssl/privkey.pem

#Customize Yaml for Oil consumption
homeassistant:
  customize: !include heating.yaml

sensor:

  - platform: energy
    name: Oil Consumption Today
    state: "{{ states('sensor.oil_consumption_today')}}" # | float(0) * 2.4 }}”
    unit_of_measurement: m³
    state_class: total_increasing
    device_class: gas

  - platform: energy
    name: Solar Production Today
    state: "{{ states('sensor.solar_production_today')}}" # | float(0) * 1.3 }}”
    unit_of_measurement: kWh
    state_class: total_increasing
    device_class: energy
    
  - platform: history_stats
    name: Oil Consumption Today
    unique_id: oil_consumption_today
    entity_id: binary_sensor.vicare_burner_active
    state: "on"
    type: time
    start: "{{ today_at() }}"
    end: "{{ now() }}"

  - platform: history_stats
    name: Solar Production Today
    unique_id: solar_production_today
    entity_id: binary_sensor.vicare_solar_pump_active
    state: "on"
    type: time
    start: "{{ today_at() }}"
    end: "{{ now() }}"

template:
  - sensor:
     - name: Oelverbrauch
       unique_id: oil_consumption_today
       state: "{{ states('sensor.oil_consumption_today') | float(0) * 2.4}}"

  - sensor:
     - name: Solar Warm Wasser
       unique_id: solar_production_today
       state: "{{ states('sensor.solar_production_today') | float(0) * 9.8}}"
       
  - sensor:
     - name: "Energy Solar Production"
       unique_id: energy_solar_production
       icon: "mdi:flash"
       unit_of_measurement: "€/kWh"
       state: "{{states('sensor.solar_production_today') | float (0) * 1.5 }}"

utility_meter:
  oil_consumption_daily:
    source: sensor.oil_consumption_today
    cycle: daily
  solar_production_daily:
    source: sensor.solar_production_today
    cycle: daily
    
  oil_consumption_weekly:
    source: sensor.oil_consumption_today
    cycle: weekly
  solar_production_weekly:
    source: sensor.solar_production_today
    cycle: weekly
    
  oil_consumption_monthly:
    source: sensor.oil_consumption_today
    cycle: monthly
  solar_production_monthly:
    source: sensor.solar_production_today
    cycle: monthly

  oil_consumption_yearly:
    source: sensor.oil_consumption_today
    cycle: yearly
  solar_production_yearly:
    source: sensor.solar_production_today
    cycle: yearly 

And this is my customization.yaml (heating.yaml):

"sensor.oil_consumption_today:
  device_class: gas
  unit_of_measurement: m³
  accuracy_decimals: 2
  friendly_name: Oel Heizleistung
  state_class: total_increasing

sensor.solar_production_today:
  device_class: energy
  unit_of_measurement: kWh
  accuracy_decimals: 2
  friendly_name: Solar Warm Wasser Produktion
  state_class: total_increasing

I wanted to follow best practice to split and not have everything in one yaml.

I am getting values in the energy dashboard, but they do not reflect the costs.

Oil burner timer = 2.4 L per hour
Solar production = 9.8kwH = 1 L oil

So what I want to achieve is:

Our daily consumption in oil with costs

and

our daily solar ROI with costs.

Can anyone maybe help to get my confusion sorted?
Since the GUI with helpers and utility meters is not for me, although highly recommended, sorry. I’d like to have it in a yaml file - and long term in yaml files split for customizatiion

Yes, i am a beginner. Still. Although I thought with all the help in this community written down I got quite far. So thank you all (all posts all comments, everyone helping out)

PS:ah, btw these different values within the different sensor creations were meant to help me - but obviously they don’t

The first thing I need to say is that you should make sure that your code you post is properly formatted. Otherwise it’s almost impossible for others to read and see errors. Make sure you put three backticks (```) on the line before and line after your code.

You should be able to create a sensor showing the costs in yaml but from what you showed above I see you have consumption but I don’t see the price anywhere. You’ll need that to get cost (cost = usage * price).

Once you get that sorted and properly posted someone should be able to help with the details.

Thank you finity, I tried to properly format but somehow I did not mange. ended up with format errors
So brackets make it readable? Sorry - backticks - ok.

edit: hope it is now properly edited.

About the costs, yes, I just cannot figure out how to set this up without helpers. they confuse me, I have entities/sensors from previous attempts and that is now even mor confusing - tried to get rid of them but they reappear after restart.
Main thing is: how would I set a cost ‘sensor’ or would it be a ‘binary sensor’ that relates to what I have set up? I know everyone needs to find their way - I just would like to see an example of a cost ‘Sensor’? in yaml to be able to follow that approach. I could not find it in the community or my search was wrong…

“you have consumption but I don’t see the price anywhere. You’ll need that to get cost (cost = usage * price).”

Yes, exactly where I am stuck- is that in the same sensor? So that you have to define it in there? Or do you have to create another sensor?

Yes the code looks better formatted now. Thank you.

So do you have an integration that provides you an entity for the price of the oil you are using?

If you don’t have that then there is literally no way for you to do anything manually in HA.

I’m not sure where the “cost” column in the second screenshot is coming from or how it is being calculated. Either it’s built-in to the integration you are using and it doesn’t present you with that information or there is an entity for the price somewhere in your system.

without that data I think you are dead in the water.

But to answer your question more generally…

you would use a template sensor not a binary sensor.

a sensor can return pretty much any value you want as it’s state as long as the state string it returns is not over 255 characters. So it is appropriate for returning a “cost” value (if you can figure out where to get the price as noted)

A binary sensor can only ever be ‘on’ or ‘off’ (well except for undesirable values like ‘unknown’ or ‘unavailable’). So that wouldn’t work for a non binary value.

there are other things I see in looking at your code above.

the first problem I see is that you are trying to create a couple of sensors using the “energy” platform. I don’t believe that exists. You should be using the “template” platform in that case. Or if you want to get into the debate over “legacy” or “modern” syntax you should use the “template:” domain instead of the “sensor:” domain. You seem to be mixing up the syntax between the two in your code.

So we will just use the modern syntax.

the history stats sensors are under the right domain so we will leave them there

the other big issue I see in your templates is that you have too many brackets {{ }} in them.

Here is an example of how you have the template:

state: "{{ states('sensor.oil_consumption_today')}}" # | float(0) * 2.4 }}”

here is a fully corrected syntax for the sensors:

template:
  - sensor:
      - name: Oil Consumption Today
        state: "{{ states('sensor.oil_consumption_today') | float(0) * 2.4 }}”
        unit_of_measurement: m³
        state_class: total_increasing
        device_class: gas

      - name: Solar Production Today
        state: "{{ states('sensor.solar_production_today') | float(0) * 1.3 }}”
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
    
sensor:
  - platform: history_stats
    name: Oil Consumption Today
    unique_id: oil_consumption_today
    entity_id: binary_sensor.vicare_burner_active
    state: "on"
    type: time
    start: "{{ today_at() }}"
    end: "{{ now() }}"

  - platform: history_stats
    name: Solar Production Today
    unique_id: solar_production_today
    entity_id: binary_sensor.vicare_solar_pump_active
    state: "on"
    type: time
    start: "{{ today_at() }}"
    end: "{{ now() }}"

as far as I can see everything else so far should work…I think…

Thank you very much finity. The explanations helped a lot.
The only integration we have is the viessmann for the heating system (API)

But with what you said made me think. So I have no cost availabe for Solar in the energy boad. Also with round (2) for my values I adjusted my sensors. Then I am slightly abusing the ‘grid’ and added my solar production in there. And oops I had EUR values visible for the grid. If at some point I get a hardware sensor to the power grid in the house I can still add that one. The confusion is slowly getting less. Now, I need to see if my calculation is correct, since somehow the values are not yet what I would expect or note down manually. At the moment (we have minus degrees) I should have 20 Liters per day which from what I read equals 20 m³ (1L oil = 1 m³).

I need to have another look at my sensors state.
maybe like this:
state: "{{ (states('sensor.oil_consumption_today') | float(0) ) | round(2) }}

Well, long way to go to get all sorted :smiley:
Thank you so much

It really doesn’t: 1000 L = 1 m³.

Perhaps you read an English site stating 1,000L and assumed that was one litre rather than one thousand?

Hi Troon, Thank you for that correction. Then maybe either I read this wrong or they have false information. :smiley:
#http://pdenergieberatung.de/data/documents/energieverbrauch.pdf#

Then I believe I should to rewrite my stuff and use kwh

Do you happen to know how you could change the time range of a sensor.
To do something like read the states from eg. 5 AM in the morning until lets say 11:59 PM and built a statistic chart that matches the heatings schedule?

Ah, that’s a bit different. They’re saying that a litre of oil contains the same energy as a cubic metre of gas, not that a litre and a cubic metre are the same thing.

You could create a template sensor:

template:
  - sensor:
      - name: sensor with schedule
        state: "{{ states('sensor.source_sensor') if now().hour > 4 else 0 }}"

Thank you I give that a try as well with the sensor.

I thought that using m³ would save me calculating kWh into litres.
And the values of today seem to get close to what we manually note down.

I am still sorting my sensors - had too many approaches., :slight_smile:

seems I missed too many lessons on maths. :frowning:
At the moment I have changed to kWh
the lable on the burner says 2kg/h which from what I understand equals to 22.8 kWh.

How do I do that calculation with that sensor i have?

If I use gas m³ I thought that would be equal to liters, but obviously not.
We have an average of 8 to 10 burner hour, but I only see around 5 kWh.
What am I doing wrong? Maths? How is this internally calculated?
What does homeassistant do when you tell it to use kWh for a device or m³?
What is the difference?