Utility Meter abnormal values on calculated sensor

Hello,

I have a strange behatvior on my utility meter that, at some point, starts giving strange values on templated sensors.

For instance, I have a sensor called “total electricity consumption” which is the sum of day & night consumptions, where the utility meter jumps into millions.

Did the same with a simple rounding of my solar production:

      solar_total_production:
        friendly_name: "Electricité solaire produite (total)"
        icon_template: mdi:solar-power
        unit_of_measurement: kWh
        value_template: "{{ (states('sensor.total_yield')|float)| round(2) }}"

On that one, I have simple Utility meters (in a dinstinct yaml):

  yearly_solar_production:
    source: sensor.solar_total_production
    cycle: yearly

  monthly_solar_production:
    source: sensor.solar_total_production
    cycle: monthly

After a few days I end up with values like (total value which is correct):
image

while the utility meter jumps through the roof:
image

As far as I could check it’s not about unit of measurements, I didn’t note any fixed ratio.

Am I doing something wrong?

Whenever the state of sensor.total_yield is unknown, like right after a home assistant restart, the |float filter returns 0 then the template jumps the the correct value when the state is available.

This effectively doubles the the value stored by the utility meter as it sees the increase from 0 to whatever the current state is.

The solution is to use an availability template.

      solar_total_production:
        friendly_name: "Electricité solaire produite (total)"
        icon_template: mdi:solar-power
        unit_of_measurement: kWh
        value_template: "{{ (states('sensor.total_yield')|float)| round(2) }}"
        availability_template: "{{ states('sensor.total_yield') not in ['unavailable', 'unknown', 'none'] }}"
2 Likes

After 24h testing it does work, thanks!

This solution seems to be exactly what I need but I can’t seem to get it to work.

I am trying to avoid the problem whilst also calculating my energy usage to include a standard charge.
The latter bit works fine but I still fall victim to the underlying energy sensor periodically being in an unknown state, which is interpreted as zero and then causes a huge jump when the next reading comes in.

I have tried a couple of things:
Firstly, creating the utility meter based on the raw energy sensor then creating a template based on utility meter to calculate the costs and work around sensor availability:

utility_meter:
    electricity_daily:
      source: sensor.bulb_energy_meter_energy_meter
      name: Daily Electricity
      cycle: daily 
sensor:
  - platform: template
    sensors:
      daily_electricity_total_cost:
        friendly_name: Daily Electricity Total Cost
        unit_of_measurement: "GBP"
        value_template: >
          {{ (states('sensor.daily_electricity') | float * 0.2067 + 0.2410) | round(2) }}
        availability_template:  "{{ states('sensor.daily_electricity') not in ['unknown', 'unavailable', 'none'] }}"

Secondly, creating a template to work around availability, using that for utility meter then using a second template to calculate costs!

utility_meter:
    electricity_reading_daily:
      source: sensor.electricity_reading
      name: Daily Electricity Reading
      cycle: daily 
sensor:
  - platform: template
    sensors:
      electricity_reading:
        friendly_name: Electricity Reading
        unit_of_measurement: "kWh"
        value_template: >
         {{ (states('sensor.bulb_energy_meter_energy_meter') | float ) }}
        availability_template: "{{ states('sensor.bulb_energy_meter_energy_meter') not in ['unknown', 'unavailable', 'none'] }}"

      daily_electricity_spending:
        friendly_name: Daily Electricity Spending
        unit_of_measurement: "GBP"
        value_template: >
          {{ (states('sensor.daily_electricity_reading') | float * 0.2067 + 0.2410) | round(2) }}

Annoyingly both of these approaches have yet to yield any issues when calculating Gas costs (presumably because the sensor has never been unavailable/unknown as yet). Any help much appreciated. I’m fairly new to all this so hopefully someone will spot a schoolboy error in the above!

Did you get this working this is exactly what I’m trying to do.
When you use these in cards do you also get the £ sign after the value like I do ?

Hi, sorry for not noticing your reply until now. I haven’t been able to get it working the way I want, i.e. with utility meters. However, I have managed to get the energy panel showing me all the info I was trying to recreate with my own sensors. Its been working perfectly for the past 4 weeks. The reason I wasn’t using the energy dashboard in the first place was because:

  1. I couldn’t incorporate the daily standing charges
  2. My gas meter integration via Smartthings was causing errors

If either of those problems are relevant to you then…
I found a really helpful post here that I used as a basis for the standing charges and I’ll paste my config below in case that helps too. The Smartthings problems were multiple and included the use of kWh instead of M3 for the units and wrong device class. I managed to just work through these. Again, if this is relevant to you then see my code below.

utility_meter:
    daily_supply:
      source: input_number.daily_kwh
      name: Daily Supply
      cycle: daily
      tariffs:
        - charge
input_number:
  daily_kwh:
    name: Daily kWh
    min: 0
    max: 1000000
    step: 0.001
    unit_of_measurement: kWh
  daily_m3:
    name: Daily m3
    min: 0
    max: 1000000
    step: 0.001
    unit_of_measurement: m³
template:
  - sensor:
      - name: "Gas Meter m3"
        unit_of_measurement: "m³"
        device_class: gas
        state_class: total
        state: >-
          {{ (states('sensor.bulb_energy_meter_gas_meter') | float * 0.08912153) }}
        availability: >-
          {{ states('sensor.bulb_energy_meter_gas_meter') not in ['unknown', 'unavailable', 'none'] }}
      - name: "Electricity Meter"
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total
        state: >-
          {{ (states('sensor.bulb_energy_meter_energy_meter')) }}
        availability: >-
          {{ states('sensor.bulb_energy_meter_energy_meter') not in ['unknown', 'unavailable', 'none'] }}
      - name: "Gas Standing Charge m3"
        unit_of_measurement: "m³"
        device_class: gas
        state_class: total
        state: >-
          {{ (states('input_number.daily_m3')) }}

No worries thanks for the reply.
I have managed to get something working by creating peak and off-peak utility sensors and created additional sensors to add them together and include the standing charge like this and repeated it to weekly and monthly.
But I like the way you’ve done that.
I noticed on your input boolean for electric you put the unit of measurement as kWh is that right ? As I thought that sensor was to calculate the standing charge which will be in GBP ?
I need to put more effort into the energy dashboard as I’ve been trying for days now to get some decent graphs for the utility sensors but can’t get any I like and the apex charts I’ve seen confuses me ha ha.

energy_cost_today:
        unique_id: energy_cost_today
        friendly_name: "Energy Cost Today"
        unit_of_measurement: '£'
        icon_template: mdi:currency-gbp
        value_template: "{{ (((states('sensor.daily_energy_peak') | float * 0.2214) + (states('sensor.daily_energy_offpeak') | float) * 0.1217 ) + 0.2330) | round(2) }}"

Hi, yes, its a bit of a workaround essentially treating the standing charge as if it was another source of kwh. This does mean that your totals will be out by 0.001kwh per day but i can live with that. I didn’t like it initially but after spending ages trying to do something better I gave up!

After 28 days of my energy setup working perfectly, I got a zero registered for Gas late on the last day of February which messed up that month and then messed up March when it started working again a couple of hours later. :weary:
Will try the approach mentioned here (to take the previous value if the current one is zero) to see if this helps. Here’s my updated config:

template:
  - sensor:
      - name: "Gas Meter m3"
        unit_of_measurement: "m³"
        device_class: gas
        state_class: total
        state: >-
          {% set value = states('sensor.bulb_energy_meter_gas_meter') | float %}
          {{ value * 0.08912153 if value else states('sensor.bulb_energy_meter_gas_meter') }}