Energy Management in Home Assistant

It should be, are there errors in your logs?

it seem that every time i do a restore the db gets corrupt. i tried it with several backups and get the same error.
“The system will rename the corrupt database file //config/home-assistant_v2.db to //config/home-assistant_v2.db.corrupt.2022-01-28T14:25:15.717211+00:00 in order to allow startup to proceed”

The best advice I can give is to exclude your database from backups or switch to the official maria db addon database.

If you want to properly backup the built in database, your best course of action is to shut down home assistant when backing up that database file (copy/paste). Otherwise there’s always a chance it can get corrupted if a write is occurring during the backup process. From what I can tell, the maria db addon database does not have this issue.

Basically the best method to avoid data corruption is using mysqldump utility. Especially with InnoDB engine in MariaDB.

1 Like

Hey @WolfsW3lp3!

I had the same crappy thing happening to me. I spent freaking ages trying to sort it out. In the end I found a post somewhere that helped…though I can’t remember where. There may be better andf more elegant ways to do it, but after so many days (probably weeks) of fiddling now that it is working I do not want to touch it! :wink:

Here’s my set up:

I have my raw data from the Shelly 3EM which measure three phases. Let’s look at just one of them: sensor.white_phase_energy.

First I filter it to make sure I am not getting garbage from the Shelly. This can happen when the Shelly is momentarily not available or updating, or busy, or whatever…when that happens you don’t want it going spastic:

  - unique_id: energy_white_filtered
    name: "Energy_White_Filtered"
    unit_of_measurement: "kWh"
    device_class: energy
    state_class: total_increasing
    state: >
          {% set value = states('sensor.white_phase_energy') | float(default=0) %}
          {{ value if value else states('energy_white_filtered') }}

As I recall from the post where I stole this yaml, the state: basically says “take the value of white_phase_energy unless it’s bad in which case stick with what you have”… At this point I will only work with my new sensor sensor.energy_white_filtered.

Next I create another template to establish an energy dashboard sensor:

  - unique_id: white_phase
    name: "White Phase"
    unit_of_measurement: "kWh"
    device_class: energy
    state_class: total_increasing
    state: "{{ states('sensor.energy_white_filtered') | float(default=0) | round(2) }}"
    availability: "{{ states('sensor.energy_white_filtered') | int(default=-100000) > -100000 }}"

This block will reject any non integer and ensures that the count can only go up. This might not resolve your drop outs and other weirdness…but it will likely help you ride through it without it making a mess of your consumption stats…

The last step was to edit the Energy Dashboard configuration to use the final entity I created (sensor.white_phase) as the source of your energy consumption data.

In your case I see you also want to do some math on your data - I would do this with the second template sensor (or insert a new sensor in the middle of the two above to make it a three-step process). Definitely don’t do anything until you’ve generate some clean inputs to work with…

Hope this helps!

CP.

1 Like

I use DBeaver (from https://dbeaver.io) on my Windows PC to connect back to HA’s mariadb and it is magic (which means no CLI required).

  1. Configure your mariadb addon to expose TCP 3306
  2. Restart
  3. Point DBeaver to: [HA IP Address]:3306

DBeaver is completely free and allows direct in table edits so you can literally overtype data and then save it so for little tweaks you don’t need to write SQL - just find it, edit it, commit it. Handy if you need to tidy up a few things.

CP.

2 Likes

Yes, it’s good, multiplatform. Also HeidiSQL, free too. Only for Windows, thought.

1 Like

I’m pulling my hair out with this one. I have a serial sensor that gets parsed to show me instant watts. Which works fine.

image

I used this yaml to turn that into a kWh meter.

  - platform: integration
    source: sensor.powerw
    name: House Energy kWh
    unit_prefix: k
    unit_time: h
    round: 2

And that seems to work a treat:

image

You would think that it would just show up in the list and I’d be off to the races with energy right? Well it’s not in the list for some reason. And I can’t figure out why.

Try:

  - platform: integration
    source: sensor.powerw
    name: House Energy kWh
    unit_prefix: k
    unit_time: h
    round: 2
    method: left

Show the attributes. You are most likely missing a required one and will have to customise it in yaml.

This?

Yes you are missing device_class: energy

Use manual customisation to fix Customizing entities - Home Assistant

Thank you very much for this detailed answer!
I came up with another idea which is working at the moment:

    - unique_id: total_solar_producing
      name: 'total_solar_producing'
      device_class: energy
      state_class: total_increasing
      unit_of_measurement: "kWh"
      state: >
        {% if states('sensor.pv_gen_meter')|float != 0.0 and states('sensor.pv_gen_meter_2')|float != 0.0 %}
          {{ states('sensor.pv_gen_meter')|float + states('sensor.pv_gen_meter_2')|float}}
        {% endif %}

Not really sure if this solves my problem but for now I haven’t got a peak or anything else. If I do so again I will definetly try your solution!

I’ve tried that but I get this error when I do it:

Invalid config for [sensor.integration]: [device_class] is an invalid option for [sensor.integration]. Check: sensor.integration->device_class. (See ?, line ?).

You have to edit it in customize.yaml not in sensor.yaml

You need to show your yaml (formatted) and tell us exactly which file it is in for us to help. It looks like you have modified the wrong file or indentation is wrong.

This is my /config/configuration.yaml


# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
no_ip:
  domain: ***
  username: ***
  password: ***
sensor:
  - platform: serial
    serial_port: /dev/ttyACM0
    baudrate: 38400
  - platform: template
    sensors:
      powerw:
        friendly_name: 'House Energy Instant'
        unit_of_measurement: "W"
        value_template: "{{ states.sensor.serial_sensor.state.split(',')[2].split(':')[1]|regex_replace(find='}', replace='', ignorecase=False)}}"
  - platform: integration
    source: sensor.powerw
    device_class: energy
    name: House Energy kWh
    unit_prefix: k
    unit_time: h
    round: 2

You can’t add the device class there. Look at the manual customisation link I posted earlier

Also for completeness I think you should have device_class: power in your setup for powerw

Adding stuff to the energy dashboard never works on the first try. It’s always a edit/customize/reload/restart/Ctrl+F5 struggle back and forth. :sweat_smile:

I now copy paste this block for every sensor into customize.yaml:

sensor.house_energy_kwh:
  device_class: energy
  unit_of_measurement: kWh
  state_class: total_increasing
  last_reset: '2000-01-01T00:00:00'

state_class could also be “total” or “measurement”. Never fully understood it. Trial and error if one of them works for you