metbril / home-assistant-blueprints

Moved to https://codeberg.org/r12t/home-assistant-blueprints
https://codeberg.org/r12t/home-assistant-blueprints
MIT License
1 stars 1 forks source link

Pre-action isn't triggered if sensor is initially above start threshold #2

Open madbrain76 opened 2 years ago

madbrain76 commented 2 years ago

Is your feature request related to a problem? Please describe. The pre-action isn't triggered if sensor is initially above start threshold. I'm trying to use this automation to detect when my cats unplug their water fountain by pulling the DC cord. The normal state is for it to consume 1W continuously. Watts go to 0.1 if the cats unplug the fountain. I set a post-action to notify me when the fountain has been unplugged.


alias: Cat fountain unplugged
description: ""
use_blueprint:
  path: metbril/appliance_has_finished.yaml
  input:
    power_sensor: sensor.cat_fountain_current_consumption
    starting_hysteresis:
      hours: 0
      minutes: 1
      seconds: 0
    starting_threshold: 1
    finishing_threshold: 0.1
    finishing_hysteresis:
      hours: 0
      minutes: 1
      seconds: 0
    actions:
      - service: notify.mobile_app_sm_s908u
        data:
          message: Cat fountain unplugged.
    pre_actions:
      - service: script.no_op_nop_one_second_delay
        data: {}

The problem is that if the fountain is already on (and consuming >=1W) when the automation is installed, the pre-action doesn't appear to get triggered. And because of this, the post-action cannot get triggered either.

The automation appears to require the watts to cross from <1W to >1W in order to trigger the pre-action. But that doesn't work for something like this pump that's expected to be on all the time.

Describe the solution you'd like Ideally, in this case, if the watts are above the "starting threshold", the pre-action should be triggered immediately upon installing the automation.

Describe alternatives you've considered None.

Additional context Another confusing part is debugging this. When sorting by "last triggered" in HomeAssistant, the date/time shown is that of when the starting_threshold is crossed. But what I really want to see is when the finishing_threshold is crossed.

madbrain76 commented 2 years ago

So, I looked into this some more. It looks like this is because the "numeric_state" trigger only works if the value crosses the threshold. It doesn't work if the initial state is already above/below the threshold that is set.

For my cat fountain's pump, I managed to do what I wanted with this simpler automation that doesn't require your blueprint :

alias: Cat fountain unplugged
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.cat_fountain_current_consumption
    for:
      hours: 0
      minutes: 0
      seconds: 1
    below: 1
condition: []
action:
  - service: notify.mobile_app_sm_s908u
    data:
      message: Cat fountain unplugged
mode: single

However, there are other use cases with other appliances that are not so easily resolved.

For example, my dishwasher normally consumes under 1W idle. It'll go anywhere from 2-1500W during the cycle. If one edits the automation in the middle of the cycle, or restarts Home Assistant in the middle of the cycle, because the sensor is already >2W and isn't crossing the threshold, the automation won't detect that the appliance is on, and won't move to the "pre-action" state at all. And thus, the (post)-"action" can never be run either ...

The same is true for pretty much any other appliance, if you end up restarting HA. If you have a lot of power sensors, as I do, the likelihood that one device is actually running while restarting HA approaches 100%.

What is needed is the ability to check if an appliance stays above a threshold X for duration Y, without also requiring it to be below threshold X to start the count. platform: numeric_state trigger in HA doesn't work this way, as it requires crossing the threshold to trigger. Is there perhaps another trigger that will work ? Or is this really a feature request for HA ?

metbril commented 2 years ago

They're probably multiple ways to do this. One of them is to create a template sensor like this one from my configuration.

https://github.com/metbril/home-assistant-config/blob/main/entities/template/binary_sensor/washing_machine_running.yaml

madbrain76 commented 2 years ago

Thanks. I really appreciate your help ! BTW, I was using the sbyx version until yesterday - your version is much better.

I'm not sure I fully understand the syntax for that binary sensor. It may work for simple cases like my dishwasher that strictly go down to a given threshold after the cycle is complete. Is there a way to create a blueprint with both the binary sensor and the actions ? I was hoping to manage it all through the GUI, in one screen per appliance, like this blueprint purports to allow. I was

For the washing machine, it is a more complicated case, I'm afraid, in part due to the optional Tumblefresh features that keeps it spinning once every 5 minutes for a few seconds at the end of the cycle, to keep the clothes from rotting in the remaining water. I wouldn't want this to be viewed incorrectly as an "appliance has started" event. My rice cooker has the same problem. It starts heating again periodically at the end of the cycle to keep the rice warm.

Using this blueprint, for the clothes washer, I have managed to deal with that by having the finishing_hysteresis set to 4 minutes. The automation works fine, unless I restart HA in the middle of the wash cycle, and then it fails to pick up the start of the appliance, because the watt sensor is already above the starting watts threshold.

In other words, your binary sensor appears to have the equivalent of a "finish hysteresis", but not a "start hysteresis", which this blueprint has. Except the start hysteresis in the blueprint uses a platform: numeric_state that requires the threshold to be crossed before the timer starts, and keeping it above the threshold for the duration of start_hysteresis before triggering the pre-action. Your binary sensor example seems to have no equivalent for the start_hysteresis. If I understand correctly, the delay_off is the finish_hysteresis.

metbril commented 5 months ago

Please let me know if you still need help with this.

To monitor a drop of power (e.g. power = 0 W), you could also use my Actionable Alert blueprint.