Prepay meter - estimate when close to zero - How do i?

I have a sensor that send the kWh i have left on my prepay meter as well as the 1min. average usage.
The data is send to HA once every minute where the sensor count the “red” led flashing, 1000 counts /kWh, and subtract it from the value i load into the sensor.
I update the sensor kWh value from the prepay meter reading every time i buy electricity.

How do i implement a “sensor” that will estimate when my kWh value will reach either zero or close to zero based on by 1min. average usage for me to buy electricity before i run out?

You could implement this equation in a template sensor:

KWh_left / 1min_avg

This will give you hours until you reach zero.

Hi tom_I

That part i understand, should have indicated so, i am not sure how to create a "template sensor that does that.
I have not done a “template” sensor, so i must be missing something…

template:
    - sensor:
      - name: "Time to ZERO kWh"
        unit_of_measurement: "h"
        state: {{ (Remaining kwh / 1min Avarage) }}

The values are derived from mqtt

Hi tom_I

I also tried - state: {{ (sensor.remaining_kwh / sensor.1min_avarage) }} - but the sensor does not even show?

Your template looks okay, though im not sure if it’s indented too much and this may be an issue.

I think you need to use the developer tools template to work on getting the state correct thought.

{{ (Remaining kwh / 1min Avarage) }}

I’m guessing the above is just pseudo code.

{{ (sensor.remaining_kwh / sensor.1min_avarage) }}

This looks it could be correct, without knowing your sensor values. Try this in dev tools.

Not quite. Try this:

        state: "{{ states('sensor.remaining_kwh')|float / states('sensor.1min_avarage`)|float }}"

Replace the entity_id’s with your actual sensors.

Hi tom_I

It seems that i am still doing something wrong.
As i mentioned above, i do not see the sensor in the entries, so i presume my formatting is wrong.
Here is my config file:

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

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

sensor:
    - platform: mqtt
      name: "Remaining kwh"
      state_topic: "powermeter"
      value_template: "{{ value_json.power_remain_kwh }}"
      expire_after: 120
      device_class: "energy"
      unit_of_measurement: "kWh"
      qos: 0
      
    - platform: mqtt
      name: "1min Avarage"
      state_topic: "powermeter"
      value_template: "{{ value_json.power_watts.avg_1min }}"
      expire_after: 120
      device_class: "power"
      unit_of_measurement: "kW"
      qos: 0
      
template:
    - sensor:
      - name: "Time to ZERO kWh"
        unit_of_measurement: "h"
        state: "{{ states('sensor.remaining_kwh')|float / states('sensor.1min_avarage`)|float }}"

The two mqtt sensor work, but he 3rd one does not even show up.

If that’s copy/paste, note that you have a backtick instead of a single quote at the end of the 1min_avarage in the template.

1 Like

I’m not sure how I did it but one of the quotes is the wrong type:

        state: "{{ states('sensor.remaining_kwh')|float / states('sensor.1min_avarage`)|float }}"
                                                                                     ^ This one

Beaten to the punch by Troon. Again.

1 Like

Yes, sorry i just did a copy / paste and changed the values without looking at it.
But i think that is not my issue.
If the state calculation is wrong, i should at least see the “sensor” but with a error value or zero value.
I am not able to see the sensor at all, so i need to resolve that issue 1st and then look at the sensor value.

With that backtick there, the template is invalid syntax preventing the sensor from being created, and there will be an error in the logs. Fix that, reload template entities and your sensor should appear.

Hi Troon
I did update the syntax
I do not have any errors in the log file?
I only see errors from yesterday when i had indent issues.
In the file editor, the tick is green - when i reload the config file i do not get any notification and the log file do not show any error.

I thougt i would at least see a sensor.time_to_zero_kwh with some error.
But i do not, even when i remove the “state:” line from the config.
So, somewhere in the config file i am doing something stupid

