AM43 blinds control through MQTT

yeah there mite be be change required, since a part of my smart-home is currently shut-downed for a year i’m not able to check it now. feel free for merge request

1 Like

Hi @akrigator, can you please explain how you disable/terminate the BLE connection and then enable once again when you need to issue a command?

I have just set this up on an ESP device and I am already seeing a large battery drain on the blind so would be keen to understand how to terminate the BLE connection.

First you need to create a switch which will allow disable/enable connection for each pairing devices, like:

switch:
  - platform: ble_client
    name: "Bedroom Left Blind BLE"
    id: bedroom_left_blind_ble
    internal: true
    ble_client_id: bedroom_left_blind
    disabled_by_default: true

Then create two scripts to handle connections for previously created switchers

script:
  - id: enable_ble
    mode: restart
    then:
      - logger.log: 
          format: "Enabling BLE..."
          level: INFO
      - switch.turn_on: bedroom_left_blind_ble
      - switch.turn_on: bedroom_right_blind_ble
      - wait_until:
          condition:
            and:
              - binary_sensor.is_on: bedroom_left_blind_connected
              - binary_sensor.is_on: bedroom_right_blind_connected
          timeout: 3min
      - if:
          condition:
            and:
              - binary_sensor.is_on: bedroom_left_blind_connected
              - binary_sensor.is_on: bedroom_right_blind_connected
          then: 
            - logger.log: 
                format: "BLE clients connected"
                level: INFO
          else: 
            - logger.log: 
                format: "Fail connect BLE clients"
                level: WARN

  - id: disable_ble
    mode: restart
    then:
      - logger.log: 
          format: "Disabling BLE..."
          level: INFO
      - switch.turn_off: bedroom_left_blind_ble
      - switch.turn_off: bedroom_right_blind_ble

Finally, use enable_ble in your custom scripts before apply command/action to ble client , like

script:
  - id: open_action
    mode: restart
    then:
      - script.execute: enable_ble
      - script.wait: enable_ble
      - if:
          condition:
            and:
              - binary_sensor.is_on: bedroom_left_blind_connected
              - binary_sensor.is_on: bedroom_right_blind_connected
          then:
            - cover.open: bedroom_left_blind_cover_internal
            - cover.open: bedroom_right_blind_cover_internal

and call disable_ble inside sleeping_ble , which is cycled in main_loop

  - id: sleeping_ble
    mode: restart
    then:
      - while: 
          condition:
            not:
              and:
                - binary_sensor.is_on: bedroom_left_blind_connected
                - binary_sensor.is_on: bedroom_right_blind_connected
          then:
            - logger.log: 
                format: "Conencting BLE again..."
                level: INFO
            - script.execute: enable_ble
            - script.wait: enable_ble
            
      - logger.log: 
          format: "Updating sensors..."
          level: INFO
      - delay: 2min
      
      - logger.log: 
          format: "Check that scripts not running..."
          level: INFO
      - while: 
          condition:
            or:
              - script.is_running: open_action
              - script.is_running: close_action
              - script.is_running: stop_action
              - script.is_running: position_action
          then:
            - logger.log: 
                format: "Still executing..."
                level: INFO
            - delay: 10s
      - script.execute: disable_ble
      - script.wait: disable_ble

  - id: main_loop
    mode: single
    then:
      - while:
          condition:
            - lambda: "return true;"
          then:
            - script.execute: sleeping_ble
            - script.wait: sleeping_ble
            - delay: 60min

Purpose of sleeping_ble usage - hide direct call of disable_ble from action* scripts to avoid their collisions and wait sensor updates, which could not be forced.

So, in other words, the sketch tries to terminate connection politely in main loop as soon as possible, but all actions initiate connections.

To get more info you should prepare a strong coffee and read thoroughly this

1 Like

Amazing, thank you so much. Going to give this a try later today. Cheers

UPDATE: got it working, Thanks @akrigator. Appeciate it