Create automation to charge solar battery during daily cheap rate electricity period

Hi, I am newish to Home Assistant and love it. Still getting to grips with automations. I am looking to create an automation to force charge the batteries connected to my solar during the cheap rate electricity period (23:30 - 05:30).

I currently have 2 automations setup to start and stop charging at the times above. Yes, very basic and I believe this can be done in 1 automation. The yaml code is pasted below…

Start charge

alias: Charge Home Battery
description: “”
trigger:

  • platform: time
    at: “23:30:00”
    condition:
    action:
  • device_id: b2e7f88987acec1a852bdec7191b879a
    domain: select
    entity_id: a05fe497a7ca8fc8813ace3a6dccb456
    type: select_last
    mode: single

Stop Charge

alias: Stop Home Battery Charging
description: “”
trigger:

  • platform: time
    at: “05:30:00”
    condition:
    action:
  • device_id: b2e7f88987acec1a852bdec7191b879a
    domain: select
    entity_id: a05fe497a7ca8fc8813ace3a6dccb456
    type: select_previous
    mode: single

Would someone be so kind to let me know a better way to do this, perhaps in a single automation ?

Thank You !

Richard

if it were me, i’d take a slightly different approach.

i’d create a “times of the day sensor” helper. that way you can easily adjust the times… i hate hardcoding things like times in the automation code.

then one automation like this:

description: ""
mode: single
trigger:
  - platform: state
    entity_id:
      - binary_sensor.test_time
condition: []
action:
  - if:
      - condition: state
        entity_id: binary_sensor.test_time
        state: "on"
    then:
      - service: light.turn_on
        target:
          entity_id: light.alcove_2
    else:
      - service: light.turn_off
        target:
          entity_id: light.alcove

a few notes…

  1. the above could be made even simpler with a single template…
    e.g. in my example have it call service light.turn_{{ states(‘binary_sensor.test_time’) }}
    but if you’re just starting out, this is perhaps clear straightforward logic that you can see & edit in the ui.

  2. you currentlyhave device_id and entity_id in your automation. i’m not sure why… i don’t know what device you’re using… but if possible, you’re better off to use entity_id only if you can, and furthermore use a more human-readable entity name (like light.alcove above)

  3. of course replace all of my entities and such with your own! just showing you the logic flow.

  4. in your posts, you should use the pre-formatted text format for code. it preserves the indentation and readability.

hope this helps! cheers!

condensed untested example of how to shrink it with a template
i don’t know the device, so just writing freehand… but hopefully it gives you the idea.

if you’re just starting out with ha, i’d do the above for now…

description: ""
mode: single
trigger:
  - platform: state
    entity_id:
      - binary_sensor.test_time
condition: []
action:
  device_id: b2e7f88987acec1a852bdec7191b879a
  domain: select
  entity_id: a05fe497a7ca8fc8813ace3a6dccb456
  type: >
    {{ 'select_last' if (states('binary_sensor.test_time') == 'on') else 'select_previous' }} 
mode: single