Calculate consumption between events

Hello,

First post, and every bit the newbie.

I just configured an automation, based on code from some other post, to detect the washer machine start and ending, using the TPLink plugin to connect to a HS110 socket switch. The triggers are set up so that when power is more than 3 W for 1 minute, the machine is considered ON. When the power is less than 3W for more than 1 minute (and it was ON) the cycle has finished, so the boolean status flag is set to OFF and it sends a notification email. This all work successfully.

But now I would like to add some info to the notification email, in particular, the energy consumption for the finished cycle. In principle, it should be a simple calculation such as:

washer_dryer_total_kwh (at the time the washer finished) - washer_dryer_total_kwh (at the time the washer started).

What would be the easiest way to set up something like this…?

Thanks!

This is my relevant bits of the “configuration.yaml” file:

automation: !include automations.yaml

tplink:
  discovery: false
  switch:
    - host: my.local.net.address
    
input_boolean: 
  washer_dryer_switch:
    name: Power status
    initial: off
    icon: mdi:tumble-dryer
  
sensor:  
  - platform: template
    sensors:
      washer_dryer_amps:
        friendly_name_template: "{{ states.switch.washer_dryer.name}} Current"
        value_template: '{{ states.switch.washer_dryer.attributes["current_a"] | float }}'
        unit_of_measurement: 'A'
      washer_dryer_watts:
        friendly_name_template: "{{ states.switch.washer_dryer.name}} Current Consumption"
        value_template: '{{ states.switch.washer_dryer.attributes["current_power_w"] | float }}'
        unit_of_measurement: 'W'
      washer_dryer_total_kwh:
        friendly_name_template: "{{ states.switch.washer_dryer.name}} Total Consumption"
        value_template: '{{ states.switch.washer_dryer.attributes["total_energy_kwh"] | float }}'
        unit_of_measurement: 'kWh'
      washer_dryer_volts:
        friendly_name_template: "{{ states.switch.washer_dryer.name}} Voltage"
        value_template: '{{ states.switch.washer_dryer.attributes["voltage"] | float }}'
        unit_of_measurement: 'V'
      washer_dryer_today_kwh:
        friendly_name_template: "{{ states.switch.washer_dryer.name}} Today's Consumption"
        value_template: '{{ states.switch.washer_dryer.attributes["today_energy_kwh"] | float }}'
        unit_of_measurement: 'kWh'

and this the relevant part of the “automations.yaml” file:

- id: washer_dryer_on
  alias: Washer&Dryer On
  trigger:
  - above: '3'
    entity_id: sensor.washer_dryer_watts
    for:
      minutes: 1
    platform: numeric_state
  action:
  - entity_id: input_boolean.washer_dryer_switch
    service: input_boolean.turn_on
    
- id: washer_dryer_off
  alias: Washer&Dryer Off
  trigger:
  - below: '3'
    entity_id: sensor.washer_dryer_watts
    for: 
      minutes: 1
    platform: numeric_state
  condition:
  - condition: state
    entity_id: input_boolean.washer_dryer_switch
    state: 'on'
  action:
  - entity_id: input_boolean.washer_dryer_switch
    service: input_boolean.turn_off
    
- id: washer_dryer_done
  alias: Washer&Dryer Done
  trigger:
  - entity_id: input_boolean.washer_dryer_switch
    platform: state
    to: 'off'
  condition: []
  action:
  - data:
      message: "Please remove the cloths"
      title: "Washer&dryer has finised"
    service: notify.notifier_email1
      
    
1 Like

You could create an input number and store the beginning kWh in that.

input_number:
  start_energy:
    mode: box
    name: Start Energy
    min: 0
    max: 1000000000
    step: 0.1 

Add the service to store the initial value to your on automation. You’re going to miss 1 minute of energy used, if this is important to you move the set value service to an automation triggered by the switch going on instead:

- id: washer_dryer_on
  alias: Washer&Dryer On
  trigger:
  - above: '3'
    entity_id: sensor.washer_dryer_watts
    for:
      minutes: 1
    platform: numeric_state
  action:
  - entity_id: input_boolean.washer_dryer_switch
    service: input_boolean.turn_on
  - service: input_number.set_value
    data_template:
      entity_id: input_number.start_energy
      value: "{{ states('sensor.washer_dryer_total_kwh') }}"

Add the calculation to the done automation:

- id: washer_dryer_done
  alias: Washer&Dryer Done
  trigger:
  - entity_id: input_boolean.washer_dryer_switch
    platform: state
    to: 'off'
  condition: []
  action:
  - data_template:
      message: "Please remove the cloths Total energy used was {{ states('sensor.washer_dryer_total_kwh')|float - states('input_number.start_energy')|float }}kWh"
      title: "Washer&dryer has finised"

There are other ways to store values. An MQTT topic, or use the variable custom component.

1 Like

hi @tom_l,

thanks for the quick reply with code and the hints - I will implement your proposed solution, test it and report back.

One basic question, though:

why, when doing the calculation for the report, you typecast the energy value to float:

action:
  - data_template:
      message: "Please remove the cloths Total energy used was {{ states('sensor.washer_dryer_total_kwh')|float - states('input_number.start_energy')|float }}kWh"
      title: "Washer&dryer has finised"

but not on the trigger action?

- service: input_number.set_value
    data_template:
      entity_id: input_number.start_energy
      value: "{{ states('sensor.washer_dryer_total_kwh') }}"

The set value service can deal with numbers as strings. The mathematical operation in the message template can not.

Understood.

Thank you again for your assistance - your example script using a input numbers worked perfectly.

Now I’ll try to get it to work without the 1 minute wait delay.

1 Like