Struggling on a new automation script to turn off desk when below wattage threshold

Hello everyone,

This is my first topic/message on this community so I would first like to start off how amazing this board is and how much help I’ve already gotten from here simply by copying/pasting some of the scripts.

I was hoping it would have never come to this, but alas, I have to ask for help on my automation desire. Because the similar scripts I did found, I can not ‘save’ because it results in an error that says it’s not allowed to do.

So what would I like to do?
When it’s 23:00, I would like for Home Assistant to turn off my desk (dutch: bureau).
However, it must only do this when it’s current Wattage is below 10W for about 3 to 5 minutes. When everything connected is off/standby, its idling around 4 to 9W.
If it sees that the usage is above 10W, it must leave it alone and check again on 00:00 and at 01:00. (I usually go to bed before 23:00, but sometimes work gets in the way)
However, dips can of course occur so that timer needs to be in there… and I am unable to implement a ‘timer’ because if I implement a “for:” at condition it does not allow me to do so.

The script:

alias: Tim's bureau uitzetten bij geen gebruik.
description: ""
trigger:
  - platform: time
    at: "00:23:00"
  - platform: time
    at: "00:00:00"
  - platform: time
    at: "00:01:00"
condition:
  - condition: numeric_state
    entity_id: sensor.shellyplusplugs_---shellyID---_switch_0_power
    below: 10
    attribute: W
action:
  - type: turn_off
    device_id: ---shelly plug ID---
    entity_id: ---shelly plug ID, switch---
    domain: switch
mode: single

Maybe it’s a simple thing that I am overlooking. But hopefully someone can help me out or even make my script better.

The times you’ve used are not the ones in your text. You’re triggering at midnight, one minute past midnight and 23 minutes past midnight. You want “23:00”, “00:00” and “01:00”.

Also, remove the attribute line from your condition unless the power is genuinely the “W” attribute of the Shelly sensor, rather than the state.

Assuming the power is in the state, set up a template binary sensor (UI, Helpers) called “desk power low” with state template:

{{ states('sensor.SHELLY_SENSOR_ID')|float(0) < 10 }}

Obviously, put your real entity ID in there. This new entity will allow you to use for: in the condition, which you can’t do with a numeric_state condition.

Then:

trigger:
  - platform: time
    at: "23:00"
  - platform: state
    entity_id: binary_sensor.desk_power_low
    to: 'on'
    for:
      minutes: 5
condition:
  - condition: time
    after: "22:59"
    before: "05:00"
  - condition: state
    entity_id: binary_sensor.desk_power_low
    state: 'on'
    for:
      minutes: 5
action:
  - service: switch.turn_off
    target:
      entity_id: switch.SHELLY_SWITCH_ID

And again with the switch ID substitution.

That will run the action as soon as the desk power has been low for 5 minutes after 23:00 and before 05:00.

1 Like

Absolutely amazing explanation and guidance @Troon, thank you very much!

I’m going to test it out tonight, but it accepted everything and the gui provides a perfect overview.

alias: Tim's bureau uitzetten bij geen gebruik.
description: "Zet bureau uit na 5 minuten laag verbruik."
trigger:
  - platform: time
    at: "23:00"
  - platform: state
    entity_id: binary_sensor.bureau_wattage_laag
    to: "on"
    for:
      minutes: 5
condition:
  - condition: time
    after: "22:59"
    before: "05:00"
  - condition: state
    entity_id: binary_sensor.bureau_wattage_laag
    state: "on"
    for:
      minutes: 5
action:
  - type: turn_off
    device_id: -snip-
    entity_id: -snip-
    domain: switch
mode: single

So basically, to change the Wattage, I only have to adjust the number “10” to something like “15” in the helper/template if the idle/standby wattage might be higher (or spike higher) to still make it turn off, correct?

Correct.

I’d still recommend changing the action to a service call rather than a device action.

1 Like