Template Cover - how to get position in ESPHome set by HA UI

Hi,
I am setting up a template cover, which has a tilt and a position value (not just closed/open), then I need to do some calculations in lambda.
I know that I can use lambda in ‘tilt_action’ to get the tilt value I set in HA, but I can’t figure out how to get the position.

When I click open/close buttons in HA UI, open/close_actions get triggered, but when I set a manual position in HA, nothing is triggered, so I don’t know where to get the position value… I am missing some kindof ‘position_action’ in ESPHome… Couldn’t find anything in docs. I am on a version 1.14.3.

Do you want to post your yaml?

Sure, although it’s just a bunch of test stuff right now. I am just missing this one piece, something like a “position_action” to get a variable with requested position value from HA UI.
Please note that there are two covers (time_cover and template), I am using the time cover to control the actual motor and to use it’s timing function to estimate position, but actually the template cover is exposed to HA to add tilt functionality, and I need to get the position value from template cover to sync it with the time cover.

cover:
  - platform: time_based
    name: "Living Room Blinds - Cover"
    id: time_cover

    open_action:
      - script.execute: open_cover
    #  - switch.turn_off: motor_down
    #  - switch.turn_on: motor_up
    open_duration: 10sec

    close_action:
      - script.execute: close_cover
    #  - switch.turn_off: motor_up
    #  - switch.turn_on: motor_down
    close_duration: 10sec

    stop_action:
      - script.execute: stop_cover
    #  - switch.turn_off: motor_up
    #  - switch.turn_off: motor_down
      
  - platform: template
    name: "Living Room Template Blind"
    has_position: true
    optimistic: True
    assumed_state: True
    open_action: 
      - script.execute: close_cover_templ
      - cover.open: time_cover
    close_action:
      - script.execute: close_cover_templ
      - cover.close: time_cover
    stop_action:
      - cover.stop: time_cover
    tilt_action:
      - logger.log:
          format: "Tilting to: %.2f"
          args: ["tilt"]
   

script:
  - id: tilt_cover
    then:
      - logger.log: Tilting cover
  - id: open_cover
    then:
      - logger.log: Opening cover
  - id: close_cover
    then:
      - logger.log: Closing cover
  - id: stop_cover
    then:
      - logger.log: Stopping cover
  - id: close_cover_templ
    then:
      - lambda: |-
          ESP_LOGD("main", "Closing template cover");
  - id: open_cover_templ
    then:
      - lambda: |-
          ESP_LOGD("main", "Opening template cover");
        
switch:
  - platform: gpio
    pin: 4
    name: "Living Room Blinds - UP"
    id: motor_up
    interlock: [motor_down]
    interlock_wait_time: 100ms
    restore_mode: always off
  - platform: gpio
    pin: 15
    name: "Living Room Blinds - DOWN"
    id: motor_down
    interlock: [motor_up]
    interlock_wait_time: 100ms
    restore_mode: always off
    
binary_sensor:
  - platform: gpio
    pin: 5
    name: "Living Room Blinds - UP Button"
    on_press:
      then:
        - switch.turn_off: motor_down
        - switch.turn_on: motor_up
    on_release:
      then:
        - switch.turn_off: motor_up
  - platform: gpio
    pin: 13
    name: "Living Room Blinds - DOWN Button"
    on_press:
      then:
        - switch.turn_off: motor_up
        - switch.turn_on: motor_down
    on_release:
      then:
        - switch.turn_off: motor_down

Is it in the base cover component? Easy to overlook this stuff sometimes https://esphome.io/components/cover/index.html

No, it is not unfortunately. I checked that one too.
In the base cover there is only an action to control the cover, to set the tilt and position manually, this is what I need to do with the time_based cover, but what I want is to retrieve the set position from the template cover, same as it works with tilt.
I will try to simplify the example a bit. Let’s assume you have just this simple template cover:

cover:
  - platform: template
    name: "Cover"
    has_position: true
    optimistic: True
    assumed_state: True
    open_action: 
      #Anything
    close_action:
      #Anything
    stop_action:
      #Anything
    tilt_action:
      - logger.log:
          format: "Tilting to: %.2f"
          args: ["tilt"]

This will effectively expose as new cover entity in HA, where I can manually set the tilt and position.
Now when I change the tilt value on the tilt slider, the tilt_action will get triggered and the “Tilting to: xx %” will get logged to the ESP log. This now works. And I just need to do exactly the same thing for the position.
I need something like:

    position_action:
      - logger.log:
          format: "Moving cover to: %.2f"
          args: ["pos"]

But there is nothing like that…
I hope this clears my question a bit :slight_smile:

Well, actually…

I have upgraded my roller blinds last weekend and have installed esphome 1.5.0-dev. There is an undocumented change to ESPHome that adds the position_action: functionallity. So, if you can wait till the release of ESPHome 1.5.0 (or don’t mind installing the development branch right now), you can get this out of the box.

It’ll work like the “something you need”, but keep in mind the variable you have to use is fixed and is indeed pos :wink:

2 Likes

Oh wow, you are right. :slight_smile: Just a while ago a stumbled upon this link in some other thread: https://deploy-preview-419–esphome.netlify.com/components/cover/template.html which looks like dev branch of docs, and it is really there.

I am using the addon from addon store in Hass.io, could you please share what it takes to update to the dev branch? Is there a dev build of docker image which will work with Hass.io? Or do I have to install esphome manually and then somehow make it work with Home Assistant?

I am really not sure what you mean by this. You can run esphome on any computer via docker.

I don’t think there is a pre-build dockerfile for hassio that includes the nightly builds. I know the “regular” docker image does include the dev branches.
Just use the dev tag (esphome/esphome:dev) if you want to use that docker image.

1 Like

Oh I see, I thought there is some ingress magic between HA and ESPHome plugin, but I just realized that the Native API works just through an integration directly with the device, which is using ESPHome firmware, so it doesn’t matter where and how the ESPHome “manager” itself is installed, I just might lose the nice menu entry in the HA side panel to open the ESPHome UI directly. And I would have to add it manually and point to to the right port for the ESPHome UI exposed from Docker.

Yeah I don’t use hassio - I just run esphome in docker.

Awesome… Well thank you very much @FF-Fox and @nickrout . Appreciate your help, I am glad this feature is in works. I will try to get the dev branch working.