Use previous status (ignoring unknow status) of an entity to do the action

Hello,

Due to this issue (https://github.com/home-assistant/home-assistant/issues/30112), I cannot use the aqara switch to close/open the cover with the push on the button.

Example:

 - id: '123'
   alias: Ouverture volets rdc
   description: ''
   trigger:
   - event_data:
       click_type: single
       entity_id: binary_sensor.switch_XXXX
     event_type: xiaomi_aqara.click
     platform: event
   condition:
   - condition: or
     conditions:
     - condition: state
       entity_id: cover.XXXX
       state: closed
     - condition: state
       entity_id: cover.XXXX
       state: closed
     - condition: state
       entity_id: cover.XXXX
       state: closed
     - condition: state
       entity_id: cover.XXXX
       state: closed
   action:
   - data:
       entity_id: cover.volets_rdc
     service: cover.open_cover

So before this issue, was no issue because states works (if one of the covers was close => open all the covers with a single push). Once, done, a new single push close all the covers.

Now I have an issue :smiley: The status of entity move to unknow after one minute… Due to that, I cannot use anymore the single push.

My questions:

  1. If I try to duplicate this automation, will generate a loop no?
    Example:
  • Open:
 - id: '123'
   alias: Ouverture volets rdc
   description: ''
   trigger:
   - event_data:
       click_type: single
       entity_id: binary_sensor.switch_XXXX
     event_type: xiaomi_aqara.click
     platform: event
   action:
   - data:
       entity_id: cover.volets_rdc
     service: cover.open_cover
  • Close
 - id: '1234'
   alias: Fermeture volets rdc
   description: ''
   trigger:
   - event_data:
       click_type: single
       entity_id: binary_sensor.switch_XXXX
     event_type: xiaomi_aqara.click
     platform: event
   action:
    - data:
       entity_id: cover.volets_rdc
     service: cover.close_cover

Due to the fact that there are no conditions for both (because no status anymore), how HA will manage it? Segfault?

  1. It’s possible to use a condition on a previous “not unknow” state of the switch.
    Example:
    The current hour is 20h and the switch (cover) is in unknow status, the last good condition was good at 16h30 (cover closed). Based on the fact the last good status was close for this cover, can we use it in a condition to allow to open the cover when we push a single on the button?
    image
    This is like if I want to tell in the condition "so, take the last good status (ignoring unavailable/unknow status & whitelist only open/close) for this entity, if the the last status is closed -> so open the cover.

I hope that it’s clear for you :smiley:

Doesn’t hesitate if you need more informations ^ ^

On the single click event just call this service:

action:
  service: cover.toggle
  entity_id: cover.volets_rdc

If they are open they will close and vice-versa.

Thank you @tom_l,

But the issue is that we don’t know the status (it’s switch to unknow after one minutes)

Edit: yes it works only one time (cover group keep in open mode every time due to unknow state of entities :()

Did you try it?

@tom_l yes, cf my edit, I need like an exception to use previous status :frowning:
I can close the cover

Ok then. I guess one way forward is to create template binary sensors (for each cover) that ignore the unknown state and only remember open or closed.

binary_sensor:
- platform: template
  sensors:
    cover.xxxx:
      friendly_name: 'Cover XXXX'
      device_class: opening
      entity_id: cover.xxxx
      value_template: >
        {% if states('cover.xxxx') == 'open' %}
          {{ true }}
        {% elif states('cover.xxxx') == 'closed' %}
          {{ false }}
        {% else %}
          {{ true if states('binary_sensor.cover.xxxx') == 'on' else false}}
        {% end if %}
    cover.yyyy:
      friendly_name: 'Cover YYYY'
      device_class: opening
      entity_id: cover.yyyy
      value_template: >
        {% if states('cover.yyyy') == 'open' %}
          {{ true }}
        {% elif states('cover.yyyy') == 'closed' %}
          {{ false }}
        {% else %}
          {{ true if states('binary_sensor.cover.yyyy') == 'on' else false }}
        {% end if %}
     cover.zzzz
       etc...

That last else statement is equivalent to “stay the same if not open or closed”.

Then use these binary sensors (binary_sensor.cover_xxxx) in your automations as they will not have the unknown state. Be aware though, even though they display open and closed in the front end you must use the states 'on' and 'off', respectively, in your automations. e.g.

 - id: '123'
   alias: Ouverture volets rdc
   description: ''
   trigger:
   - event_data:
       click_type: single
       entity_id: binary_sensor.switch_XXXX
     event_type: xiaomi_aqara.click
     platform: event
   condition:
   - condition: or
     conditions:
     - condition: state
       entity_id: binary_sensor.cover.xxxx
       state: 'off'
     - condition: state
       entity_id: binary_sensor.cover.yyyy
       state: 'off'
etc...

Thank you very much @tom_l, it works!