Time above and below trigger issue

Hi,

I’m new to home assistant and have an issue.

I have integrated my solar inverter and have for example the when set to when the battery percentage drops below a certain level it will charge.

If I just have the when and then do set it works but I would like to set the and if to check if the time is above 23:30 for example but when I set that the automation doesn’t run.

Any ideas.

Please share your automation YAML correctly formatted for the forum.

alias: Test
description: ""
trigger:
  - type: battery_level
    platform: device
    device_id: ad77094e2a4ab2ea42d2651055d5de64
    entity_id: b500bfe8706123698104c289e24011de
    domain: sensor
    below: 95
condition:
  - condition: time
    after: "12:05:00"
action:
  - device_id: ad77094e2a4ab2ea42d2651055d5de64
    domain: number
    entity_id: 2058baf222419feb3d11ce006b4b68e6
    type: set_value
    value: 100
  - type: turn_on
    device_id: ad77094e2a4ab2ea42d2651055d5de64
    entity_id: 85a40bdd5831db74670dfb73d5ed6300
    domain: switch
  - service: notify.mobile_app_jamie_s_iphone
    metadata: {}
    data:
      title: Test automation
      message: Test
mode: single

That is a test one I setup but it wont run no matter what time I set in the future.

Your battery state of charge has to go from above 95 to below 95 to trigger. It won’t trigger if it is already below 95.

To get around this, trigger on all valid changes of your battery level then check if it is below the required level in another condition:

alias: Test
description: ""
trigger:
  - platform: state
    entity_id: b500bfe8706123698104c289e24011de
    not_to:
      - unknown
      - unavailable
condition:
  - condition: numeric_state
    entity_id: b500bfe8706123698104c289e24011de
    below: 95
  - condition: time
    after: "12:05:00"
action:
  - device_id: ad77094e2a4ab2ea42d2651055d5de64
    domain: number
    entity_id: 2058baf222419feb3d11ce006b4b68e6
    type: set_value
    value: 100
  - type: turn_on
    device_id: ad77094e2a4ab2ea42d2651055d5de64
    entity_id: 85a40bdd5831db74670dfb73d5ed6300
    domain: switch
  - service: notify.mobile_app_jamie_s_iphone
    metadata: {}
    data:
      title: Test automation
      message: Test
mode: single

You should read this advice about avoiding device ids too: Why and how to avoid device_ids in automations and scripts

1 Like

Thanks for the help, do I need to edit anything in the edited code you posted.

Using the actual entity_id rather than the identifier number for the entity would be a good idea, as per the link.

e.g.

this:

entity_id: sensor.your_sensor

not this:

entity_id: b500bfe8706123698104c289e24011de

Thank you, I now have the trigger checking for any state or attribute changes to the battery and then have the condition set for the time and below a certain percentage.

One thing I have now come across is it now wants to constantly run even if the conditions have been met and the actions ran.

Is there a way to stop it running once the conditions have been met and the action has run. I only want to stop it until the following day.

Thanks.

what’s the specific criteria that should trigger it to turn things off? when that numeric state hits 100?

also post your latest code. easier to work from working code

This is an edited version but the same automation.

As you can see I have the battery sensor set to trigger on any state change, then the condition to check its after 23.30 and the battery percentage is below 12%.

I know the way I have it setup it will constantly trigger which is what I want but the way I would like it to work is that once the conditions are met and the actions have run it doesn’t then run again until the next day.

Hope that makes sense.


alias: 4. Charge Solar Battery after 23:30pm
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.lux_battery
condition:
  - condition: time
    after: "23:30:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
  - condition: numeric_state
    entity_id: sensor.lux_battery
    below: 12
action:
  - type: turn_off
    device_id: ad77094e2a4ab2ea42d2651055d5de64
    entity_id: 85a40bdd5831db74670dfb73d5ed6300
    domain: switch
  - device_id: ad77094e2a4ab2ea42d2651055d5de64
    domain: number
    entity_id: 2058baf222419feb3d11ce006b4b68e6
    type: set_value
    value: 100
  - type: turn_on
    device_id: ad77094e2a4ab2ea42d2651055d5de64
    entity_id: 9fc721d2c3ef5eb78702bb2fb8223b6d
    domain: switch
  - service: notify.mobile_app_jamie_s_iphone
    metadata: {}
    data:
      message: Please check the Lux app.
      title: Solar Battery now Charging
mode: single


alias: Solar 3. Discharge Solar Battery if Below 60% at 23:00pm
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.lux_battery
    enabled: true
condition:
  - condition: time
    after: "23:00:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
  - condition: numeric_state
    entity_id: sensor.lux_battery
    below: 60
