Automating Shutter Position Based on Time with MQTT

I’ve been working on automating my balcony shutter using the Sonoff Dual R2 and MQTT in Home Assistant. While I’ve got the basic functionality down, I wanted to take it a step further by having the position update automatically based on how long the shutter is moving. Here’s what I came up with!

First, I set up the MQTT template cover as follows:

yaml

  • platform: template
    covers:
    balcony_bedroom_cover:
    friendly_name: “Balcony Bedroom Shutter”
    open_cover:
    - service: mqtt.publish
    data:
    topic: ‘cmnd/bedroombalcony/power2’
    payload: ‘OFF’
    - service: mqtt.publish
    data:
    topic: ‘cmnd/bedroombalcony/power1’
    payload: ‘ON’
    close_cover:
    - service: mqtt.publish
    data:
    topic: ‘cmnd/bedroombalcony/power1’
    payload: ‘OFF’
    - service: mqtt.publish
    data:
    topic: ‘cmnd/bedroombalcony/power2’
    payload: ‘ON’
    stop_cover:
    - service: mqtt.publish
    data:
    topic: ‘cmnd/bedroombalcony/power1’
    payload: ‘OFF’
    - service: mqtt.publish
    data:
    topic: ‘cmnd/bedroombalcony/power2’
    payload: ‘OFF’
    position:
    - value_template: >-
    {% if is_state(‘cover.balcony_bedroom_cover’, ‘opening’) %}
    {{ 100 - ((now() - last_triggered(‘balcony_bedroom_cover’)) | 0) / 45 * 100 }}
    {% elif is_state(‘cover.balcony_bedroom_cover’, ‘closing’) %}
    {{ ((now() - last_triggered(‘balcony_bedroom_cover’)) | 0) / 50 * 100 }}
    {% else %}
    {{ state_attr(‘cover.balcony_bedroom_cover’, ‘position’) }}
    {% endif %}

This setup tracks how long the shutter has been moving and adjusts the position accordingly. For opening, it decreases from 100% to 0% over 45 seconds, and for closing, it increases from 0% to 100% over 50 seconds. The position is updated every second, giving a smooth transition.

I also modified the stop automation to ensure the position is set correctly when the shutter stops:

yaml

  • alias: ‘Stop down shutter after x time’
    trigger:
    platform: mqtt
    topic: cmnd/bedroombalcony/power1
    payload: ‘ON’
    action:

    • delay: ‘00:00:45’
    • service: cover.stop_cover
      entity_id: cover.balcony_bedroom_cover
    • service: cover.set_cover_position
      entity_id: cover.balcony_bedroom_cover
      data:
      position: 0
  • alias: ‘Stop up shutter after x time’
    trigger:
    platform: mqtt
    topic: cmnd/bedroombalcony/power2
    payload: ‘ON’
    action:

    • delay: ‘00:00:50’
    • service: cover.stop_cover
      entity_id: cover.balcony_bedroom_cover
    • service: cover.set_cover_position
      entity_id: cover.balcony_bedroom_cover
      data:
      position: 100

This ensures that if the shutter is stopped manually, it still reports the correct position. I’ve been testing this setup for a few days now, and it’s working perfectly! The position updates smoothly, and I can even create automations based on the shutter’s position.

If anyone has any suggestions or improvements, I’d love to hear them!