Turn off plug if electric power consumption exceed a value

Hi,

I have a plug (Gosund SP111) with entity sensor.smartplug_2_energy_today which cumulates the electric power consumption.
I like to charge my Scooter for example with 400Wh and then turn off the plug.

My idea:
Slider in Lovelace to select value from 1 - 400 Wh.
Automation: If plug turn on -> charge my Scooter with SliderValue and turn off plug

I don’t know how to calculate the Wh from start charging (turn on) with the available entities.

Idea:
startvalue = sensor.smartplug_2_energy_today (value when start charging)
turn off plug if startvalue + SliderValue > sensor.smartplug_2_energy_today

But I can’t create this with my current know-how about Hass Syntax.
And maybe there is a better way?
Can the history chart be helpful?

Can somebody give me a good startpoint please?

Hi there… Please try the following.

You first need to create two input_number entities by adding these to your configuration.

input_number:
  charger_threshold:
    name: Charger Tthreshold
    min: 1
    max: 400
    step: 1
  starting_energy:
    name: Charging Start Energy
    min: 0
    max: 4000
    step: 1  

The first input number is to select the threshold of charging i,e between 1 to 400 Wh and the second is to store the value of the energy sensor at the starting of the charging process. If you want you change the max and minimum of the entities.

After this you need to add the following automation.

alias: Smart Charging
description: ''
mode: single
trigger:
  - platform: state
    entity_id: switch.charger
    to: 'on'
    from: 'off'
condition: []
action:
  - service: input_number.set_value
    target:
      entity_id: input_number.starting_energy
    data:
      value: '{{states(''sensor.smartplug_2_energy_today'') |float}}'
  - wait_for_trigger:
      - platform: template
        value_template: >-
          {{states('input_number.charger_threshold') | float +
          states('input_number.starting_energy') | float  >
          states('sensor.smartplug_2_energy_today') | float}} 
    continue_on_timeout: false
  - service: switch.turn_off
    target:
      entity_id: switch.charger

Please change the switch.charger to the entity_id of your scooter charger.

If you have any doubts in adding this automation, please check out the gif.

Do let us know if you want any more help.

1 Like

Perfect. This is really great.
Thank you for spending your time.

2 Likes

I tried to adapt this to instead turn off a TV when a certain amount of power had been used, I was planning to use as a sort of kids monitor. Only issue is that every midnight it turns off if you are watching it. I’ve also not been able to get it to turn off correctly when power is used. I assume something to do with the units. Total daily energy is in Kw, but UI values are in Wh. Any ideas appreciated.

alias: Smart Kids TV Limit
description: ''
trigger:
  - platform: state
    entity_id: switch.shelly_1pm_test_01_relay
    to: 'on'
    from: 'off'
condition: []
action:
  - service: input_number.set_value
    target:
      entity_id: input_number.tv_starting_energy
    data:
      value: '{{ (states(''sensor.shelly_1pm_test_01_total_daily_energy'') | float) * 1000 }}'
  - wait_for_trigger:
      - platform: template
        value_template: >-
          {{ (states('input_number.tv_threshold') | float)*1 +
          (states('input_number.tv_starting_energy') | float)*1  >
          (states('sensor.shelly_1pm_test_01_total_daily_energy') | float)*1000 }} 
    continue_on_timeout: false
  - service: switch.turn_off
    target:
      entity_id: switch.shelly_1pm_test_01_relay
    data: {}
mode: single

Okay, I made a few changes to remove the issues with units. Now everything is specified in kWh. In config:

input_number:
  tv_threshold:
    name: TV Threshold
    min: 0.1
    max: 5.0
    step: 0.1
  tv_starting_energy:
    name: TV Start Energy
    min: 0.0
    max: 5.0
    step: 0.1

in automation:

alias: Smart Kids TV Limit
description: ''
trigger:
  - platform: state
    entity_id: switch.shelly_1pm_test_01_relay
    to: 'on'
    from: 'off'
condition: []
action:
  - service: input_number.set_value
    target:
      entity_id: input_number.tv_starting_energy
    data:
      value: '{{ (states(''sensor.shelly_1pm_test_01_total_daily_energy'') | float) }}'
  - wait_for_trigger:
      - platform: template
        value_template: >-
          {{(states('input_number.tv_threshold') | float) +
          (states('input_number.tv_starting_energy') | float)  >
          (states('sensor.shelly_1pm_test_01_total_daily_energy') | float) }} 
    continue_on_timeout: false
  - service: switch.turn_off
    target:
      entity_id: switch.shelly_1pm_test_01_relay
    data: {}
mode: single

I guess the issue is that I need to reset the tv_starting_energy just before midnight, but if I do that it might still push the sum of tv_threshold + tv_starting_energy over. So really I need to set it at the exact same time as total_daily_energy is reset at midnight.

Seems clunky. Am I missing something?