How to template a 'covering' cover

Hi,

trying to create a binary_sensor for a cover to indicate it is currently changing position.
Im stuck though…
this is a the template for current position:

{{state_attr('cover.raamverduistering_stookhok','current_position')}}

I think it would be needed to compare current position with position of now() - (now()+1).
or could I simply check for last_changed (which changes on all attributes changes too)

{{(now() - states.cover.raamverduistering_stookhok.last_changed).total_seconds() < 1 }}

thing is, it seems last_changed doesn’t work on this cover?

if stopped, and again changing position, the last changed remains the same… it only changes when state changes from open to closed, not even vice versa ??

Hey Marius, can you provide a little more detail as to which cover(s) you’re using?
Most of the covers have a “closing” and “opening” state while moving, can you check if yours does as well? The Icon for the cover will change in the UI to an arrow pointing up-/downwards.

thanks, yes, I would have expected that, indeed, but apparently, on the Ikea covers, that is not the case:

only thing changing is the current_position.

and as said even last_changed only changes on state change open to closed

Yes, I also have the IKEA covers, do you use ZHA, their gateway or Deconz?
I’m using ZHA for them - don’t know what you’re trying to achieve, but I set up an automation that triggers an update_entity service call on the cover whenever its state gets updated, so it won’t be stuck at “closing”.

I use the IKEA hub.

I’m simply trying to have the frontend show the cover is moving…

Checked out the other IKEA blind on the Tradfri Gateway, you’re right, they don’t report opening/closing.
In that case I’d create two “Trend” sensors: https://www.home-assistant.io/integrations/trend
One for opening, one for closing. Have the icon_template (not icon) reflect the value of the new binary sensors :slight_smile:

1 Like

Trend binary sensor would be the way to go. It may lag a bit depending on your position update rate as it requires two readings to start calculating.

1 Like

Though the Gateway reports the position pretty quickly, so shouldn’t be too big of a concern :slight_smile:

1 Like

you mean something like this:

binary_sensor:
  - platform: trend
# open : current_position == 100%
    sensors:
      raamverduistering_stookhok_opening:
        friendly_name: Stookhok opening
        entity_id: cover.raamverduistering_stookhok
        attribute: current_position
        device_class: opening

      raamverduistering_stookhok_closing:
        friendly_name: Stookhok closing
        entity_id: cover.raamverduistering_stookhok
        attribute: current_position
        invert: true
        device_class: opening

  - platform: template
    sensors:
      raamverduistering_stookhok_moving:
        friendly_name: Stookhok moving
        value_template: >
          {{is_state('binary_sensor.raamverduistering_stookhok_opening','on') or
            is_state('binary_sensor.raamverduistering_stookhok_closing','on')}}
        device_class: moving

thanks! will probably do the trick this needs some more attention, because both binaries show off all the time, and dont respond at all (wait, restart… had the wrong entity_id…)
Ok, so the entity_id helped :wink: still not perfect though, because it only works when moving, and doesnt stop when the cover is either stopped manually, or because it reached its objective postition.
Do I need to set anything else in the sensor configs? Maybe the sample_duration? Nope, tried that, results are still the same: only starting a motion changes the binary trend sensor… min_gradient is too unreliable to use.

Even if this did the trick though, I feel we would need the attribute in the cover entity_id directly. Hope this can be PR or FR’d to the Ikea integration, will have a look to contact the author.

Also, I don’t understand the absence of response checking the last_changed, and more than that, it is only changing in 1 direction… must be a bug?

small update for the night:
this automation does work triggering on state (which is also triggered by the attributes changes, as I hoped in my first post)

automation:
  alias: Cover is moving
  trigger:
    platform: state
    entity_id: cover.raamverduistering_stookhok
  action:
    service: notify.system
    data_template:
      title: Cover is moving
      message: current position is {{trigger.to_state.attributes.current_position}}

It even triggers more than once per second so it seems…:

had to get back to my first thought, an automation triggering on the state (which also triggers on each attribute change) of the cover. Use that in a template for a binary sensor… very convoluted, especially since the automation shouldn’t do anything, only trigger…

had to use an empty script, with a delay of a second (and use the upcoming mode: parallel I have been testing lately, so dont yet try this before it is released) because without the delay in the script, the automation wouldn’t set it’s last_triggered attribute:

automation:
  alias: Cover stookhok is moving
  trigger:
    platform: state
    entity_id: cover.raamverduistering_stookhok
  action:
    service: script.trigger_only

and script:

trigger_only:
  alias: Trigger only
  mode: parallel
  sequence:
    delay:
      seconds: 1

to create a binary_sensor:

  - platform: template
    sensors:
      raamverduistering_stookhok_moving:
        friendly_name: Stookhok moving
        entity_id: sensor.time,  automation.cover_stookhok_is_moving
        value_template: >
          {{(now() - state_attr('automation.cover_stookhok_is_moving','last_triggered'))
             .total_seconds() < 2}}

so I can now customize my cover:

homeassistant:
  customize:
    cover.raamverduistering_stookhok:
      templates:
        icon: >
          if (entities['binary_sensor.raamverduistering_stookhok_moving'].state == 'on') return 'mdi:animation';
          if (state == 'open' && attributes.current_position == 100) return 'mdi:window-shutter-open';
          if (state == 'closed') return 'mdi:window-shutter';
          else return 'mdi:window-shutter-alert';
        icon_color: >
          if (entities['binary_sensor.raamverduistering_stookhok_moving'].state == 'on') return 'red';
          if (state == 'open' && attributes.current_position == 100) return 'gold';
          if (state == 'closed') return 'darkblue';
          else return 'green';

:slight_smile:

consuming all of this, made me decide to file a bug report on the missing attributes ‘opening’ and ‘closing’ after all. Really feels like it should have been there in the first place. https://github.com/home-assistant/core/issues/37223

having consumed very useful advise from Phil, (making this really his solution, but cant cross thread a solution mark I think?) the setup has advanced, please see Automation issue: why isn't this wait_template waiting to avoid crossposting the same code.

Finally I have found a way to not only indicate the covers are moving, but also in which direction :wink:

Now let’s hope the FR will at all be considered, cause this should really all be unnecessary, and in the core code for the Ikea Tradfri covers…