action:
  - device_id: ad77094e2a4ab2ea42d2651055d5de64
    domain: number
    entity_id: 2058baf222419feb3d11ce006b4b68e6
    type: set_value
    value: 100
  - type: turn_on
    device_id: ad77094e2a4ab2ea42d2651055d5de64
    entity_id: 85a40bdd5831db74670dfb73d5ed6300
    domain: switch
  - service: notify.mobile_app_jamie_s_iphone
    metadata: {}
    data:
      title: "Solar Battery now Discharging to the Grid "
      message: Please check the Lux app.
  - delay:
      hours: 23
      minutes: 30
      seconds: 0
      milliseconds: 0
mode: single


alias: Solar 4. Charge Solar Battery after 23:30pm
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.lux_battery
condition:
  - condition: time
    after: "23:30:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
  - condition: numeric_state
    entity_id: sensor.lux_battery
    below: 12
action:
  - type: turn_off
    device_id: ad77094e2a4ab2ea42d2651055d5de64
    entity_id: 85a40bdd5831db74670dfb73d5ed6300
    domain: switch
  - device_id: ad77094e2a4ab2ea42d2651055d5de64
    domain: number
    entity_id: 2058baf222419feb3d11ce006b4b68e6
    type: set_value
    value: 100
  - type: turn_on
    device_id: ad77094e2a4ab2ea42d2651055d5de64
    entity_id: 9fc721d2c3ef5eb78702bb2fb8223b6d
    domain: switch
  - service: notify.mobile_app_jamie_s_iphone
    metadata: {}
    data:
      message: Please check the Lux app.
      title: Solar Battery now Charging
  - delay:
      hours: 23
      minutes: 30
      seconds: 0
      milliseconds: 0
mode: single

Would either of the automations above achieve what I want.

I know I may need to play with how long I delay for.

alias: Test 4. Charge Solar Battery after 23:30pm
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.lux_battery
condition:
  - condition: time
    after: "23:30:00"
  - condition: numeric_state
    entity_id: sensor.lux_battery
    below: 12
  - condition: template
    value_template: >
      {{ (this.attributes.last_triggered | as_local).date() != now().date() if this.attributes.last_triggered is defined else true }}
action:
  - device_id: ........

Because you used device_id my editor mangled it… So I’m cutting off to just the condition part which is the key aspect…

It looks at the last triggered date and today’s date and says they must be different dates

your approach of doing a delay (or do a wait for midnight) would also work. However there’s no need to wait for 23 hours. Just wait for 30 minutes. Once it’s past midnight your condition of 23:30 takes over

Your way does have the vulnerability that if there is a restart before midnight it doesn’t resume so it will trigger again.

I think I have mine right, but I am on mobile and did not test it…

1 Like

I would use a toggle helper for this. In your script, you toggle the helper on indicating it’s run for the day and have that toggle as part of your conditions (if toggle is false and other conditions pass, run the actions, toggle the toggle and then further executions of the automation would not pass).

Then, I would reset the toggle daily at 23:15 via a “reset” automation.

Template condition… 100%.

@armedad FTW! I completely forgot about the template condition… Nicely done!!!

1 Like

I also nuked the enumeration of every day. That is unnecessary if you want every day


alias: Solar 4. Charge Solar Battery after 23:30pm
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.lux_battery
condition:
  - condition: time
    after: "23:30:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
  - condition: numeric_state
    entity_id: sensor.lux_battery
    below: 12
  - condition: template
    value_template: >
      {{ (this.attributes.last_triggered | as_local).date() != now().date() if
      this.attributes.last_triggered is defined else true }}
action:
  - type: turn_off
    device_id: ad77094e2a4ab2ea42d2651055d5de64
    entity_id: 85a40bdd5831db74670dfb73d5ed6300
    domain: switch
  - device_id: ad77094e2a4ab2ea42d2651055d5de64
    domain: number
    entity_id: 2058baf222419feb3d11ce006b4b68e6
    type: set_value
    value: 100
  - type: turn_on
    device_id: ad77094e2a4ab2ea42d2651055d5de64
    entity_id: 9fc721d2c3ef5eb78702bb2fb8223b6d
    domain: switch
  - service: notify.mobile_app_jamie_s_iphone
    metadata: {}
    data:
      message: Please check the Lux app.
      title: Solar Battery now Charging
mode: single

Thank you very much, can I just confirm I have put it in the correct place, does it look correct.

yeah. looks right eyeballing it. i’m not able to test it, but it looks right.

There’s a blueprint that does this: