I recently changed my power monitor to a seed studio 2 chanel to monitor mains in/out and solar generation. The bit I am struggling with is the energy dashboard doesn’t register the export correctly.
The main power goes ± on import and export, so the total kWh goes up and down depending on direction. So the display never show exported amount just “negative” consumption. Here is yesterday’s chart:
Not sure how to fix this, the only thing I can think of is to make a couple of helpers that do something like add up based on if power is going in or out but it feels like a brute force solution. So was hoping someone might have a more elegant method before I brute force it all!
Cheers all, I’ll look into the post. I’ve been searching for a few days and just haven’t been sure what terms to search for. I was guessing I’d have to split them out but wanted to make sure I wasn’t missing anything!
I’ll probably make up a script that looks at the current kWh value to the last changed, if positive add the change to the consumed and negative add to exported.
I’m not sure that is going to work for this use case. I am looking at the energy consumed (kWh) not the instantaneous power, the figure doesn’t go negative, it just goes up and down according to wether the power is flowing in or out.
What I think I need is to effectively record the power when it changes, then when it changes again, compare that to the stored value, it it has gone up I add to the kWh for import, if it has gone down, then add to the export. The store that value to repeat the process.
For example, here is the chart for today, you can see at about 10am when the clouds cleared and sun came out, the power was being exported so the total kWh consumed from the grid was going down:
Thanks tom_l, sorry for the delay replying, been work these last few weeks have been ridiculously busy at work, leaving little time for me to tinker.
Have popped that into my configuration.yaml and initially threw an error (there is a second endif in the export section ).
Corrected that and while the sensors have appeared in my devices/entities tab they just say “Unavailable”
Checking the logs I am getting a few errors:
Logger: homeassistant.helpers.sensor
Source: helpers/trigger_template_entity.py:91
First occurred: 14:50:15 (52 occurrences)
Last logged: 14:56:30
Error rendering state template for sensor.import_energy: UndefinedError: 'homeassistant.helpers.template.TemplateStateFromEntityId object' has no attribute 'get'
Error rendering state template for sensor.export_energy: UndefinedError: 'homeassistant.helpers.template.TemplateStateFromEntityId object' has no attribute 'get'
Logger: homeassistant.helpers.template
Source: helpers/template/__init__.py:2357
First occurred: 14:50:15 (52 occurrences)
Last logged: 14:56:30
Template variable error: 'homeassistant.helpers.template.TemplateStateFromEntityId object' has no attribute 'get' when rendering '{% set previous = this.get('attributes', {}).get('last_valid_state', none) or trigger.from_state.state | float(none) %} {% if previous is none %} none {% else %} {% set diff = trigger.to_state.state | float - trigger.from_state.state | float %} {% set current = this.state | default(0) | float(0) %} {{ current + diff if diff > 0 else current }} {% endif %}'
Template variable error: 'homeassistant.helpers.template.TemplateStateFromEntityId object' has no attribute 'get' when rendering '{% set previous = this.get('attributes', {}).get('last_valid_state', none) or trigger.from_state.state | float(none) %} {% if previous is none %} none {% else %} {% set diff = trigger.to_state.state | float - trigger.from_state.state | float %} {% set current = this.state | default(0) | float(0) %} {{ current - diff if diff < 0 else current }} {% endif %}'
I think it is having issues with the “get” but I don’t understand enough about how these commands work in order to diagnose further
Here is the code I am using, the only changes are to include the sensor and the name & id of the import and export sensors
template:
- triggers:
- trigger: state
entity_id: sensor.seeedstudio_2ch_em_bl0939_energy_1
not_to:
- unknown
- unavailable
sensor:
- name: Mains Import Energy
unique_id: mains_import_energy # or generate a UUID here
device_class: energy
state_class: total_increasing
state: >
{% set previous = this.get('attributes', {}).get('last_valid_state', none) or trigger.from_state.state | float(none) %}
{% if previous is none %}
none
{% else %}
{% set diff = trigger.to_state.state | float - trigger.from_state.state | float %}
{% set current = this.state | default(0) | float(0) %}
{{ current + diff if diff > 0 else current }}
{% endif %}
attributes:
last_valid_state: "{{ trigger.to_state.state }}"
- name: Mains Export Energy
unique_id: mains_export_energy # or generate a UUID here
device_class: energy
state_class: total_increasing
state: >
{% set previous = this.get('attributes', {}).get('last_valid_state', none) or trigger.from_state.state | float(none) %}
{% if previous is none %}
none
{% else %}
{% set diff = trigger.to_state.state | float - trigger.from_state.state | float %}
{% set current = this.state | default(0) | float(0) %}
{{ current - diff if diff < 0 else current }}
{% endif %}
attributes:
last_valid_state: "{{ trigger.to_state.state }}"