Sensor value below thresold for amount of time between 2 hours

Hi !
I would want to shutdown completely my HiFi/Home cinema system by night. I bought a zigbee plug for that (Nous A1Z).
My needs :

  • supervision only between 00:30AM and 06:00AM (during the daytime I allow standby which consumes power)
  • power sensor value below 50W during 5 minutes (to be sure the shutdown is “real”)

I would want 0 where we see almost 40 :
image

For that I tried this configuration :

alias: Shutdown Home Cinema
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.plug_num3_power
    for:
      hours: 0
      minutes: 5
      seconds: 0
    above: -0.1
    below: 50
condition:
  - condition: and
    conditions:
      - condition: time
        after: "00:30:00"
        before: "06:00:00"
      - condition: state
        entity_id: switch.plug_num3
        state: "on"
action:
  - service: switch.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: switch.plug_num3
mode: single

But the triggers occurs only if the power consumption crosses the thresold between the 2 hours. If I switch off my TV before 00:30AM it doesn’t work.

Could you help me please ?!

Regards

A few thoughts…

  • Not sure why you need the “above” line in the trigger. As I understand it, the actual trigger is dropping below 50 for 5 minutes.
  • Multiple conditions are “and” by default, so you don’t need that line.
  • You don’t really need to test to see whether the plug is on - if it’s off already the switch.turn_off command won’t make any difference.

To make it more concise you might consider using a threshold helper in the trigger and a time of day helper in the condition.

The automation as it is now will trigger when power falls below 50W for five minutes. If the time is before 00:30 it will stop. It will not trigger again until power goes above 50W and falls below 50W for a second time.

1 Like

My main problem is the Note here : Automation Trigger - Home Assistant

Crossing the threshold means that the trigger only fires if the state wasn’t previously within the threshold.

If the consumption is below the thresold before the period in which I want to detect it, nothing triggers.
I would need a sensor like a numeric value with a duration attribute.
Perhaps should I try to cron every 5 minutes a check of the value, keep the previous one in something like a variable, compare with the current, and trigger if both are below 50W.
I’m surprised this doesn’t already exist in HA !? Because this can be apply to multiple circumstances.

How about a second automation triggered at 00:30? If power is below 50W, turn off.

2 Likes

Definitely not. Do as Stiltjack suggests: two triggers, matching conditions, so that both have to be true before proceeding. Here you go, with lots of unnecessary cruft removed:

alias: Shutdown Home Cinema
trigger:
  - platform: numeric_state
    entity_id: sensor.plug_num3_power
    for: "00:05:00"
    below: 50
  - platform: time
    at: "00:30:01"
condition:
  - condition: time
    after: "00:30:00"
    before: "06:00:00"
  - condition: numeric_state
    entity_id: sensor.plug_num3_power
    below: 50
  - condition: state
    entity_id: switch.plug_num3
    state: "on"
action:
  - service: switch.turn_off
    entity_id: switch.plug_num3
mode: single
1 Like

Thank you Stiltjack and Troon. This seems to work fine.
Little tricky but working as desired :slight_smile: