Multiply sensor value with another value

Hi

I have a few sensors measuring the current power usage and the total consumption since last reset,

What I would like to do is to multiply the value this template sensor is presenting with the cost of electricity, for examle 2kr (swedish currency) per kWh.

Is there any way to add another sensor taking the value from the below template sensor and multiplying it with a given number? (Of course it would be cooler to retrieve the current cost from my power distributor but that would be the next step :slight_smile:

platform: template
sensors:
  total_power_usage:
    value_template: '{{ ((states.sensor.pc_power_2_4.state | float) + 
    (states.sensor.server_power_3_4.state | float) + 
    (states.sensor.frys_power_7_8.state | float) + 
    (states.sensor.kyl_power_8_8.state | float) + 
    (states.sensor.soffa_power_6_8.state | float) + 
    (states.sensor.sovrum_power_10_8.state | float) + 
    (states.sensor.tvattmaskin_power_9_8.state | float) + 
    (states.sensor.tvsovrum_power_5_8.state | float)) | round(1) }}'
    friendly_name: 'Total power usage'
    unit_of_measurement: 'w'
1 Like

Managed to solve it!

This is the code for calculating the cost off the current usage right now, i used the template sensor from the code in my previous post

platform: template
sensors:
  total_power_cost:
    value_template: '{{ ((states.sensor.total_power_usage.state | float * 1 / 1000 *1.885)) | round(2) }}'
    friendly_name: 'Current cost'
    unit_of_measurement: 'kr'

And a sensor for calculating the total cost since last reset, I’ll try to find a way to calculate it on a month basis

The sensor that sums up all power measurements

platform: template
sensors:
  total_energy_used:
    value_template: '{{ ((states.sensor.tvsovrum_energy_5_0.state | float) + 
    (states.sensor.frys_energy_7_0.state | float) + 
    (states.sensor.kyl_energy_8_0.state | float) + 
    (states.sensor.pc_energy_2_0.state | float) + 
    (states.sensor.server_energy_3_0.state | float) + 
    (states.sensor.soffa_energy_6_0.state | float) + 
    (states.sensor.sovrum_energy_10_0.state | float) + 
    (states.sensor.hall_energy_11_0.state | float) + 
    (states.sensor.tvattmaskin_energy_9_0.state | float)) | round(0) }}'
    friendly_name: 'Total used'
    unit_of_measurement: 'kWh'

And the calculation

platform: template
sensors:
  total_energy_cost:
    value_template: '{{ ((states.sensor.total_energy_used.state | float * 1.885)) | round(0) }}'
    friendly_name: 'Total cost'
    unit_of_measurement: 'kr'
4 Likes

Works like a charm, thanks!
Did you figure out how to show it per day or month?

unfortunately I never found a way to even start figuring it out =/

Hi there. I’m wanting to do the same thing as you and then feed the result into the new utility_meter, but I’m unsure about your sensors. Mine are listed below that I want to add together.

    - sensor.xiaomi_office_power
    - sensor.xiaomi_tv_power
    - sensor.cupboard_light_power
    - sensor.davids_light_power
    - sensor.lounge_light_power
    - sensor.washing_machine_power
    - sensor.zwave_garage_light_south_power
    - sensor.family_room_shelly_power
    - sensor.stairs_shelly_power

Where did you derive the sensor for your value template? Not quite sure how you got
states.sensor.total_power_usage.state.

Any feedback would be appreciated.

states.sensor.total_power_usage.state is the total of adding your individual sensors using this template:

platform: template
sensors:
  total_energy_used:
    value_template: '{{ ((states.sensor.xiaomi_office_power.state | float) + 
    (states.sensor.xiaomi_tv_power.state | float) + 
    (states.sensor.cupboard_light_power.state | float) + 
    (states.sensor.pc_energy_2_0.state | float) + 
    (states.sensor.server_energy_3_0.state | float) + 
    (states.sensor.soffa_energy_6_0.state | float) + 
    (states.sensor.sovrum_energy_10_0.state | float) + 
    (states.sensor.hall_energy_11_0.state | float) + 
    (states.sensor.tvattmaskin_energy_9_0.state | float)) | round(0) }}'
    friendly_name: 'Total used'
    unit_of_measurement: 'kWh'
  • I have changed the first couple for you so you get the gist… this will then create the sensor you need for your second template sensor. total_energy_used:

use this sensor in the utility monitor…

I have also taken it a step further and calculated the cost with these template sensors (I had to add my off-peak and peak each being muliplied by their respective tarriff:

- platform: template
  sensors:
    offpeak_monthly_electricty_cost:
      value_template: '{{ ((states.sensor.monthly_energy_offpeak.state | float * 0.078)) | round(0) }}'
      friendly_name: 'Off Peak Monthly cost'
      unit_of_measurement: '£'

- platform: template
  sensors:
    peak_monthly_electricty_cost:
      value_template: '{{ ((states.sensor.monthly_energy_peak.state | float * 0.155)) | round(0) }}'
      friendly_name: 'Peak Monthly cost'
      unit_of_measurement: '£'

- platform: template
  sensors:
    total_electricity_used:
      value_template: '{{ ((states.sensor.offpeak_monthly_electricty_cost.state | float) + 
      (states.sensor.peak_monthly_electricty_cost.state | float)) | round(2) }}'
      friendly_name: 'Total used'
      unit_of_measurement: '£'
2 Likes

Thanks for that, very much appreciated. :+1:

It’s the least I could do for all your help with my (now bluetooth enabled) NUC :smiley:

You may want to look at getting a Loop energy monitor. There is an official HA component and it is relativly cheap (£65) and dead easy to setup.

More sofiaticated ones will identify appliance signatures(power per appliance) but this one works pretty well to give tou your total household usage.

Any idea what is wrong here?

  - platform: template
    sensors:
      energy:
    value_template: '{{ ((states.sensor.sonoff_10006f9587_power | float) + (states.sensor.sonoff_10006f98cc_power | float)) | round(0) }}'
        friendly_name: 'Power'
        unit_of_measurement: 'W'

I’m getting value 0 all the time, while both sensors can read value, no errors in log.

Also would it be possible to set
icon: mdi:flash-outline
for my custom calculate sensor samehow?

value_template: '{{ ((states.sensor.sonoff_10006f9587_power.state

you need to add “.state” to the end of your sensor. I have corrected your code below:

  - platform: template
    sensors:
      energy:
    value_template: '{{ ((states.sensor.sonoff_10006f9587_power.state | float) + (states.sensor.sonoff_10006f98cc_power | float)) | round(0) }}'
        friendly_name: 'Power'
        unit_of_measurement: 'W'

I’m in the process of adding your costing templates now which should be great but I need to divide the monthly electricity amount by 1000 as they are reporting W instead of kWh.

I ended up doing this which works:

 | float)) | multiply(0.001) | round(4) }}'
        friendly_name: 'Total Power Use'
        unit_of_measurement: 'kWh'

Whoops, that screws the daily and monthly calcs for utility_meter. Trying again.

EDIT: Sorted. Moved the multiply(0.001) to the costing calculation template

  server_monthly_electricty_cost:
    value_template: '{{ ((states.sensor.server_m.state | multiply(0.001) | float * 0.2023)) | round(2) }}'
    friendly_name: 'Server Monthly Cost'
    unit_of_measurement: '$'

Lol, you beat me to it… i was about to suggest that… glad you got it working… the next step is to now incorporate somehow the rss feed for fuel prices… imagine if home assistant could automatically switch your fuel suppliers based on the cheapest rate…!

Ok, I am doing something wrong here. Hope anyone can help me out a bit.

The error I am getting:

Log Details (ERROR)
Tue Apr 09 2019 00:36:59 GMT+0200 (CEST)

Invalid config for [sensor.template]: required key not provided @ data['sensors']. Got None. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/

my configuration,yaml file:

sensor:
  - platform: template
    total_energy_cost:
    value_template: '{{ ((sensor.power_today | float * 1.885)) | round(0) }}'
    friendly_name: 'Total cost'
    unit_of_measurement: '€'

I don’t need to sum up different value’s. Just have 2 values which I want to show the currency form from, the sensors:

sensor.power_today
Attributes for this sensor:

unit_of_measurement: kWh
friendly_name: Power_today
icon: mdi:power-plug

sensor.gas_today
Attributes for this sensor:

unit_of_measurement: M3
friendly_name: Gas_today
icon: mdi:gas-cylinder

Can anyone please help me into the right direction?

Template extension wasn’t defined.

sensor:
  - platform: template
    total_energy_cost:
    value_template: '{{ (states('sensor.power_today') | float * 1.885) | round(0) }}'
    friendly_name: 'Total cost'
    unit_of_measurement: '€'

Look here for more examples:

1 Like

Thanks! Got it up and running now :slight_smile:

1 Like

Perfect. I’ve been doing the same but haven’t managed to split the 4 hours of peak time yet.
I’ll have a go at this.
Thanks

Hello All,
I need help!..I am on the quest of creating a custom template sensor; where I use the value from one entity and multiply that value by the number 5000.

The goal here is that I added the yahoo finance integration to my HA and want to use a stock’s current price (entity value) AND times that number by the number of shares that I have for it (5000).

  - platform: template
    sensors:
      mystock_holding:
        friendly_name: My Stock Holding's Value
        value_template: "{{ (states('sensor.yahoofinance_ticker_usd') | float * 5000) | round(0) }}"

I have attempted everything on this page, as well as other threads, but cant seem to output the desired calculation. What I get is either the “on” or the “unavailable” values.

Image of UI:
image

Thank you in advance

I can’t see anything wrong with this value_template. Can you show a screenshot of what the developer tools page shows for the sensor sensor.mystock_holding and also sensor.yahoofinance_ticker_usd I wonder if the USD is actually part of the state of that sensor rather than a unit of measurement.

1 Like