How to switch off a device when its power consumption is below X for a certain time?

Hi there,

I’m using HA since around 1 1/2 years, nevertheless I’m quite new to stuff like automation.
I’ve some of them running now and they’re doing their job well, however, this time I’m struggling.

What I want to achieve is the following:

A device connected to a Shelly Plug S shall be switched off automatically once the measured power consumption is below a certain value (e.g. 14W) for a certain amount of time (e.g. 30 minutes).
On the contrary, it shall not switch off when its power consumption is above that value.

Since the “For” option in an automation would not survive a restart of HA, which is likely to happen in my case, I need some alternative and I think it can be managed by using a template.
But I’ve no clue yet how to do it, even after reading about this stuff.

Any hints are appreciated.

Thanks!

There are some other posts with temp store=ing values and they seem to point to : store this in input_number but I am not sure what precisely you think to loose from a restart unless you want to be very exact on the 30 mins and how often you restart (why?)

Hi,

thanks for your reply.

I thought that such a “For” timer would not survive any restart of HA.

But if that’s not the case and it would be only skipped in case HA gets restarted in the moment of where such a timer would countdown, then it’s OK for me.

Usually my HA only reboots during late night three times a week because of automated backup job.

You are right. It is the case.

delay/for/wait/etc all get cancelled any time HA is restarted or automations reloaded.

I usually create an input_datetime and set that to the desired off time in the automation for any timers (esp critical ones) over 5 minutes.

then I use that input_datetime to do the desired action at that time. that method will survive restarts/reloads.

I’ve posted about it elsewhere but I can’t find it at the moment.

Just to be clear, I do understand that automations are cancelled/restarted but… in this case my assumption (!) is that it would still be able to identify how long the state was set. So, with a restart, it would find the state ‘on’(example) and still be able to deduct it was on before the restart…as the last_update/changed does not alter with a restart

that is actually not correct. those do get reset after a restart.

You can check it yourself if you restart HA and check any device last_x and will show to be the restart time of HA.

there is literally no “timer” functions in HA that are reliable and are maintained thru a restart/reload.

Just setup my dev-env + restart and I indeed stand corrected (so: THANKS) and regardless of my use/expectations … my only ‘excuse’ is that I never had a need for this situation…not sure how to deal with this now as I also restart HA a couple if times a week when new stuff arises…will contemplate anew

Hi there,

thanks for your replies and info. So my understanding was basically not wrong, even though it would make things a bit easier if we could go that way.

Anyway, I’ll try with input_datetime (which I already tried but not yet successful).
Would be happy if you could probably find your old post @finity

Thanks

I can give you an example…

this will turn off my fish tank wave pumps 5 minutes after I turn them on:

input_number:
  tank_heads_run_time:
    name: "Tank Heads Run Time (Minutes)"
    initial: 5
    min: 0
    max: 59
    step: 1
    mode: box


input_datetime:
  tank_heads_off_time:
    name: Tank Power Heads Off Time
    has_date: true
    has_time: true

automation:
  - alias: Tank Power Head Cycle Off
    trigger:
      - platform: state
        entity_id: 
          - switch.tank_head_east_6
        to: 'on'
      - platform: time
        at: input_datetime.tank_heads_off_time
      - platform: homeassistant
        event: start
      - platform: event
        event_type: automation_reloaded
    condition:
      - condition: state
        entity_id: input_boolean.tank_pause_automations
        state: 'off'
    action:
      - choose:
          - conditions: "{{ trigger.platform == 'state' }}"
            sequence:
              - service: input_datetime.set_datetime
                entity_id: input_datetime.tank_heads_off_time
                data:
                  datetime: >
                    {% set run_minutes = states('input_number.tank_heads_run_time') | int %}
                    {{ (now() + timedelta(minutes = run_minutes)).timestamp() | timestamp_local  }}
          - conditions: 
              - "{{ trigger.platform == 'homeassistant' or trigger.platform == 'event'}}"
              - "{{ as_timestamp(now()) - as_timestamp(states('input_datetime.tank_heads_off_time')) < 120 }}"
              - "{{ is_state('switch.tank_head_east_6', 'on') }}"
            sequence:
              - service: switch.turn_off
                entity_id: switch.tank_head_east_6
          - conditions:
              - "{{ trigger.platform == 'time' }}"
              - "{{ is_state('switch.tank_head_east_6', 'on') }}"
            sequence:
              - service: switch.turn_off
                entity_id: switch.tank_head_east_6