Help with template sensor for heating device

Hi,

I’ve been struggling with this for a long time now since moving from history stats to utility energy.

I have sonoff devices that control some electrical heating devices and am trying to calculate the daily and monthly costs based on fixed KWH tariffs.

   - platform: template
     sensors:
       heating_kitchen_monthly:
         friendly_name: "Kitchen heating cost monthly"
         unit_of_measurement: '$'
         value_template: >-
             {{ ((states('sensor.kitchen_heating_monthly_energy_offpeak') | float) * (states('input_number.ute_offpeak_cost') | float ))  | round(2) }}  
         
            
   - platform: template
     sensors:
           kitchen_heating_power:
               friendly_name: "Kitchen Heating Power"
               unit_of_measurement: 'W'
               value_template: >-
                   {% if is_state('switch.sonoff_1000c7019c', 'on') %}
                       1100
                     {% else %}
                       0
                   {% endif %}    
   - platform: integration
     source: sensor.kitchen_heating_power
     name: kitchen_heating_energy
     unit_prefix: k
     round: 2  

If I check these in the template developer tools I see a value of ‘0.82’
‘input_number.ute_offpeak_cost’ = 4.88
I am not sure how it is calculating this when the device has been ‘on’ for 1.76 hours at 1100 W. (I have tried setting this at 1.1 with unit ‘kwh’ also but then get a small number.

Not sure if I’m making myself clear here.
Here I need to see a calculation of 1.76 hours/1100W/4.88 pesos per kwh = 9.447 pesos
Why am I only seeing a value of 0.82 from the sensor:
{{ (states(‘sensor.kitchen_heating_daily_energy_offpeak’)) }} ?
Thanks!


Just about to give up on the energy integration as I just cannot get it to calculate the right kwh costs for my floor heating.
Works spot on with history stats:

  - platform: template
    sensors:
      office_heating_monthly_costs_new:
        friendly_name: Office Monthly Costs New
        value_template: >-
          {{ (((states('sensor.office_heating_monthly') | float) * (states('input_number.ute_offpeak_cost'))  | round(2)) * 1.1) | round(2) }}
        unit_of_measurement: "$"


  - platform: history_stats    
    name: Office Heating Monthly
    entity_id: switch.sonoff_10008f8794
    state: "on"
    type: time    
    start: "{{ now().replace(month=states('input_number.start_month') | int, day=states('input_number.start_day') | int, hour=0, minute=0, second=0) }}"
    end: "{{ now().replace(month=states('input_number.end_month') | int, day=states('input_number.end_day') | int, hour=0, minute=0, second=0) }}"   
    

energy integration:

  - platform: template
    sensors:
          office_heating_power:
              friendly_name: "Office Heating Power"
              unit_of_measurement: 'W'  #(tried 'kwh' here also)
              device_class: energy
              value_template: >-
                  {% if is_state('switch.sonoff_10008f8794', 'on') %}
                      1100
                    {% else %}
                      0
                  {% endif %}    
  - platform: integration
    source: sensor.office_heating_power
    name: office_heating_energy
    unit_prefix: k
    round: 2 

  - platform: template
    sensors:
      heater_office_cost_monthly:
        friendly_name: "Office Cost Monthly"
        value_template: >-
          {{ (states('sensor.office_heating_monthly_energy_offpeak') | float) *  (states('input_number.ute_offpeak_cost')) | round(2) }}
        unit_of_measurement: "$"  
  - platform: template
    sensors:
      heater_office_hours_monthly:
        friendly_name: "Office Hours Monthly"
        value_template: >-
         {{ (states('sensor.office_heating_monthly_energy_offpeak') | float) | round(2) }} 
        unit_of_measurement: "Hrs" 

  office_heating_monthly_energy:
    source: sensor.office_heating_energy
    cycle: monthly
    tariffs:
      - peak
      - offpeak  
    offset:
     days: 11  

Hi,
I’ve been having this problem too.
However I found the reason and the solution to this problem.
My case is very similar to your case.
In my case, I’m trying to create an energy sensor for a fan entitity.
The power meter sonsor for the fan entitiy is a template sensor just like your sensor.

The reason the integrator is not working is because it needs an updated value for the power sensor, so that it can integrate between each value point. However, since the power measurement is not changing, it is also not updating as well. Since the is not an updated new power measurement point, the integrator can not calculate.

The solution is to create a time triggered template sensor and add a small amount of randomization to the power meter sensor.

here is how I configured my template power meter sensor:
I made it to udpate every 2 seconds. My real power meter sensors update power reading every 2 seconds as well.

template:
  - trigger:
      - platform: time_pattern
        seconds: '/2'
    sensor:
      - name: Template PM Living Room Fan Power
        icon: 'mdi:flash'
        device_class: power        
        unit_of_measurement: "W"
        state_class: measurement
        state: >
          {% if is_state("fan.livingroom_ceiling_fan", "on") -%}
            {% if is_state_attr("fan.livingroom_ceiling_fan",'percentage', 16) -%}{{(5.94 + range(-50, 50) | random/1000)|round(2) }}
            {%- elif is_state_attr("fan.livingroom_ceiling_fan",'percentage', 33) -%}{{(7.98 + range(-50, 50) | random/1000)|round(2) }}
            {%- elif is_state_attr("fan.livingroom_ceiling_fan",'percentage', 50) -%}{{(12.99 + range(-50, 50) | random/1000)|round(2) }}
            {%- elif is_state_attr("fan.livingroom_ceiling_fan",'percentage', 66) -%}{{(17.93 + range(-50, 50) | random/1000)|round(2) }}
            {%- elif is_state_attr("fan.livingroom_ceiling_fan",'percentage', 83) -%}{{(23.07 + range(-50, 50) | random/1000)|round(2) }}
            {%- elif is_state_attr("fan.livingroom_ceiling_fan",'percentage', 100) -%}{{(37.14 + range(-50, 50) | random/1000)|round(2) }}
            {%- endif %}
          {%- else -%}
            0.00
          {%- endif %}

This adds a random value from a range of -0.05W to 0.05W. This is also very similar to a real power reading fluctuation.

Then I’m using the integrator as the following:

  - platform: integration
    source: sensor.template_pm_living_room_fan_power
    name: "Template PM Living Room Fan Energy"
    round: 0

using round: 0 is important with the integrator since you don’t want any unnecessary strain on your HA and and any more resolution that a 1Wh is highly unnecessary. Even 1Wh resolution is too much but at least you’ll get a nice energy graph.

In real life, if you use a real power meter, you’ll see that there is some fluctuation in the power readings. That is because resistance of heating coils changes with change in temperature which results in changes in current and thus change in power, also another reason is fluctuations in mains voltage.

I hope this helps you and anyone who wants to create template power and energy meters for their smart devices since entiity states can easily be used to create template energy sensors this way.

Good luck.