Timer trigger for a plug for battery charge

i need to create an on/off timer for a plug that is at 1:05h when a battery entity is at 27% and then adds 5 minutes for every 2% more the battery loses (for example 25% = 1:10h 23% = 1:15h 21%= 1:20 and so on) i can’t do a normal battery charge automation because the device is not on when charging so only the last state is known

Starting the timer would need a templated action like:

service: timer.start
data:
  duration: |
    {% set b_level = states('sensor.YOUR_BATTERY') | int %}
    {{ ((-150 * b_level) + 7950)|round(-2)|int | timestamp_custom("%H:%M:00", false) }}
target:
  entity_id: timer.YOUR_TIMER

If you describe how the rest of the automation is supposed to work we can likely help you put all the pieces together.

now is like this

alias: pulse elite charging
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.pulse_elite_charger
    id: "on"
    from: "off"
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 0
  - platform: numeric_state
    entity_id:
      - sensor.presa_termosifone_scrivania_switch_0_power
    for:
      hours: 1
      minutes: 5
      seconds: 0
    id: "off"
    above: 1
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - "on"
          - condition: template
            value_template: >-
              {{ now() - state_attr('automation.avvio', 'last_triggered') >
              timedelta(minutes=2) }} 
          - condition: numeric_state
            entity_id: sensor.pulse_elite_battery_level_last
            below: 27
          - condition: state
            entity_id: input_boolean.pulse_elite_charged
            state: "off"
          - condition: template
            value_template: >-
              {{ now() - states('sensor.z2m_last_updated') | as_datetime |
              as_local > timedelta(minutes=2) }}
        sequence:
          - service: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.presa_termosifone_scrivania_switch_0
      - conditions:
          - condition: trigger
            id:
              - "off"
          - condition: state
            entity_id: binary_sensor.pulse_elite_charger
            state: "on"
        sequence:
          - service: input_boolean.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.pulse_elite_charged
          - service: switch.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: switch.presa_termosifone_scrivania_switch_0
mode: single

but this way i have to modify the on time everytime the charge is below 27% (percentage before the last turn off) for the battery to arrive at 80%
but if i have to do it manually it’s not an automation anymore

You’ll need to create a timer helper and paste its entity ID in the 2 marked places.

alias: pulse elite charging
description: ""
trigger:
  - id: "on"
    platform: state
    entity_id:
      - binary_sensor.pulse_elite_charger
    from: "off"
    to: "on"
  - id: "off"
    platform: event
    event_type: timer.finished
    event_data:
      entity_id: ## YOUR TIMER ##
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: "on"
          - condition: template
            value_template: >-
              {{ now() - state_attr('automation.avvio', 'last_triggered') > timedelta(minutes=2) }} 
          - condition: numeric_state
            entity_id: sensor.pulse_elite_battery_level_last
            below: 27
          - condition: state
            entity_id: input_boolean.pulse_elite_charged
            state: "off"
          - condition: template
            value_template: >-
              {{ now() - states('sensor.z2m_last_updated') | as_datetime |
              as_local > timedelta(minutes=2) }}
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.presa_termosifone_scrivania_switch_0
          - service: timer.start
            data:
              duration: |
                {% set b_level = states('sensor.pulse_elite_battery_level_last') | int %}
                {{ ((-150 * b_level) + 7950)|round(-2)|int | timestamp_custom("%H:%M:00", false) }}
            target:
              entity_id: ## YOUR TIMER ##
      - conditions:
          - condition: trigger
            id: "off"
          - condition: state
            entity_id: binary_sensor.pulse_elite_charger
            state: "on"
        sequence:
          - service: input_boolean.turn_on
            target:
              entity_id: input_boolean.pulse_elite_charged
          - service: switch.turn_off
            target:
              entity_id: switch.presa_termosifone_scrivania_switch_0
mode: single

like this?


the reset option has to be checked or unchecked?
coud you plese insert your modification here? i’m a little disoriented by yaml so i’m afraid i will make some mistake

