Automations Headache

Hi All,

I want to run what seems to be a super simple automation, but can’t seem to make it work. I think it is the trigger that I am not understanding. I have tried plenty triggers, the one in the code was just my last ditch attempt.

I would like to start my poop pump when certain conditions are met. In my mind it is easiest to apply an IFTTT in the “Action”

It seems like the trigger on the other hand only run once, if the trigger happened and the conditions are not met then the automation doesn’t run. I need the trigger to be “Open” (for lack of a better word)

I need my pool pump to switch on when there is enough power available from my PV system, when cloud cover is less than 60%, Battery SOC above 80% and after sun rise (I know this will already be covered in the Power condition). Below is my code, how can I make this work?

alias: Pool Pump_New
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.pv_power
    below: sensor.power_production_now
condition: []
action:
  - if:
      - condition: numeric_state
        entity_id: sensor.pv_power
        below: sensor.power_production_now
      - type: is_value
        condition: device
        device_id: 2cfeb81c51d656035fa9852a40ab1fdb
        entity_id: 40b35a2dfde36c7ebe0df2035898e25a
        domain: sensor
        below: 60
      - condition: numeric_state
        entity_id: sensor.battery_state_of_charge
        above: 80
      - condition: sun
        after: sunrise
    then:
      - type: turn_on
        device_id: b1c12eef9b4b90a58bce6e586111d55e
        entity_id: e929971b7c568a8fb55c18a757edba37
        domain: switch
mode: single

Two ways you can do this:

Change your trigger to:

trigger:
  - platform: state
    entity_id:
      - sensor.pv_power
    not_to:
      - unknown
      - unavailable

So it triggers on all state changes of your sensor. You have enough conditions to only allow the actions to happen as per your stated requirements.

The other option is to leave the pv_power trigger as is and add all the other sensors in your conditions as triggers as well.

Make those triggers and conditions.


Example

alias: Pool Pump_New
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.pv_power
    below: sensor.power_production_now
  - platform: numeric_state
    entity_id: sensor.your_cloud_cover
    below: 60
  - platform: numeric_state
    entity_id: sensor.battery_state_of_charge
    above: 80
  - platform: sun
    event: sunrise
condition:
  - condition: numeric_state
    entity_id: sensor.pv_power
    below: sensor.power_production_now
  - condition: numeric_state
    entity_id: sensor.your_cloud_cover
    below: 60
  - condition: numeric_state
    entity_id: sensor.battery_state_of_charge
    above: 80
  - condition: sun
    after: sunrise
action:
  - type: turn_on
    device_id: b1c12eef9b4b90a58bce6e586111d55e
    entity_id: e929971b7c568a8fb55c18a757edba37
    domain: switch
mode: single

This is a common technique to handle the situation where multiple things must be true (“enough power available from my PV system, when cloud cover is less than 60%, Battery SOC above 80% and after sun rise”) before an action can occur.

Thank you, I will try this. I am really keen to make this work, this will be my first major automation other than the small “When gate opens, switch on garage light”

Fantastic, thank you.

How would I go about switching the pump off again later in the day when PV power is not enough anymore? But also keep it off. Example:

Let’s say it is after 4pm, check if PV Power is enough, if Yes, then stay on, Else Off, but stay off until the next morning where “Pool Pump_New” will run again

Create a separate automation for the exclusive purpose of turning off the pump.

Absolutely, but I meant to ask was. Let’s say the Off Automation triggered, how do I prevent the On to trigger again to prevent the pool from switching on and off every 5 minutes as the PV power might fluctuate.

This requirement is absent from your first post and serves to modify potential solutions.

What other requirements do you have for controlling the pump’s operation?

Apologies, I am really learning as I am going here. But really keen to get stuck into this.

