Smart-Me energy montiroing template sensors

Hi Michael

Very interesting! How did you modify the standard component with this?

I have tried looking it up, but I have not found a good way of doing it.

Thanks
Mikkel

Hi All!
I Was thinking - regarding theese spikes.
How often do you poll the smartme service?
because i dont get them in my data.

Also i have been looking into the modbus/tcp configuration.
once i enabled it on my device - via postman and the ocnfig api
i was able to read data via a. 3. party program directly on the device.
however i cant seem to find a register that holds the meter-reading.
I have asked smart-me - but haven’t heard from them yet.

That being set - i have not been able to read from the device inside
home-assistant yet.
this is how far i have got:

modbus:
  - name: elmodbus
    type: tcp
    host: 10.0.0.104
    port: 502
    sensors:
      - name: modbusel
        #slave: 1
        address: 0x2004
        #input_type: holding
        #unit_of_measurement: kw
        state_class: measurement
        data_type: string
        #count: 1
        #scale: 0.1
        #offset: 0
        #precision: 1
        #data_type: integer

but so far no luck.
i used the api documentation for refrence:

right now it’s the hex-value
but i also tried converting it into an integer - without luck.

br Ronni

Hi All!
I finally took some time to look into the modbus setup - to avoid polling the cloud service.
I managed to get it working - after some trial and error.

So basically all you have to do is to enable the modbus interface.
This opens port 502 on the devices - and it enables you to communicate via modbus/tcp directly to the device.

you can enable this via the rest api - as described earlier in this thread - if you cant make it work - let me know - and i will try to help out.

after this - you can create the sensors in you config file.
i have pasted my config in here - where i choose to show the current usage as watt - but you can change the this to KW if you like that better.
i am still to have the energy dashboard confirm that the scale of the counter reading is correct.
but i think it looks right.

modbus:
  - name: "kamstrup"
    type: tcp
    close_comm_on_error: false
    host: 10.0.0.104
    port: 502
    delay: 2
    sensors:
      - name: "Kamstrup Total Watt"
        unique_id: "kamstrup_total_watt"
        device_class: power
        address: 8195
        count: 2
        input_type: holding
        unit_of_measurement: W
        scale: 1
        precision: 2
        data_type: int32
      - name: "Kamstrup Counter"
        unique_id: "kamstrup_counter"
        device_class: energy
        state_class: total_increasing 
        address: 8267
        count: 2
        input_type: holding
        unit_of_measurement: kWh
        scale: 0.001
        precision: 2
        data_type: int32

some of the values are boilerplate - so they might be unnessecary - but it helped me in making them work.

Right now the default update reading i 15sec - which i think is ok for me - and alot better than the 60 seconds the cloud api offered.

Have a nice day :slight_smile:

2 Likes

Hi Hostrup,

How did you find the correct adress for kamstrup?
When I read the TCP documentation (smart-me wiki - Modbus TCP) and convert eg. Active Energy Import Total which is 0x204C then I get 8268 - it should be 8267

Do I always substract 1 or how do you get the right adress?

I use this (but I dont feel its correct):
- name: Kamstrup Total Export
state_class: total_increasing
unit_of_measurement: kWh
count: 2
address: 8269
scale: 0.001
precision: 2
device_class: energy
data_type: int32

Hi Peter!
yes it would seem so.

they actually state that the internal address is -1
so i simply took the address eg. 2004 which is 8196 - and then i substracted 1
which is the one for total watt etc.

i dont have solarpanels - so cant verify the export part - but you can always compare with the app.
also -please remember the correct scale.

Hi Hostrup,
Thanks for the fast response.

Remember the correct scale - what do you mean when you mention scale?

You still dont experience these outliers in your reading?

I experienced it and are trying with a filter (outliers) to see if I can filter it away

Hi!
By scale i mean: scale: 0.001
so that you get the correct scale ( according to the value you read ).
With regards to the spikes - then no.
my data seems pretty consistant.
do you also get the spikes on the smart-me app / webinterface?
and as i wrote earlier - i have no solarpanels - so i dont export any power -
maybe it has something to do with that?

br Ronni

Hi all :slight_smile:

I have been away, from this community, for quite some time. But I have had some time to further investigate the issue with the “spikes”.

There are spikes on the smart-me web-interface.

I then changed the setup, so that it used the Modbus TCP protocol.
Using Wireshark, I could detect the same kind of spikes, in the network data.

It appears that the spikes originate either from the smart-me meter, or the Kamstrup meter.
I’m just not sure how to locate the actual source of the problem.
I’m not too keen on the idea of spending money on a new smart-me unit, if the issue is with the Kamstrup meter.

Hi Hostrup,
Thanks for your good input :slight_smile:

I am trying through the webinterface now and see if that improve the situation.

Hi Michael,

I had the spikes as well when I use the data directly from the Smart-Me/Kamstrup.
I am now trying through the webinterface instead and so far I have not seen the spikes.




Hi @hostrup
I got this peak today and I can see from the smart-me app that it have a even higher peak. Its very clear in the export section as I have not produced any power but exported 66.7kWh
The Import is properly correct (I did charge the car during the night)