alias: pulse elite charging
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.pulse_elite_charger
    id: "on"
    from: "off"
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 0
  - platform: numeric_state
    entity_id:
      - sensor.presa_termosifone_scrivania_switch_0_power
    for:
      hours: 1
      minutes: 5
      seconds: 0
    id: "off"
    above: 1
  - platform: state
    entity_id:
      - sensor.pulse_elite_battery_level
    id: used
    from: unavailable
    for:
      hours: 0
      minutes: 2
      seconds: 0
  - platform: state
    entity_id:
      - sensor.pulse_elite_battery_level
    id: "in use "
    from: unavailable
  - platform: state
    entity_id:
      - sensor.pulse_elite_battery_level
    id: "non in use "
    to: unavailable
  - platform: state
    entity_id:
      - script.ps5_watch
      - script.pc_watch
      - script.switch_watch
    id: ps link sync
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - "on"
          - condition: template
            value_template: >-
              {{ now() - state_attr('automation.avvio', 'last_triggered') >
              timedelta(minutes=2) }} 
          - condition: numeric_state
            entity_id: sensor.pulse_elite_battery_level_last
            below: 27
          - condition: state
            entity_id: input_boolean.pulse_elite_charged
            state: "off"
          - condition: template
            value_template: >-
              {{ now() - states('sensor.z2m_last_updated') | as_datetime |
              as_local > timedelta(minutes=2) }}
        sequence:
          - service: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.presa_termosifone_scrivania_switch_0
      - conditions:
          - condition: trigger
            id:
              - "off"
          - condition: state
            entity_id: binary_sensor.pulse_elite_charger
            state: "on"
        sequence:
          - service: input_boolean.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.pulse_elite_charged
          - service: switch.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: switch.presa_termosifone_scrivania_switch_0
      - conditions:
          - condition: trigger
            id:
              - used
          - condition: template
            value_template: >-
              {{ now() - state_attr('automation.avvio', 'last_triggered') >
              timedelta(minutes=2) }} 
          - condition: template
            value_template: >-
              {{ now() - states('sensor.z2m_last_updated') | as_datetime |
              as_local > timedelta(minutes=2) }}
        sequence:
          - service: input_boolean.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.pulse_elite_charged
      - conditions:
          - condition: trigger
            id:
              - "in use "
        sequence:
          - service: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.ps_link
      - conditions:
          - condition: trigger
            id:
              - "non in use "
        sequence:
          - service: switch.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: switch.ps_link
      - conditions:
          - condition: trigger
            id:
              - ps link sync
          - condition: not
            conditions:
              - condition: state
                entity_id: sensor.pulse_elite_battery_level
                state: unavailable
        sequence:
          - service: switch.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: switch.ps_link
          - delay:
              hours: 0
              minutes: 0
              seconds: 2
              milliseconds: 0
          - service: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.ps_link
mode: single

before i cut the other condition to make the automation more readable

Checked

alias: pulse elite charging
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.pulse_elite_charger
    id: "on"
    from: "off"
    to: "on"
  - id: "off"
    platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.pulse_elite_charging_timer
  - platform: state
    entity_id:
      - sensor.pulse_elite_battery_level
    id: used
    from: unavailable
    for:
      hours: 0
      minutes: 2
      seconds: 0
  - platform: state
    entity_id:
      - sensor.pulse_elite_battery_level
    id: "in use "
    from: unavailable
  - platform: state
    entity_id:
      - sensor.pulse_elite_battery_level
    id: "non in use "
    to: unavailable
  - platform: state
    entity_id:
      - script.ps5_watch
      - script.pc_watch
      - script.switch_watch
    id: ps link sync
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - "on"
          - condition: template
            value_template: >-
              {{ now() - state_attr('automation.avvio', 'last_triggered') >
              timedelta(minutes=2) }} 
          - condition: numeric_state
            entity_id: sensor.pulse_elite_battery_level_last
            below: 27
          - condition: state
            entity_id: input_boolean.pulse_elite_charged
            state: "off"
          - condition: template
            value_template: >-
              {{ now() - states('sensor.z2m_last_updated') | as_datetime |
              as_local > timedelta(minutes=2) }}
        sequence:
          - service: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.presa_termosifone_scrivania_switch_0
          - service: timer.start
            data:
              duration: |
                {% set b_level = states('sensor.pulse_elite_battery_level_last') | int %}
                {{ ((-150 * b_level) + 7950)|round(-2)|int | timestamp_custom("%H:%M:00", false) }}
            target:
              entity_id: timer.pulse_elite_charging_timer
      - conditions:
          - condition: trigger
            id:
              - "off"
          - condition: state
            entity_id: binary_sensor.pulse_elite_charger
            state: "on"
        sequence:
          - service: input_boolean.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.pulse_elite_charged
          - service: switch.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: switch.presa_termosifone_scrivania_switch_0
      - conditions:
          - condition: trigger
            id:
              - used
          - condition: template
            value_template: >-
              {{ now() - state_attr('automation.avvio', 'last_triggered') >
              timedelta(minutes=2) }} 
          - condition: template
            value_template: >-
              {{ now() - states('sensor.z2m_last_updated') | as_datetime |
              as_local > timedelta(minutes=2) }}
        sequence:
          - service: input_boolean.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.pulse_elite_charged
      - conditions:
          - condition: trigger
            id:
              - "in use "
        sequence:
          - service: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.ps_link
      - conditions:
          - condition: trigger
            id:
              - "non in use "
        sequence:
          - service: switch.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: switch.ps_link
      - conditions:
          - condition: trigger
            id:
              - ps link sync
          - condition: not
            conditions:
              - condition: state
                entity_id: sensor.pulse_elite_battery_level
                state: unavailable
        sequence:
          - service: switch.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: switch.ps_link
          - delay:
              hours: 0
              minutes: 0
              seconds: 2
              milliseconds: 0
          - service: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.ps_link
mode: single

thanks a lot