Hi,
I have the following problem: My EV charger software tracks total energy charged as well as energy charged from the grid and sends those numbers to MQTT which I can then use to define a sensor in Home Assistant. So far so good. The problem is that the software resets the numbers to 0 whenever it is restarted. However, I need totals not just since the last restart but over the entire year. Now my question: How can I define a sensor which sums up all the energy values from the software even if the software resets its own counters to 0 every once in a while?
Thanks for your help!
Use a utility meter with a yearly cycle.
As long as you leave the default net_consumption: false
it will only ever count up, until it resets after a year.
Thanks for the reply! Unfortunately, I cannot get it to work. I have setup MQTT sensors for the input values from my charger. They are both defined as “device_class: energy” and “state_class: measurement”. However, when trying to setup the utility_meter, I get the following error in my logs:
Logger: homeassistant.components.sensor
Source: util/dt.py:185
Integration: Sensor ([documentation](https://www.home-assistant.io/integrations/sensor), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+sensor%22))
First occurred: 4:38:26 PM (3 occurrences)
Last logged: 4:38:27 PM
* Error adding entities for domain sensor with platform utility_meter
* Error while setting up utility_meter platform for sensor
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 428, in async_add_entities await asyncio.gather(*tasks)
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 691, in _async_add_entity await entity.add_to_platform_finish()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 776, in add_to_platform_finish await self.async_added_to_hass()
File "/usr/src/homeassistant/homeassistant/components/utility_meter/sensor.py", line 512, in async_added_to_hass dt_util.parse_datetime(state.attributes.get(ATTR_LAST_RESET)) File "/usr/src/homeassistant/homeassistant/util/dt.py", line 185, in parse_datetime return ciso8601.parse_datetime(dt_str)
TypeError: argument 1 must be str, not None
Show us the yaml for the sensor you are using as input.
Here you go:
sensor:
- name: "Ladeenergie Wallbox gesamt EVCC"
icon: mdi:ev-station
state_topic: "evcc/site/savingsTotalCharged"
unit_of_measurement: "kWh"
device_class: energy
state_class: measurement
That should be
state_class: total_increasing
Thanks! And that still works even though the sensor will reset to 0 every once in a while?
Edit: Even after changing it to “total_increasing” it is not working. I’m getting the same errors as before. The input sensors however, work just fine. Don’t know what the problem is.
Yes it will be fine.
See my edit above. It is still not working. I get the same errors as before.
I’m guessing you can’t show your Utility meter config because you are using the UI?
No problem. I’m still one of those long-time users who do almost everything in YAML.
Nothing really special in the utility_meter config:
utility_meter:
ladeenergie_wallbox_gesamt:
source: sensor.ladeenergie_wallbox_gesamt_evcc
cycle: yearly
ladeenergie_wallbox_netz:
source: sensor.ladeenergie_wallbox_netz_evcc
cycle: yearly
We’re a dying breed.
Yeah that looks fine.
Yes, unfortunately we are.
What is strange to me is the fact that I have a gas consumption sensor together with a daily utility_meter which is configured exactly the same way. The gas consumption is a mqtt sensor with “state_class: total_increasing” and the utility_meter is based off of that mqtt sensor with “cycle: daily”. Works perfectly. I have no clue why the other utility_meter for the charger are not working.
The only real difference I see in mine is it’s typecast as a float and uses state class of total. Perhaps you aren’t being fed a number even if it looks like a number?
- state_topic: "event/metering/summation/minute"
name: "Household Electricity Usage"
icon: mdi:transmission-tower
unit_of_measurement: 'kWh'
# the Energy Bridge returns "Watt-minutes" every minute in the "value"; convert it to kilowatt-hours
value_template: "{{ value_json.value | float / 60000 }}"
# the "time" in the message is a Unix-like timestamp (in milliseconds) of the start of the last reading
last_reset_value_template: "{{ now().fromtimestamp(value_json.time / 1000).replace(tzinfo=now().tzinfo) }}"
device_class: energy
state_class: total
Sometimes yaml works in mysterious ways!
How can I find out? Or is there a way to convert a potential string into a number when defining the MQTT sensor? I just can’t get it to work. Really frustrating.
You do need default values for your filters.
value_template: "{{ value_json.value | float(0) / 60000 }}"
If anything was introducing an error I’d say it was this:
last_reset_value_template: "{{ now().fromtimestamp(value_json.time / 1000).replace(tzinfo=now().tzinfo) }}"
Do you have an example of value_json.time
?
In my example | float turns a string it into a decimal. If I’m unsure whether I will be presented a number or a string I’ll just typecast a value to be safe. Can you show us the MQTT output?
I’m not having any errors. That was just an example of what I was doing.
{
"type": "minute",
"time": 1663428660000,
"local_time": "2022-09-17T11:31:00",
"value": 3541.7472916666666
}