Energy tab, import and export on same sensor?

Hi all,

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:


You can see rather than showing the purple export it just shows the lines still in the blue as export.

Here are the sensors from the sensor:

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!

1 Like

You need to split that into two sensors, both positive. There are many posts on how to do this on the forum. e.g. How to split a sensor into positive and negative sensors

1 Like

yep … if one does like longlish code :slight_smile:

I still prefer 1 of these lines within the “{% if …” sections of the templates

{{ max (0, 0 - states('sensor.grid_power') | float(0)) }}
{{ max (0, states('sensor.grid_power') | float(0)) }}

since it doesn’t ask for an “{% else %}” section

2 Likes

It’s a cookbook post. It is meant to be as easy to understand as possible. Those variants are listed in following posts.

2 Likes

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.

1 Like

No, don’t do that. Just make the two template sensors as per the cookbook topic.

3 Likes

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:


I need to split this into two kWh, one adding the rises in kWh and another adding the reductions.

I think anyway :confounded:

1 Like

I have it on good authority (TheFez) that this will do what you want:

configuration.yaml

template:
  - triggers:
      - trigger: state
        entity_id: sensor.your_net_energy_sensor_here
        not_to:
          - unknown
          - unavailable
    sensor:
      - name: Import Energy
        unique_id: 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: Export Energy
        unique_id: 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 %}
          {% endif %}
        attributes:
          last_valid_state: "{{ trigger.to_state.state }}"
1 Like

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 :heart_hands:).

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 }}"