So the requirements are as follows for a full automation experience:

  1. Turn on the pump when those conditions are met
  2. Stop running the trigger in a loop once it it turned on. (I see in the logs that it keeps on triggering even if the pump is on already after the modifications suggested earlier by @tom_l)
  3. Switch off the pump when PV production is low and then keep the pump off by not running point 1 again. The reason is that PV power might fluctuate on cloudy days or when equipment in the house gets switched on and off. To prevent the pump switching on and off, the first point should run again after point 3 triggered within the same day…tomorrow point 1 can test and run again.

I hope I making sense, this is the way I envisage it
3.

Add a State Condition that confirms the pump is off. This prevents the automation from turning on the pump when it’s already on.

condition:
  - condition: numeric_state
    entity_id: sensor.pv_power
    below: sensor.power_production_now
  - condition: numeric_state
    entity_id: sensor.your_cloud_cover
    below: 60
  - condition: numeric_state
    entity_id: sensor.battery_state_of_charge
    above: 80
  - condition: sun
    after: sunrise
  - condition: state
    entity_id: switch.your_pump
    state: 'off' 

You want to prevent the pump from being turned on again until when? Until after the start of the next day (00:00:00), in other words, until tomorrow?

If the answer is yes then, effectively, you want to restrict the automation to turn on the pump no more than once a day. Is that correct?

That is correct yes

The following is an enhanced version of my previous example. It contains two additional conditions.

  1. A State Condition to prevent the automation from turning on the pump if it’s already on.

  2. A Template Condition to prevent the automation from turning on the pump more than once a day.

alias: Pool Pump_New
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.pv_power
    below: sensor.power_production_now
  - platform: numeric_state
    entity_id: sensor.your_cloud_cover
    below: 60
  - platform: numeric_state
    entity_id: sensor.battery_state_of_charge
    above: 80
  - platform: sun
    event: sunrise
condition:
  - condition: numeric_state
    entity_id: sensor.pv_power
    below: sensor.power_production_now
  - condition: numeric_state
    entity_id: sensor.your_cloud_cover
    below: 60
  - condition: numeric_state
    entity_id: sensor.battery_state_of_charge
    above: 80
  - condition: sun
    after: sunrise
  - condition: state
    entity_id: switch.your_pump
    state: 'off'
  - condition: template
    value_template: >
      {{ state_attr(this.entity_id, 'last_triggered') | default(today_at(), true) < today_at() }}
action:
  - type: turn_on
    device_id: b1c12eef9b4b90a58bce6e586111d55e
    entity_id: e929971b7c568a8fb55c18a757edba37
    domain: switch
mode: single

Holy Crap! I need to study templates. That is JS in there right?

I will try this, thank you so much!

It Jinja2.

Reference

Templating


Explanation of the template’s operation:

  • The value of this.entity_id is the automation’s entity_id, whatever it might be. For this particular example, it will be automation.pool_pump_new.

  • today_at reports a datetime object representing today’s date and time at 00:00:00.

  • Automations have a last_triggered attribute representing the last time the automation started executing its actions.

  • The state_attr function gets the value of the last time the automation executed its actions.

  • The template checks if the last time the automation executed its actions is before today at 00:00:00.

My goodness, ok another language I need to learn.

Awesome, thanks. Thanks again for the in depth explanation, cannot wait for tomorrow to test this

Morning, I am getting this error on the template. Am I doing something wrong?

What is this.automation.pool_pump_new
image

The automation needs to exist.

Yes the automation exists. “this.automation.pool_pump_new” comes from the explanation @123 gave. I wasn’t sure if I should use “this.automation.pool_pump_new” or just “automation.pool_pump_new”. So I tested both, hence 2 screenshot errors.

You should use 'automation.pool_pump_new' and include the single quotes.

like this

{{ state_attr('automation.pool_pump_new', 'last_triggered') | default(today_at(), true) < today_at() }}

1 Like

Damn, I need to learn the language! Thanks it works. Now I just need to get the automation to work. The template is returning “False”, which is actually expected, it will only return “True” if the automation actually executed the actions, but it can’t execute due to the “False” value.