Help with approach on a timed event that turns on an outlet, waits 5 minutes, then turns it off when the power drops below 5 watts

Hi, I have a battery powered device that I want to be recharged every few weeks. The charger is permanently plugged into a Sonoff S31 outlet/power meter running Tasmota which is set up in Home Assistant.

I was thinking to have Home Assistant turn on the charger every three weeks, wait a minute for the power reading from the S31 to stabilize, then turn it off when the power drops below 5 watts.

This would let the battery run down over a few weeks, then charge it up without letting it continue charging once it is full.

I know this is overkill but I enjoy this kind of project!

I’m not familiar with all the options for scripting Home Assistant - does anyone have general suggestions on a good way to do this?
Thanks

I have some Lithium Ion batteries I do this exact thing with but I recharge them every week. You could easily modify it to 3 weeks.

you need an input_datetime to hold the date & time the charger was turned off:

input_datetime:
  charger_1_off_time:
    name: Charger 1 Turned Off At
    has_date: true
    has_time: true

the automations include announcements to let me know when charging is complete.

And I have several batteries I monitor/charge so they are included where appropriate.

then the automations for one charger become:

automation:
  - alias: Garage Power Charger 1 Check Charging
    trigger:
      - platform: state
        entity_id: switch.garage_power_strip_switch_1
        to: 'on'
        for:
          minutes: 5
      - platform: numeric_state
        entity_id: sensor.garage_power_strip_power_1
        below: 3.0
        for:
          minutes: 5
    condition:
      - condition: template
        value_template: '{{ states("sensor.garage_power_strip_power_1") | float < 3.0 }}'
      - condition: state
        entity_id: switch.garage_power_strip_switch_1
        state: 'on'
    action:
      - service: switch.turn_off
        entity_id: switch.garage_power_strip_switch_1
      - condition: time
        before: '20:00:00'
      - condition: time
        after: '10:00:00'
      - service: notify.alexa_media
        data:
          target: 
            - media_player.garage_dot
            - media_player.kitchen_dot
          data:
            type: tts 
          message: "Charger number 1 has either completed charging or doesn't have a battery connected and it has been turned off."

  - alias: Garage Power Chargers Set Off Time
    trigger:
      - platform: state
        entity_id:
          - switch.garage_power_strip_switch_1
          - switch.garage_power_strip_switch_2
          - switch.garage_power_strip_switch_3
          - switch.garage_power_strip_switch_4
        to: 'off'
    action:
      - service: input_datetime.set_datetime
        data:
          entity_id: >
            {% if trigger.entity_id == 'switch.garage_power_strip_switch_1' %}
              input_datetime.charger_1_off_time
            {% elif trigger.entity_id == 'switch.garage_power_strip_switch_2' %}
              input_datetime.charger_2_off_time
            {% elif trigger.entity_id == 'switch.garage_power_strip_switch_3' %}
              input_datetime.charger_3_off_time
            {% elif trigger.entity_id == 'switch.garage_power_strip_switch_4' %}
              input_datetime.drill_charger_off_time
            {% endif %}
          timestamp: "{{ now().timestamp() }}"

  - alias: Garage Power Chargers Refresh Battery Charge
    id: garage_power_chargers_refresh_battery_charge
    trigger:
      - platform: template
        value_template: "{{ as_timestamp(now()) - state_attr('input_datetime.charger_1_off_time', 'timestamp') | int > 86400 * 7 }}"
        id: 'chg_1'
      - platform: template
        value_template: "{{ as_timestamp(now()) - state_attr('input_datetime.charger_2_off_time', 'timestamp') | int > 86400 * 7 }}"
        id: 'chg_2'
      - platform: template
        value_template: "{{ as_timestamp(now()) - state_attr('input_datetime.charger_3_off_time', 'timestamp') | int > 86400 * 7 }}"
        id: 'chg_3'
      - platform: template
        value_template: "{{ as_timestamp(now()) - state_attr('input_datetime.drill_charger_off_time', 'timestamp') | int > 86400 * 7 }}"
        id: 'chg_4'
    action:
      - service: switch.turn_on
        data: 
          entity_id: >
            {% if  trigger.id == 'chg_1' %}
              switch.garage_power_strip_switch_1
            {% elif trigger.id == 'chg_2' %}
              switch.garage_power_strip_switch_2
            {% elif trigger.id == 'chg_3' %}
              switch.garage_power_strip_switch_3
            {% elif trigger.id == 'chg_4' %}
              switch.garage_power_strip_switch_4
            {% endif %}

Thank you, this is very helpful, I was able to adapt it to my setup and it looks like it will work.