Relative newbie would like someone to check my automation

Hi

With help previously much appreciated from this forum i build an automation to trigger a switch only once the level of stored electricity in my battery is above a certain % and only once a Day. It is working great :slight_smile:
I’d like to add a new rule that ensures the automation wont run before 10am, I’ve done the below. I attempted adding a condition which i think was similar yesterday but it stopped the whole thing triggering so I deleted it. Hence my question. Thank you very much in advance.

alias: Switch on water heater when solar battery above %
description: ""
trigger:
  - type: battery_level
    platform: device
    device_id: 4a1f34a3802562d798a870b015177638
    entity_id: 174763093e22bdbf4622f040f58e1929
    domain: sensor
    above: 97
    below: 100
condition:
  - condition: template
    value_template: "{{ this.attributes.last_triggered.date() != now().date() }}"
  - condition: and
    conditions:
      - condition: time
        after: "10:00:00"
action:
  - type: turn_on
    device_id: bb96378c3812f80f6f66c736d8a0f718
    entity_id: dfd5869d4db3dca16673c2895f786f4b
    domain: switch
  - delay:
      hours: 1
      minutes: 50
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: bb96378c3812f80f6f66c736d8a0f718
    entity_id: dfd5869d4db3dca16673c2895f786f4b
    domain: switch
mode: single

I’m not sure I understand the issue here.
Does it not work?

Just to be clear, the automation runs once per day except if you restart HA. If you restart then it can run more than once.
Not once per year as you said.

The and part is not needed, conditions are “and” by default and it’s incorrectly written anyways.
But it shouldn’t make a difference even though it’s wrong, it should still work as it is.

And lastly, having delays of 1.5 hours is not good.
It’s better to add a new trigger that triggers when the device has been on for 1.5 hours and turn it off.
Long delays might get killed by a restart leaving the device on forever.

Oh and devices in automations are not great.
It’s better to use the entities since that is much easier to change if something breaks.

1 Like

Thank you all round. I meant daily. Much appreciated.

To further expand on the excellent advice above, you should think through the logic in more detail to ensure it will do what you want, when you want.

You said you don’t want it to run before 10:00. But if the battery level goes above 97 at 9:00 (for example), what do you want to happen once 10:00 occurs? If you want the automation to then run at 10:00, you need to add that to your automation. Meaning, you need to add a time trigger for 10:00.

What if your HA server is shut down or restarting while the battery level goes above 97? HA will have missed the battery level ‘crossing the threshold’ and no trigger will occur. Do you want to check the battery level upon startup of HA? If so, you’ll need to add a trigger for HA startup.

Now, let’s assume you’ve gone ahead and added those triggers. But we have a problem: we don’t want the automation to execute on every trigger. We want it to execute when the conditions are correct. Which are: after 10:00, battery level above 97, and hasn’t ran since midnight. So all three of those conditions need to be in the conditions section.

And that template condition could be improved to:

"{{ this.attributes.last_triggered < today_at() }}"

Thank you. Helpful suggestions. I’ll now research how to create a trigger on start up and also how to embed a delay into my action such that it doesn’t take action until after say 10am

Home assistant start event is what you are looking for.

Nono… you are going about it the wrong way here.
Don’t delay an automation more than necessary. A few seconds are fine, minutes could be an issue. Hours is a big nono.

Add a time trigger at 10:00 instead.
So that gives you three triggers, start event, battery, and 10:00.

If you could change your automation to use entities instead of devices then we would happily help you redesign the automation.
Just choose the numeric (or number?) trigger and set it up.
Then I guess the actions are call service → switch on/off.

If you post this then you will get a proper automation back probably with some explanation, but device automations are not something people like to help with generally.

Hi Paul, in addition to what @Hellis81 is saying you could have a look at Why and how to avoid device_ids in automations and scripts