Help needed with an Automation - Switch on Power Utility if needed at night and then switch it back of when charged enough

I want to use as little as possible utility power at my warehouse. In short, at 20:00, check the State of Charge % (SOC) of the Solar battery. If < 75%, then switch the Power Utility on to charge the battery UNTIL it reaches 85%, then switch the Power Utility switch off. Need this to be in ONE Automation to ensure the switching off at 85% only happens if this automation switched on the power utility.

I have all the hardware, I am just struggling to do the Automation part. Can someone please help this newbie.

It makes no difference if it’s one or two automations for this reason. The condition is the same.
The switch can only be on if the battery needed to be charged? So if the switch reacts to the off command then the battery must have been below 75% at 20:00?
Or is this incorrect?

Must be one automation I think and the turning OFF of the Power Utility Switch when the SOC of the Solar battery reaches 85% should only apply if this automation switched the Utility Power on. There are other automations if for example the weather is horrible and there is no PV to switch on the power utility to charge the batteries to 100%. If the switching off at 85% at night is a separate automation it will switch it off during the day as well at 85% when I want it to charge to 100%. Agree?

I am stupid/new to this stuff, so all I need is someone that has extensive knowledge on automations to assist. The request is:

At 20:00, if SOC is <75% at 20:00, switch on a certain Switch UNTIL the SOC reaches 85% however long that takes, then switch that switch off.

I believe it will be much easier with two automations because you need to look at trigger time on the on automation.

description: ""
mode: single
triggers:
  - trigger: time
    at: "20:00:00"
conditions: 
  - condition: numeric_state
    entity_id: sensor.soc
    below: 75
actions:
  - action: switch.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: switch.some_switch
description: ""
mode: single
triggers:
  - trigger: numeric_state
    entity_id: sensor.soc
    above: 84
conditions: 
  - condition: template
    value_template: >
       {{ now() - state_attr("automation.switch_turn_on", 'last_triggered') <= timedelta(hours=8) }}
actions:
  - action: switch.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: switch.some_switch

Notice that you need to change the entity names since you have not said anything about them. And in the off automation it references the on automation, make sure that name is correct.

I would also use two automations. The first is triggered at 20:00 to check the batterylevel, switch the charger on and turn the second automation on. This 2nd automation is triggered when the battery level reaches 85% and turns the switch off and turns itself off. The 2nd automation is only enabled by the 1st automation.

alias: TurnChargerOn
description: ""
triggers:
  - trigger: time
    at: "20:00:00"
conditions:
  - condition: numeric_state
    entity_id: sensor.batterylevel
    below: 75
actions:
  - action: switch.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: switch.esp32_s3_relay_6ch_relay_1
  - action: automation.turn_on
    metadata: {}
    data: {}
    target:
      entity_id:
        - automation.turnchargeroff
mode: single

alias: TurnChargerOff
description: ""
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.batterylevel
    above: 85
conditions: []
actions:
  - action: switch.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: switch.esp32_s3_relay_6ch_relay_1
  - action: automation.turn_off
    metadata: {}
    data:
      stop_actions: true
    target:
      entity_id:
        - automation.turnchargeroff
mode: single

Amazing, you guys are next level and thank you soooo much for being willing to help!

I do not want to be a pain in the behind, but would a quick 2 mins Teams Call with a screenshare to assist to do the above be possible? Even with Teamviewer in on my PC or Anydesk?

I do not have time for a call.
But either of the two automations posted here should work.

I personally don’t use turn off automations in automations.
I reserve that to humans. If I see an automation turned off then I know a human did it (me).

EDIT:
Please note that I made an assumption that the battery takes less than 8 hours to charge.
Change this if I’m wrong.

Thank you Jurgen

Have a questions on your solution:

  1. I do not want to turn off the automation to switch Power utility on when below 75%. In other words, if Automation 1 and 2 runs, will it run again by itself the next day without humanly switching stuff on again or is the Automation then disabled?

There shouldn’t be needed any.
The switch on automation also turns on the “off” automation.

Automation 1 is not stopped and runs again next day. Only automation2 stops itself when triggered by condition met.

I agree - that Toggle switch showing an Automation is Enabled or Disabled…, is that what you are referring to related to Turning Automations On and Off?

If yes, then I agree with you Hellis.

Quick Question…, on your implementation then, related to Automation 2 I have done the below:

description: >-
Part B - After 20:00, if SOC due to Eskom Charging reaches 83% , then switch
Power Utility off.
triggers:

  • trigger: numeric_state
    entity_id: 9d32aceb8d1aa6a9a9726b99ec089481
    above: 83
    conditions:
  • condition: template
    value_template: >
    {{ now() - state_attr(“automation.switch_turn_on”, ‘last_triggered’) <=
    timedelta(hours=4) }}
    actions:
  • action: switch.turn_off
    metadata: {}
    data: {}
    target:
    entity_id: a7eeabeea694e5709fba1c014c3f41e0
    mode: single
    alias: >-
    Part B - After 20:00, if SOC due to Eskom Charging reaches 83% , then switch
    Power Utility off.

Related to " {{ now() - state_attr(“automation.switch_turn_on”, ‘last_triggered’) <= timedelta(hours=4) }}"

  1. Should I amend something in the above sentence related to switch name or anything.
  2. Related to ‘last_triggered’, is this specifically when the switch was turned on even if by any other Automation, then this will apply if within the last 4 hours being from 20:00 to 00:00?

You need to edit the post with proper formating.
use the button </> to enter code.

It’s very hard to read the code as it is, but entity_id: 9d32aceb8d1aa6a9a9726b99ec089481 is not correct.
That is probably some device id or something.
Use developer tools to get the proper entity id.
Same thing here: entity_id: a7eeabeea694e5709fba1c014c3f41e0

  1. No nothing should be added. What you need to do is replace the automation.switch_turn_on with the name of the automation you called the turn on automation.

  2. No… This is when the automation was last triggered. So it should be 20:00. It has nothing to do with the switch itself.
    I get a bit unsure now that I think, but I believe the last triggered only updates when the automation has run, so if the condition says stop then the last triggered does not update.

Does this look ok/better related to Automation 1/Part A?

Yes. That looks good

And Does this look ok/better related to Automation 2/Part B?

That looks wrong.
Open developer tools and search for the automation name as entity id (filter entities).
It should start with automation and should not include spaces, and probably some of the other characters.

If I Click on Developer Tools, then at the top menu you have “YAML” then “States”, “Actions”, etc.

The screenshot is from States but shows ID and then a Friendly_Name. Is what I have not correct?

The blue text is the entity id. click on the copy icon and you will get the entity id.
This should be pasted in the state_attr template.