Do you get the result you are expecting if you paste the template into Developer Tools / Templates?

{{ states('sensor.remaining_kwh')|float / states('sensor.1min_avarage')|float }}

Does your previous post contain your entire configuration.yaml, with just that single quote character fixed?

What version of HA are you running? The “new” template format you’re using is relatively recent.

Hi Troon

  1. Yes, i do get a result from the “state:” in the Developer Tools / Templates:
    Result type: string
template:
    - sensor:
      - name: "Time to ZERO kWh"
        unit_of_measurement: "h"
        state: "0.3177151515151515"

This template listens for the following state changed events:

Entity: sensor.1min_avarage
Entity: sensor.remaining_kwh

But i still do not see any sensor???

  1. Yes, here is the entire configuration.yaml with the “fixed” quote
# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

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

sensor:
    - platform: mqtt
      name: "Remaining kwh"
      state_topic: "powermeter"
      value_template: "{{ value_json.power_remain_kwh }}"
      expire_after: 120
      device_class: "energy"
      unit_of_measurement: "kWh"
      qos: 0
      
    - platform: mqtt
      name: "1min Avarage"
      state_topic: "powermeter"
      value_template: "{{ value_json.power_watts.avg_1min }}"
      expire_after: 120
      device_class: "power"
      unit_of_measurement: "kW"
      qos: 0
      
template:
    - sensor:
      - name: "Time to ZERO kWh"
        unit_of_measurement: "h"
        state: "{{ states('sensor.remaining_kwh')|float / (states('sensor.1min_avarage'))|float }}"
  1. Latest as this is my 1st attempt at importing external sensors to HA

Version core-2021.5.5
Newest Version core-2021.5.5

I can’t see anything wrong with that — my next step would be to align the template section indenting to the documentation (just move the - sensor: two spaces left):

template:
  - sensor:
      - name: "Time to ZERO kWh"
        unit_of_measurement: "h"
        state: "{{ states('sensor.remaining_kwh')|float / (states('sensor.1min_avarage'))|float }}"

…although this shouldn’t make a difference. I assume you have restarted HA / reloaded template entities?

Hi Troon

Yes, it was a spacing issue…
But the template section does not show that hey should be at the same indent.
This is now working:

template:
  - sensor:
      - name: "Time to ZERO kWh"
        unit_of_measurement: "h"
        state: "{{ states('sensor.remaining_kwh')|float / (states('sensor.1min_avarage')|float) *1000 }}"

Thank you for all your time and affords to assist.

1 Like

For anyone else that is interested in doing something similar, here is the script.
From this i get hours remaining and days remaining before i get to zero based on the current rate.
Next is to use consumption per day or week to get a more realistic value.

template:
  - sensor:
      - name: "Time to ZERO kWh"
        state: "{{ (states('sensor.remaining_kwh')|float / (states('sensor.1min_avarage')|float) *1000) | round(2) }}"
        unit_of_measurement: "hrs"
    
      - name: "Days to Zero kWh"
        state: "{{ ((states('sensor.remaining_kwh')|float / (states('sensor.1min_avarage')|float) *1000) / 24) | round(0) }}"
        unit_of_measurement: "Days"

I would love to know how you setup the KWH remaining. I already have a pulse counter implemented and can read my current consumption. I also have the “Energy” dash board setup using this. I would love to be able to enter my current units and have home assistant work out what is left. Could you please share? Thanks

Hi molesza

At the time i was using an ESP8266 that was, counting the pulses and sending the 1min_average as well as the remaining kWh.
The kWh reading was derived from the value i manually entered into the ESP8266 via a web interface.
The ESP8266 subtracted the “Pulses” from the kWh i manually entered and then fed “Remaining kWh” via mqtt to HA.

I have stopper using the “Remaining kWh” until i can resolve some power issues as we have frequent power outages in South Africa due to a lack of capacity from our State owned Electricity supplier.

Regards
Z69