Sonoff POW R2 - how to know how I consumed in a day or month

Hello guys!

I have a Sonoff POW R2 (original fw) and I’m trying to get some information, but I don’t know if i´m gonna get having the device with original firmware.

The main question is: Is it possible to extract from the device more info than the power, current, and voltage?

This 3 information I have working fine, but I thought I just had to multiply the power information and the rate, so I would have monthly and daily rate (of course, using utility_meter)

This is my lovelace:
Capturar

Capturar2

You can see, only today 22.5Kwh … that’s impossible… I would like a way to capture the daily info from the device or ewelink app …
Checking my ewelink app, today was only 0.15, not 22 …

Do you guys have any ideas?

Thank you in advance.

#CONFIGURATION.YAML
sonoff:
  custom_components
  username: [email protected]
  password: XXXXX
  reload: always
  sensors: [power, current, voltage]

utility_meter:
   monthly_energy:
    source: sensor.sonoff_1000fe1f6c_power
    cycle: monthly
   daily_energy:
    source: sensor.sonoff_1000fe1f6c_power
    cycle: daily
```

#SENSORS.YAML

```
      pow_mothly_cost:
        friendly_name: Custo Mensal 2 - integration do K
        value_template: "{{ (states('sensor.consumo_sonoff_monthly_in_KWH') | float * 0.62) | round(2) }}"
        unit_of_measurement: 'R$'

  - platform: integration
    source: sensor.monthly_energy
    name: consumo_sonoff_monthly_in_KWH
    unit_prefix: k
    round: 1 
