Template for percent energy use compare to yesterday, week, month and year

Hi!
I’m trying to setup a sensor that shows % energy used compare to yesterday, last week, last month and last year. The math i can solve but i can’t get it to work in a template.

To get the right value I need to divided the energy value from yesterday with the hour, number of days, days in the month and days in the year to get a value that is reliable.

This is my template for the day but i don’t get the right value. Can I get som help to set up a template that givs my a valut if i use more och less % energy compare to last day.

{{ (states('sensor.energy_in_day') | float 
/ states('sensor.energi_senaste_dag') | float) /24*now().hour }}

sensor.energi_senaste_dag = value from yesterday
sensor.energy_in_day = value for today

{{ (states('sensor.energy_in_day') | float / 
states('sensor.energi_senaste_dag') | float) /24 *(now().hour)|round(2)*100 }}

I think this will give me energy used compare to yesterday in %.
Can anyone correct me if I got it wrong?

And I need some help to remove decimals. I only want hole value.

{{ ((states('sensor.energy_in_day') | float / states('sensor.energi_senaste_dag') | float) / 24 * now().hour * 100) | round }}

Thanks, but when i calculate the % in a calculator I get different value. In my calculator it says 34 % but with the template I get 39%. Is the math right in the template?

Manual: 34 %
Template: 39 %

what are you using in your calculation for hour and what hour is it currently?

I use 13 and the clock was 13:45.

The calculation is going to have some error because you’re 3/4th of the way through the hour. You could just forgo using the current hour and just get the percentage.

{% set total_yesterday = states('sensor.energi_senaste_dag') | float %}
{% set total_today = states('sensor.energy_in_day') | float %}
{{ (total_today / total_yesterday) * 100 }}

I did that before. My aim is to get a value that is reliable for the current hour. Therefore I divided with 24 and multiple with current hour. When I do that I get a better value for the current hour.

I can live with som error in the value. Thanks for the template code.

I think there is a problem with the math. Right now I have this value.

Clock: 14:45
Energy_in_day: 67,451
Energi_senaste_dag: 87,166

With the values your code gives me a value of 45

If I put the same value in my calculator i get 1,3265 (32)

87,166/24 = 3,6319
3,6319 * 14 = 50,8468

67,451/50,8468 = 1,3265

Sorry, your initial explanation didn’t highlight your intentions. Now that I understand what your goal is:

{% set today = states('sensor.energy_in_day') | float %}
{% set yesterday = states('sensor.energi_senaste_dag') | float %}
{{ ((today / (yesterday / 24 * now().hour) - 1) * 100) | round }}

Great! Thanks for your help

Hi! I can’t get next mission right. My aim is to calculate how many hour that has past this week. Can you help me?

        {% set week = states('sensor.energy_in_week') | float %}
        {% set last_week = states('sensor.energi_senaste_vecka') | float %}
        {{ ((week / (last_week / **HOUR THAT HAS PAST THIS WEEK** | round }}

Mabye I got it to work. Is this right?

        {% set week = states('sensor.energy_in_week') | float %}
        {% set last_week = states('sensor.energi_senaste_vecka') | float %}
        {{ ((week / (last_week / 168 * (( ( now().hour ) + ( now().isoweekday() - 1)))) * (24) ) -100) | round (0) }}

What day does your week start at? If it starts on sunday the eq is

{% set hours_this_week = now().hour + 24 * now().isoweekday() %}

If you want it to start on monday, you need to subtract 24 hours.

If you want it to start on saturday, you need to add 24 hours.

My week starts at monday.

Right now the clock is 13:34 and with your code i get 39 (13 *3)

In this moment I want to get the hours for 2 days and 13 hours. 24*2 + 13 = 61 hours

ok so

I did!
But the code gives me 39. The hours should be 61.

Monday (24) + thursday (24) + wednesday (13) = 61

I solved it like this
{{ ( now().hour ) + ( now().isoweekday() - 1) * (24) }}

you put the 24 in the wrong spot

in my code you just needed to add it at the end. :man_shrugging:

{% set hours_this_week = now().hour + 24 * now().isoweekday() - 24 %}
1 Like

My bad! I put the 24 in the wrong spot. Sorry