Switch Off device if power is and stayed under the threshold

Hi there,

trying to move from Openhab to HomeAssistant and currently in the process of “copying” the rules.

I got one, where I switch off my TV, if the power value is below 40 watts for 3 minutes.
I tried to built this in an automation, but it seems that the power needs to drop from >40 to <40 to trigger this automation.

As I am stupid sometimes, I switch on the TV and don’t use it, so the power value stays below 40 watts all the time.

alias: Standby Fernseher Büro
description: ''
trigger:
  - platform: state
    entity_id:
      - sensor.r1_fernseher_power
    to: '40'
    for:
      hours: 0
      minutes: 3
      seconds: 0
  - type: power
    platform: device
    device_id: 3ec2df7f130b78ceb86f7591d9f5efd3
    entity_id: sensor.r1_fernseher_power
    domain: sensor
    below: 40
    for:
      hours: 0
      minutes: 3
      seconds: 0
  - platform: numeric_state
    entity_id: sensor.r1_fernseher_power
    for:
      hours: 0
      minutes: 3
      seconds: 0
    below: <40
condition: []
action:
  - type: turn_off
    device_id: 3ec2df7f130b78ceb86f7591d9f5efd3
    entity_id: switch.r1_fernseher
    domain: switch
mode: single

If anyone of you has a working automation like that, I would really like to understand it

Might be better to use a numerical state like this:

platform: numeric_state
entity_id: sensor.r1_fernseher_power
for:
  hours: 0
  minutes: 3
  seconds: 0
below: '40'

PS: Welcome to the community!

Hi there again and thank you for the warm welcome :slight_smile:

I looked into my Openhab rule and found a solution that works for me. Not the most elegant way but if someone wants to use it feel free.

alias: Standby Fernseher Büro
description: ''
trigger:
  - platform: time_pattern
    minutes: /1
condition: []
action:
  - if:
      - type: is_power
        condition: device
        device_id: 3ec2df7f130b78ceb86f7591d9f5efd3
        entity_id: sensor.r1_fernseher_power
        domain: sensor
        below: 40
    then:
      - delay:
          hours: 0
          minutes: 3
          seconds: 0
          milliseconds: 0
      - if:
          - type: is_power
            condition: device
            device_id: 3ec2df7f130b78ceb86f7591d9f5efd3
            entity_id: sensor.r1_fernseher_power
            domain: sensor
            below: 40
        then:
          - type: turn_off
            device_id: 3ec2df7f130b78ceb86f7591d9f5efd3
            entity_id: switch.r1_fernseher
            domain: switch
mode: single

unfortunately it is not monitoring over the whole 3 minutes, but my openhab rule was basically the same. If there is a better solution, please let me know.

The numerical state trigger is a better solution!

You have set an automation to run every minute, 24 hours a day which although not a massive issue in itself is a waste of resources. Also if the IF statement if true then you have a 3 min delay and in those 3 mins the automation will have tried to trigger again up to 3 times and as you have single mode set this will cause warnings / errors in the home assistant logs.

If you want to carry out an action if the state of something is a below a set numerical value for a set time then you want your trigger to be exactly that.

I just had a look into the documentation. And it states that it only triggers if it crosses the value. Which would mean it would only trigger, if the value goes from >40 to <40.

Fires when the numeric value of an entity’s state (or attribute’s value if using the attribute property, or the calculated value if using the value_template property) crosses (and only when crossing) a given threshold. On state change of a specified entity, attempts to parse the state as a number and fires if the value is changing from above to below or from below to above the given threshold.

As the value is not crossing the threshold but will stay under 40 the whole time, it would mean it would never trigger.

Sorry, me being stupid, I must have glossed over this part.

Question, when you say I sometimes switch it on, I assume you mean via IR remote or similar, as apposed to switching the smart plug on, so when its switched off (or I assume in standby) what is the power consumption?

My setup is basically a smart plug with energy meter. If I switch it on the TV gets into standby and I then have to take the remote to switch it on completely. (Going to search for a solution to do it in one step later). If I don’t turn it on copmpletely it will never cross the threshold and the plug will stay powered on. Standby wattage is around 9-14 watts, which espacially with the high electricity prices and the fact I got 3 TVs in the house, is a fortune now :stuck_out_tongue:

OK, understood, so your concern is that you will switch the plug on but then not use the TV, wait 2 mins I have a possible idea…

What about something like this:
(Replacing for your entities of course!)

trigger:
  - platform: state
    entity_id:
      - switch.tv_plug
    from: 'off'
    to: 'on'
    id: tv plug goes on
    for:
      hours: 0
      minutes: 3
      seconds: 0
  - platform: numeric_state
    entity_id: sensor.tv_plug_power
    for:
      hours: 0
      minutes: 3
      seconds: 0
    below: '40'
    id: consumption below 40w
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: consumption below 40w
        sequence:
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.tv_plug
      - conditions:
          - condition: trigger
            id: tv plug goes on
        sequence:
          - if:
              - condition: numeric_state
                entity_id: sensor.tv_plug_power
                below: '40'
            then:
              - service: switch.turn_off
                data: {}
                target:
                  entity_id: switch.tv_plug

This will only trigger when either the wattage drops below 40 for 3 mins and then switch the plug off
OR
will trigger when the plug has been on for 3 mins and will then check its still in standby and if it is, will switch the plug off.

3 Likes

Thanks a lot, I will test this and report back!

1 Like

There’s a more concise solution now. I tried numeric_state trigger first and it failed. But there’s also this way (which works well) - here turning off a forgotten dehumidifier when it’s running for 70 min - its power is known:

type: power
platform: device
device_id: f67153f49fdd85021797788eb2882391
entity_id: sensor.gosund1_energy_power
domain: sensor
above: 70
below: 100
for:
  hours: 1
  minutes: 10
  seconds: 0