Using entity_id's in if statement in scripts

I have multiple door / window magnet sensor & cover combinations. I am closing covers at a certin time of day but if the window / door is open the covers might get damaged while closing (they are roller curtains). Because there will be many of these, I want to “scriptize” this and want to pass the magnet & door combinations to this script.

The script below works OK:

alias: Wait until windor or door closed
sequence:
  - if:
      - type: is_open
        condition: device
        device_id: "{{ v_sensor_device_id }}"
        entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_2
        domain: binary_sensor
    then:
      - wait_for_trigger:
          - type: not_opened
            platform: device
            device_id: "{{ v_sensor_device_id }}"
            entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_2
            domain: binary_sensor
      - service: cover.close_cover
        data: {}
        target:
          entity_id: "{{ v_cover_entity_id }}"
    else:
      - service: cover.close_cover
        data: {}
        target:
          entity_id: "{{ v_cover_entity_id }}"
mode: queued
icon: mdi:curtains
max: 10

However, there is one major one minor challenge with it:

  • Major: When I try to change binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_2 to “{{ v_sensor_entity_id }}” I get an error - Message malformed: Entity ID {{ v_sensor_entity_id }} is an invalid entity ID for dictionary value @ data[‘entity_id’]
  • Minor: I cannot remove device_ids even when I provide entity_id

How can I include entity_id as a variable? Also, is it possible to remove device_id?

Don’t use Device conditions or triggers… they do not support templates. Use Template conditions and triggers if you want to template a condition or trigger.

1 Like

Worked like a charm, both questions answered in a few lines - thanks so much. Much tidier script too. Here’s the resulting simple script:

alias: Wait until windor or door closed
sequence:
  - if:
      - condition: template
        value_template: "{{ is_state(v_sensor_entity_id,'on') }}"
    then:
      - wait_for_trigger:
          - platform: template
            value_template: "{{ is_state(v_sensor_entity_id,'off') }}"
      - service: cover.close_cover
        data: {}
        target:
          entity_id: "{{ v_cover_entity_id }}"
    else:
      - service: cover.close_cover
        data: {}
        target:
          entity_id: "{{ v_cover_entity_id }}"
mode: parallel
icon: mdi:curtains
max: 10

called with:

- service: script.wait_until_windor_or_door_closed
  data:
    v_sensor_entity_id: binary_sensor.XXX
    v_cover_entity_id: cover.YYY

from inside an automation.

FYI, the if then else isn’t needed in your script.

alias: Wait until windor or door closed
sequence:
  - wait_template: "{{ is_state(v_sensor_entity_id, 'off') }}"
  - service: cover.close_cover
    target:
      entity_id: "{{ v_cover_entity_id }}"
mode: parallel
icon: mdi:curtains
max: 10
  • If the sensor is initially on it’ll wait until it’s off before closing the cover.
  • If the sensor is initially off it’ll close the cover immediately.
1 Like

so logical, thanks for taking the time to improve. Updated mine.