```

You are using the incorrect sensor for the utility meter. You need to use an energy sensor, not power.

The POW2 does have a total energy consumed sensor that you can feed to utility meters for weekly/monthly/yearly cycles. It also has total energy consumed sensors for today and yesterday.

I do not know if/how these are available on the standard firmware. Tasmota certainly has them:

https://tasmota.github.io/docs/devices/Sonoff-Pow-R2/#telemetry

What sensors do you see in the Configuration / Integrations page for the device?

Thank you for the quick reply my friend

That´s the problem… sonoff provides me only on/off, current, power and voltage… there´s no “energy sensor”… that´s why I’m using the power info to do all the math :slight_smile:

I know tasmota is much better, but my pow r2 is a little but inaccessible, behind heavy furniture, etc…

I just realised you are using a custom component to connect to your device. What happens if you change your sensors: config to this and restart?

sonoff:
  custom_components
  username: [email protected]
  password: XXXXX
  reload: always
  sensors: [power, current, voltage, energy]

(I obscured your email address as well).

This is just a guess as I don’t know what custom component you are using to check if that is a valid option.

I don’t think that custom components offers energy sensor, as (I believe) one could only turn existing attributes into sensors.
But, yeah, @Guilherme_Gimenes , I’m interested in the result of the test that Tom suggested above.

Searching through the internet… would this help at all? Let us know.

Hi there.

The only way I found to have my sonoff devices linked to HA was to use this custom component I got here:

I just downloaded and copied the folder “sonoff” with all its contents to my folder “custom_components” and then added the code you just mentioned. Always worked perfectly with my 4 basicR2 and 2 TX switches.
Is there any other way to have them working with HA?

I just checked the link I pasted here and it says there´s no energy option …:frowning:

This is exactly the custom component I´m using. It works, but it seems there´s no energy option.

Most people I see that use the energy option have their sonoff POW with tasmota fw.

All is not lost.

You can use the integral sensor to convert power to energy:

Then use that in the utility meters.

I´m already doing that. Actually it only converts W to Kwh, and then I multiply by the rate I have. The issue is: I´m still using the power sensor, not the energy, got it? I have to find a way to get a energy sensor… and I´m not sure If I will be able to get it having the original fw.

for example, using power sensor, converted to K, 759KWh in one day LOL … times 0.62, 472 bucks in one day.
Capturar

The integral of power with respect to time is energy. The output of the integral sensor is energy. You have an energy sensor, use it in your utility meter. Many people do this. Search the forum.

My custom component provides me only voltage, power and voltage. If I use the integration, it will convert K to KWh… It is doing that… but it shows like 400KWh in one day… checking my app, the correct number should be like 2KWh a day. If my component provides also energy, I would use it with the integration, to convert to KWh, and then multiply by the rate to know the cost. I´m using incorrectly the power sensor as the source of the information I need.

No you wouldn’t. An energy sensor would already be measured in kWh. No need to integrate it.

Ok let’s fix that. What method are you using for the integration?

Read this note in the document: https://www.home-assistant.io/integrations/integration/

method string (optional, default: trapezoidal)

Riemann sum method to be used. Available methods are trapezoidal, left and right.

In case you have an appliance which produces spikey consumption (like an on/off electrical boiler) you should opt for the left method to get accurate readings.

Particularly that last sentence.

The link I had leads you to an example. Are you saying that example is not working?
Copying and pasting:

sensor:
- platform: template
  sensors:
    today_consumption:
      friendly_name: Today consumpion
      unit_of_measurement: kWh
      value_template: "{{ state_attr('switch.sonoff_1000abcdef', 'consumption').0 }}"
    ten_days_consumption:
      friendly_name: 10 days consumpion
      unit_of_measurement: kWh
      value_template: "{% set p=state_attr('switch.sonoff_1000abcdef', 'consumption') %}{{ p[:10]|sum if p }}"

I mean, if this sonoff.update_consumption service is not working somehow, I suppose you could open a ticket…?

Thank you very much @k8gg ! it worked!
My sensors.yaml

      custo_energia_mes_simples: # teste de valor multiplicando o mensal pelo valor. Está funcionando corretamente, mas não possui botão dinamico para informar o valor. 
        friendly_name: Custo Mensal 2 - valor no codigo
        value_template: "{{ (states('sensor.consumo_mes_kwh') | float * 0.62) | round(2) }}"
        unit_of_measurement: 'R$'

      consumo_dia_kwh:
        friendly_name: Consumo DIA # marcação do consumo diário do SONOFF POW R2 >> é necessário "call service" sonoff:update_consumption, em Developer Tools\Services para update. Usar automation
        unit_of_measurement: kWh
        value_template: "{{ state_attr('switch.sonoff_1000fe1f6c', 'consumption').0 }}"
      consumo_mes_kwh:
        friendly_name: Consumo MÊS # marcação do consumo diário do SONOFF POW R2 >> é necessário "call service" sonoff:update_consumption, em Developer Tools\Services para update. Usar automation
        unit_of_measurement: kWh
        value_template: "{% set p=state_attr('switch.sonoff_1000fe1f6c', 'consumption') %}{{ p[:22]|sum if p }}"

Now I know how much I´m spending daily. The only issue is that I cannot know monthly, only the past 30 days… and now for example, if I set to 30, it will show me 23 days this months + 7 days last month… it shows me periods, not the whole closed month… no big deal.
Capturar

To have it working real-time, I have to manually call the service “sonoff:update_consumption”, so the next step is to create an automation to have the sensors updating, I don’t know, every hour maybe.

Thank you once again!

From the Utility meter docs:

So set the cycle to monthly if you want monthly totals and it will reset on the 1st of every month.

Also keep in mind:

Sorry, @tom_l , I´m still a newbie, that´s why I ask basic questions here. (and sometimes the English language is a problem too)

Using the code below, I´m able to get the KWh (daily and monthly), but like I said, it´s a continuous counter, so if I set to 30, on July 1st, it will count the past 30 days, not july 1st onwards.
sensors.yaml:

      consumo_dia_kwh:
        friendly_name: Consumo DIA
        unit_of_measurement: kWh
        value_template: "{{ state_attr('switch.sonoff_1000fe1f6c', 'consumption').0 }}"
      consumo_mes_kwh:
        friendly_name: Consumo MÊS 
        unit_of_measurement: kWh
        value_template: "{% set p=state_attr('switch.sonoff_1000fe1f6c', 'consumption') %}{{ p[:30]|sum if p }}"

cap1
2 new entities created:
cap2

So, you are saying I must add to my configuration.yaml:

utility_meter:
  consumo_mensal_utility_meter:
    source: ??????
    cycle: monthly

but the question is: which entity / source should I use? switch.sonoff_1000fe1f6c (main), sensor.consumo_dia_kwh (day) or sensor.consumo_mes_kwh (month) ?

Is there no continuous energy consumption meter that does not reset?

Ideally that is what you want to feed the utility meter.

But, the utility meter is clever and only reacts to an increase. So it shouldn’t matter if the sensor you use for it resets occasionally.

Say you have a utility meter with a yearly cycle, if on day 30 you get an increase from 90 to 95 in the sensor you feed the utility meter, it will add 5 to the utility meter value. The next day the sensor resets to 0 then increases to 7. The utility meter still adds 7 to its total.

If all you have is the 30 day sensor you should still be able to use that for any cycle length in the utility meter.

just need to add this to configuration file?

I will explain what I did so far:

My sensors.yaml:

      consumo_dia_kwh:
        friendly_name: Consumo DIA # marcação do consumo diário do SONOFF POW R2 >> é necessário "call service" sonoff:update_consumption, em Developer Tools\Services para update. Usar automation
        unit_of_measurement: kWh
        value_template: "{{ state_attr('switch.sonoff_1000fe1f6c', 'consumption').0 }}"
      consumo_mes_kwh:
        friendly_name: Consumo MÊS # marcação do consumo diário do SONOFF POW R2 >> é necessário "call service" sonoff:update_consumption, em Developer Tools\Services para update. Usar automation
        unit_of_measurement: kWh
        value_template: "{% set p=state_attr('switch.sonoff_1000fe1f6c', 'consumption') %}{{ p[:25]|sum if p }}"

with the code above, 2 entities were generated:
1 - sensor.consumo_dia_kwh: 1-day count - it works for me
2 - sensor.consumo_mes_kwh: 30 days count - it doesn’t work for me, because I need the month (right now June, not the past 30 days including 5 days of May)

notice that my code is “25” instead of 30… I did it on purpose today, because today is 25th and now I´m able to see the the whole June

My configuration.yaml, I added the code:

utility_meter:
  consumo_mensal_utility_meter:
    source: sensor.consumo_mes_kwh
    cycle: monthly

I´m using as a source, “sensor.consumo_mes_kwh” and a new entity was created:
sensor.consumo_mensal_utility_meter

And I´m using this entity, hoping it will work. I will wait until 1st of july, to see if the counter will start over… right now, it is increasing every day… if it is 1kwh, and the day increases 1.5, this entity also increases 1.5 becoming 2.5 …let´s see what´s gonna happen

It didn´t work, in parts.

I´m able to capture the daily energy, and it resets everyday. (If I set to “1” instead of “0”, it shows me yesterday´s energy) :grinning:

sensors.yaml:

      energy_day_kwh:
        friendly_name: Day Energy 
        unit_of_measurement: kWh
        value_template: "{{ state_attr('switch.sonoff_1000fe1f6c', 'consumption').0 }}"
      consumo_mes_kwh:
        

and I´m able to get the energy from the past 30 days (today is July 2nd, so I´m getting 28 days from June and 2 from July)

      energy_month_kwh:
        friendly_name: Month energy
        unit_of_measurement: kWh
        value_template: "{% set p=state_attr('switch.sonoff_1000fe1f6c', 'consumption') %}{{ p[:30]|sum if p }}"

so far so good. What I did next to capture the current month, excluding days from the previous month:
configuration.yaml:

utility_meter:
  Energy TEST:
    source: sensor.energy_day_kwh
    cycle: monthly

Check below: The day is correct, I just checked the app and it is the same
But… The month is wrong. Today is July 2nd, and the number should be 2.42 (yesterday) + 1.19 (today) = 3,61 … instead of 1.14 …
Capturar

Any idea what I’m doing wrong?