So not completely solved, I hope its a one timer.

I have ordered a Saveeye and will test that one as well.

Hi Peter!
OK - it’s strange what happens - again - i have no “export” because i only consume power - as i dont have any solarpanels.

If you go to the smart-me homepage - and login there - you have the option to export your data - in both excel and csv format.

maybe you can try to export your data - and see how it looks there?
if there is an error on your Kamstrup meter - you should contact the utility company.
But - i guess your billing is ok - so my suspecion is that your smart-me meter might be defective.

have you tried to contact smart-me support? maybe they can add som value - as to why they think the spikes apear?

br Ronni

Hi Ronni,
It have actually not happened the last 3 weeks, I will cross my fingers and hope that everything is good. If I experience it again I will try to do as you say, look into the values I can export from Smart me and see there the problem is.
Br,
Peter

Hi Peter!
Great to hear.
I can tell that my integration also runs pretty smooth.
i use the modbus/tcp connection.
sometimes it “drops” the connection - and then i have to restart HA to make the connection available again. besides that - it works perfect.
i did play arround with the options for the modbus integration - but i haven’t found a 100% bulletproof connection type yet. :slight_smile:

@hostrup

thks for sharing this. Have installe smart-me based on your setup. All wroks fine except being able to add it into the energy tab:

I have used your setup in my templates.yaml file:

- sensor:
    - name: "El forbrug aktuelt stat"
      unit_of_measurement: kW
      state: "{{ states('sensor.el_forbrug_aktuel') }}"
      attributes: 
        device_class: power
        state_class: measurement        
    - name: "El MĂĽler stat" #the one that i use on the energy tab
      unit_of_measurement: kW
      state: "{{ states('sensor.el_forbrug_total') }}"
      attributes:
        device_class: "energy"
        state_class: "total_increasing"

but get the following error:

Logger: homeassistant.components.sensor
Source: components/sensor/__init__.py:597
Integration: Sensor (documentation, issues)
First occurred: 2:35:59 PM (1 occurrences)
Last logged: 2:35:59 PM

Sensor sensor.el_forbrug_aktuelt_stat has device class None, state class None and unit kW thus indicating it has a numeric value; however, it has the non-numeric value: unknown (<class 'str'>); Please update your configuration if your entity is manually configured, 

Any idea what I am doing wrong

Marinus

@hostrup

Hi I’m new here and I’m expermenting a bit with your API integration to the Smart-Me for the Kamstrup meter.
I have solar panels so I also get exports values
Am I the only one who wonders over the the reading “CounterReading” the “CounterReading” is not the value shown on the front of the Kamstrup meter (not on my meter anyway) my meter show the value of “CounterReadingImport”.
The value of “CounterReading” is the “CounterReadingExport” subtracted from “CounterReadingImport”

“CounterReading”: 29747.519999999997,
“CounterReadingImport”: 33714.31,
“CounterReadingExport”: 3966.79,

Rgds Knud

Hi Knud!
I cant test that - as i only have import :slight_smile:

I might have found a workaround to the “spikes” issue… Well at least on my setup :upside_down_face:
Still in a test phase, but no spikes have occured since March 20th.

I noticed that the registers “Active Energy Import Total” and “Active Energy Import Tariff 1” are always identical. “Spikes” often appear in one or the other, but apparently never in both of them, at the same time.

Using a template sensor, and some scripting, I basically do the following:

  • Set a margin of how big a change in the value, is acceptable (In my case: 10kWh - Not even possible for me to use that much energy in 15 seconds, but spikes are always much bigger.)
  • Check the value of “Active Energy Import Total”. If the value is within acceptable limits, use that value.
  • Check the value of “Active Energy Import Tariff 1”. If the value is within acceptable limits, use that value.
  • If none of the registers return “valid” data, use the templates previous value.

This is my setup:

modbus:
  - name: "kamstrup"
    type: tcp
    close_comm_on_error: true
    host: HIDDEN
    port: HIDDEN
    delay: 2
    sensors:
      - name: "Kamstrup -  Active Energy Import Total - kWh"
        unique_id: "kamstrup_active_energy_import_total_kwh"
        device_class: energy
        state_class: total_increasing
        address:  8267
        count: 2
        unit_of_measurement: kWh
        scale: 0.001
        precision: 2
        data_type: int32
      - name: "Kamstrup -  Active Energy Import Tariff 1 - kWh"
        unique_id: "kamstrup_active_energy_import_tariff_1_kwh"
        device_class: energy
        state_class: total_increasing
        address:  8271
        count: 2
        unit_of_measurement: kWh
        scale: 0.001
        precision: 2
        data_type: int32

