How to convert from a counter to "used energy" timeseries

Hi all,
sorry for the newbie question. i’m trying to convert from a tasmota counter datasource sensor.tasmota_counter_c1 wich is counting units of work over time (1/75th) of a kWh for each tick into an entity that will graph energy consumption over time.

  • Power time-series P(W) as function of time (derive Work over time giving power)

  • work (Wh) used per day, (Wh) /hour etc. (Quantize Work into timeslots)

Data should also be good to use it in the energy dashboard (consuption from grid).
The counter ist monotonuos rising.

Maybe someone could point me in the right direhtion.
Thanks in advance

Integrating Your Electrical Grid

History Stats Sensor

Utility Meter

Riemann sum integral

Also, take a look at the DIY Rain Guage thread, you may be able to use a similar combination of sensors to convert your ticks into the different calculations you are trying to get.

1 Like

ok, so now the counter is considerd to be reset in every time period.
now: how do i do math with the value?
how can i convert it to a class that is acceptable in the energy dashboard?

i stumbled uppon samle code hat does give me errors

 Loads default set of integrations. Do not remove.
default_config:

# Text to speech
tts:
  - platform: google_translate

# Example configuration.yaml entry

mqtt:
  discovery: true
  discovery_prefix: homeassistant

  
  sensor:
    name: "Upstairs Temperature"
    state_topic: "homeassistant/test/front-door-pi"
    device_class: "temperature"
    value_template: "{{ value_json.temperature }}"

  - platform: template
    total_power_grid:
    value_template: '{{ states('sensor.tasmota_counter_c1') | float(0) * 0.013333  | round(2) }}'
    friendly_name: 'total power grid'
    unit_of_measurement: 'kWh'



 

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

Hello @michl42 !

I just found this topic, as i was looking for a similar setup. I also have a counter that I need to convert into energy data.

I used a template sensor:

template:
- sensor:
  - name: office_heater_energy
    unit_of_measurement: "Wh"
    device_class: energy
    state_class: total_increasing
    state: "{{ states('counter.office_heater_consumption') }}"

Each time the counter is updated, the template sensor will be updated as well.

After a few minutes, I can say this seems to work : i’m able to add the sensor as an “Energy consuming device” in the energy dashboard.

Also, your template sensor is under the mqtt key. Make sure to fix the indentation:

mqtt:
  discovery: true
  discovery_prefix: homeassistant

  
  sensor:
    name: "Upstairs Temperature"
    state_topic: "homeassistant/test/front-door-pi"
    device_class: "temperature"
    value_template: "{{ value_json.temperature }}"

# No leading spaces 
template:
- sensor:
    - name: total_power_grid
      unit_of_measurement: "kWh"
      device_class: energy
      state_class: total_increasing
      value_template: '{{ states('sensor.tasmota_counter_c1') | float(0) * 0.013333  | round(2) }}'
1 Like

Hi, thanks for the code.
Confiquration in yaml seems a bit difficult to me and config error messages are not that helpfull.
Also Yaml seems to be extremely picky about indentation and AFAIK configuration schema has been changed in the past, so there are al lot outdated samples on the web i fell for.
In my case the value_template keyword sees to be causing an configuration error.
But this seems to work for me:

# Loads default set of integrations. Do not remove.

default_config:

# Text to speech

tts:
  - platform: google_translate

# Example configuration.yaml entry
influxdb:
  api_version: 2
  ssl: false
  host: 192.168.2.xxx
  port: xxx
  token: xxx==
  organization: 9xxxxx
  bucket: home_assistant
  tags:
    source: HA
  tags_attributes:
    - friendly_name
  default_measurement: units
  exclude:
    entities:
      - zone.home
    domains:
      - persistent_notification
      - person
  include:
    domains:
      - sensor
      - binary_sensor
      - sun
    entities:
      - weather.home

mqtt:
  discovery: true
  discovery_prefix: homeassistant

sensor 1:
  platform: mqtt
  name: "ow_Boil_7c"
  device_class: "temperature"
  unit_of_measurement: "C"
  state_topic: "home-assistant/onewire/10xxxc"

...

sensor 14:
  platform: mqtt
  name: "ow_boden"
  device_class: "temperature"
  unit_of_measurement: "C"
  state_topic: "home-assistant/onewire/2xxx9f"
 
template:
- sensor:
  - name: "total_power_grid"
    unit_of_measurement: "kWh"
    device_class: energy
    state_class: total_increasing
    state: "{{ states('sensor.tasmota_counter_c1') | float(0) * 0.013333  | round(2) }}"