Turning on / of charger based off battery percentage

I have a powered roller blind that i have plugged into a charger. I would like to have the charger turn on if the battery is below 20 % and turn off if the battery is above 90%.

The charger is plugged into a zigbee wall plug
I can do it with 2 separate automations but was wondering if it can be done all in one? I am not good with getting multiple things going in one.

Thanks in advance!

Yes there are multiple methods by which it can be done in a single automation.

One option is to assign Trigger IDs to each numeric state trigger, then use an If/Then action based on a Trigger Condition.

Another option is to use a Threshold sensor with a range:

Then your automation fires when the state of the sensor changes, and it only has one action If-Then, if the sensor is on, that means turn the charger off, else turn the charger on.

In my opinion, this is an elegant solution that allows you to control the range and therefore the thresholds separately from having to edit the automation by hand.

1 Like

thanks mate!

would this work in theory?

alias: bedroom blind toggle
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.bedroom_blind_battery
conditions: []
actions:
  - type: toggle
    device_id: c10e131df246d5e8bd9ac8b4f3289ad0
    entity_id: 33ff29afe27a7872e30da63b133e1d95
    domain: switch
mode: single

Not really…

Issues:

  • One restart or one manual switching action is all it takes to throw your toggle actions off. It is better to use more determinative actions like switch.turn_on and switch.turn_off.
  • Think about how your automation will flow. Assuming it is toggling correctly, when the charge goes above 90, the binary sensor will turn off, and the switch will be toggled off… then after a bit of use, the charge will drop below 90, the binary sensor will turn back on, and the switch will be toggled on. It will never make it to you lower threshold.

no worries at all. How do I do a switch.on or switch.off.

I use the GUI for automations as i don’t really know how to write it with yaml.

would this be any better?

description: ""
mode: single
triggers:
  - trigger: numeric_state
    entity_id:
      - binary_sensor.bedroom_blind_battery
    attribute: lower
    below: 30
conditions: []
actions:
  - type: turn_on
    device_id: c10e131df246d5e8bd9ac8b4f3289ad0
    entity_id: 33ff29afe27a7872e30da63b133e1d95
    domain: switch
  - condition: numeric_state
    entity_id: binary_sensor.bedroom_blind_battery
    above: 90
  - type: turn_off
    device_id: c10e131df246d5e8bd9ac8b4f3289ad0
    entity_id: 33ff29afe27a7872e30da63b133e1d95
    domain: switch

I have also made this one. Its a bit janky but seems to work. I also wanted it it still work if the plug was manually turned on.

alias: master bedroom blind plug battery - test
description: ""
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.master_bedroom_blind_battery_2
    below: 30
  - trigger: state
    entity_id:
      - switch.bedroom_blind_plug
    from: null
conditions: []
actions:
  - type: turn_on
    device_id: c10e131df246d5e8bd9ac8b4f3289ad0
    entity_id: 33ff29afe27a7872e30da63b133e1d95
    domain: switch
  - if:
      - condition: numeric_state
        entity_id: sensor.master_bedroom_blind_battery_2
        above: 90
    then:
      - type: turn_off
        device_id: c10e131df246d5e8bd9ac8b4f3289ad0
        entity_id: 33ff29afe27a7872e30da63b133e1d95
        domain: switch
mode: single

Better, but still not quite there. Conditions are not Waits, they are checked immediately.

Again, think about how the automation flows… The battery charge drops below 30 triggering the automation, then the switch is turned on, next the condition is checked. The condition will always fail, because the value will always be just below 30 since it’s only been charging for maybe a second.

As I described previously, use two triggers and an If/Then action.

That one will also fail to turn off during charging.


You need to trigger on both events when the charge crosses the threshold values:

alias: master bedroom blind plug battery - test
description: ""
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.master_bedroom_blind_battery_2
    above: 90
  - trigger: numeric_state
    entity_id:
      - sensor.master_bedroom_blind_battery_2
    below: 30
  - trigger: state
    entity_id: switch.bedroom_blind_plug
    from: null
conditions: []
actions:
  - if:
      - condition: numeric_state
        entity_id: sensor.master_bedroom_blind_battery_2
        above: 90
    then:
      - type: turn_off
        device_id: c10e131df246d5e8bd9ac8b4f3289ad0
        entity_id: 33ff29afe27a7872e30da63b133e1d95
        domain: switch
    else:
      - condition: numeric_state
        entity_id: sensor.master_bedroom_blind_battery_2
        below: 30
      - type: turn_on
        device_id: c10e131df246d5e8bd9ac8b4f3289ad0
        entity_id: 33ff29afe27a7872e30da63b133e1d95
        domain: switch
mode: single
1 Like

I just stick to two triggers. Simple and solid then.

alias: Turn off phone charger at 84%
description: ""
triggers:
  - trigger: numeric_state
    entity_id: sensor.phone_battery_level
    above: 85
actions:
  - action: switch.turn_off
    data: {}
    target:
      entity_id:
        - switch.charger_socket
mode: single
conditions:
  - condition: state
    entity_id: binary_sensor.phone_charging
    state: "on"
alias: Turn on phone charger socket at 20%
description: Power up charger cable ready
triggers:
  - trigger: numeric_state
    entity_id: sensor.phone_battery_level
    below: 20
actions:
  - action: switch.turn_on
    data: {}
    target:
      entity_id: switch.charger_socket
mode: single
conditions: []

These are two different states you are watching for, so why not have two different automations?

I like to keep things simple. In my case it is a mobile phone with the HA app giving out a charge state.

As it is a mobile I am charging I have sometimes added a “turn on light” or “make a noise” in the 20% test to remind me I need to charge the phone

1 Like

Thanks man! ill give this a go. My tiny mind cant comprehend trigger id lol.