Mqtt -Template Cover - Sonoff Dual - Please help automate position based on time

Hi, I am using the mqtt template cover to trigger a sonoff dual r2 embedded in the wall in order to drive the shutter motor. I know that the shutter needs around 45 seconds to fully close from open and around 50 seconds to go from closed to full open. Since I don’t drive the motor from any other source except HA the state cannot change from any other source so whatever state HA will store will be the one.
I would like to incorporate a logic to update the cover state but I don’t know how to implement it. I call your expertise for help.

so this is the code I use

- 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'

I want to have an automation (?) that monitors the topic and if power1 ON detected counts how many seconds till it detects power1 OFF and the same for power2. for every 1 second, the position is updated by 2 open=100 closed=0.
Starting from fully open (1 st time usage) state will be 100 then if power1 ON for 10 seconds then status will update to 80. if then it detects the POWER2 ON for 5 seconds then the status should be updated to 90. hope you get logic here.

now I use a very simple automation in order to close the sonoff and it works great. I don’t care if the motor shutter reaches a bit earlier the fully open or fully closed position because the motor is calibrated to stop when the shutters reach a specific point so no harm if sonoff is still open even after the motor has been stopped.

- 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

- 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

Can someone help me toward the right direction? thank youuuuu.

1 Like

sorry for bumping, I made some progress but my knowledge is really really limited
I have found a way to know how much time the ON payload is active by creating a binary sensor and counting the time by using this template:

{% if is_state('binary_sensor.power1_sensor', 'on') %}
'{{(as_timestamp(now()) - as_timestamp(states.binary_sensor.power1_sensor.last_changed))}}'
{% else %}
{% endif %}

how can I use this in the automation and to get closer solving my problem in the first post?

Hi, I am also using sonoff for my shutter, do you have any progress making the time based position sensor?

Hi I have solved it by looking at this thread

It has all the info you need.

1 Like

Thanks, that help me a lot