template:
  - sensor:
    - name: "Kamstrup - kWh"
      device_class: energy
      state_class: total_increasing
      unit_of_measurement: kWh
      state: >
        {% set margin = 10 %}
        {% set new_kwh_value = none %}
        {% set total_kwh_state = states('sensor.kamstrup_active_energy_import_total_kwh') %}
        {% set tariff_1_kwh_state = states('sensor.kamstrup_active_energy_import_tariff_1_kwh') %}
        {% if total_kwh_state != 'unavailable' and tariff_1_kwh_state != 'unavailable' %}
          {% set previous_kwh_value = state_attr('this', 'previous_state')|float if state_attr('this', 'previous_state') is not none else none %}
          {% set new_kwh_value = previous_kwh_value %}
          {% set total_kwh = total_kwh_state|float %}
          {% if previous_kwh_value is not none %}
            {% if total_kwh is not none %}
              {% set delta_total_kwh = total_kwh - previous_kwh_value %}
              {% if (delta_total_kwh|float >= -(margin|float)) and (delta_total_kwh|float <= (margin|float)) %}
                {% set new_kwh_value = total_kwh %}
              {% endif %}
            {% else %}
              {% if tariff_1_kwh_state is not none %}
                {% set tariff_1_kwh = tariff_1_kwh_state|float %}
                {% set delta_tariff_1_kwh = tariff_1_kwh - previous_kwh_value %}
                {% if (delta_tariff_1_kwh|float >= -(margin|float)) and (delta_tariff_1_kwh|float <= (margin|float)) %}
                  {% set new_kwh_value = tariff_1_kwh %}
                {% endif %}
              {% endif %}
            {% endif %}
          {% else %}
            {% set tariff_1_kwh = tariff_1_kwh_state|float %}
            {% if total_kwh|float == tariff_1_kwh|float %}
              {% set new_kwh_value = total_kwh %}
            {% endif %}
          {% endif %}
        {% endif %}
        {{ new_kwh_value }}

Not the prettiest piece of code, but it’s a work in progress :see_no_evil:

Any ideas to improve it, are welcome.

1 Like

Hey guys

I’m in posession of a new power meter which is using smart-me as a gateway.
Besides the total energy consumption (which is working fine), I also have the total consumption splitted up by tariff (T1 and T2). I created the sensors but the energy dashboard somehow isn’t able to properly read it although the sensor seem to be properly configured.

For some reason the energy dashboard takes the total amount and adds it to the consumption resulting in let’s say 100kWh energy consumption for 1 hour.

Did anyone have a similar issue?
I’m currently not using modbus (since it’s behind a paywall), so only API.

Any advice would be welcome.

Please find my sensor configuration and the template below:

rest:
  - authentication: basic
    username: !secret smart-me_username
    password: !secret smart-me_password
    scan_interval: 30
    resource: https://smart-me.com:443/api/Devices/<ID>
    sensor:
      - name: "Stromverbrauch aktuell" #this shows the current use i kw
        value_template: '{{ value_json.ActivePower }}'
        unit_of_measurement: "kW"
        device_class: "power"
        json_attributes:
          - "ActivePower"
          - "Name"
          - "Serial"
          - "ActivePowerUnit"
      - name: "Stromzähler" #this shows the total meter reading - from when the meter was installed
        value_template: '{{ value_json.CounterReading}}'
        unit_of_measurement: "kWh"
        device_class: "energy"
        json_attributes:
          - "CounterReadingT1"
          - "Name"
          - "Serial"
          - "CounterReadingUnit"
      - name: "Stromzähler T1" #this shows the T1 meter reading - from when the meter was installed
        value_template: '{{ value_json.CounterReadingT1}}'
        unit_of_measurement: "kWh"
        device_class: "energy"
        json_attributes:
          - "CounterReadingT1"
          - "Name"
          - "Serial"
          - "CounterReadingUnit"
      - name: "Stromzähler T2" #this shows the T2 meter reading - from when the meter was installed
        value_template: '{{ value_json.CounterReadingT2}}'
        unit_of_measurement: "kWh"
        device_class: "energy"
        json_attributes:
          - "CounterReadingT2"
          - "Name"
          - "Serial"
          - "CounterReadingUnit"
template: 
  - sensor: 
    - name: "Aktueller Stromverbrauch"
      state: "{{ states('sensor.stromverbrauch_aktuell') }}"
      unit_of_measurement: kW
      attributes: 
        device_class: power
        state_class: measurement
  - sensor: 
    - name: "Stand Stromzähler Total"
      unit_of_measurement: kWh
      state: "{{ states('sensor.stromzahler') }}"
      attributes: 
        device_class: energy
        state_class: total_increasing 
  - sensor: 
    - name: "Stand Stromzähler T1"
      unit_of_measurement: kWh
      state: "{{ states('sensor.stromzahler_t1') }}"
      attributes: 
        device_class: energy
        state_class: total_increasing 
  - sensor: 
    - name: "Stand Stromzähler T2"
      unit_of_measurement: kWh
      state: "{{ states('sensor.stromzahler_t2') }}"
      attributes: 
        device_class: energy
        state_class: total_increasing 

And here a picture of the energy dashboard:

EDIT

NVM I realized when looking at the sensor those spikes are indeed returned by the API as already mentioned before my post.
However, it seems that this isn’t an issue when getting the values directly via modbus.

[quote=“Danne46, post:40, topic:339375, full:true”]
Thanks for sharing.Have done copy and past your integration, just filled in email and password and device id.But can’t get it working, problem with entity uid. Tried to fix that but think I am stupid or to new in this. Would be great to get a panel for energy.Using nabu casa green.