Question about script

I have the following script

go_to_bed:
  alias: Goodnight
  icon: mdi:bed-empty
  sequence:
  - service: automation.trigger
    entity_id:
    - automation.set_alarm_at_night  
  - service: light.turn_on
    data:
      entity_id: light.bedside_lamp
  
  - service: cover.close_cover
    data:
      entity_id:
      - cover.50758014840d8e91f4fd #window1
  - delay: '00:00:40'
  
  - service: cover.close_cover
    data:
      entity_id:
      - cover.50758014840d8e91f036 #window
  - delay: '00:00:10'
  
  - service: cover.close_cover
    data:
       entity_id:
       - cover.50758014840d8e91eaa2 #bedroom
  - delay: '00:00:15'
  
  - service: cover.close_cover
    data:
      entity_id:
       - cover.50758014840d8e918632 #kid
  - delay: '00:00:02'

and I would like to add a condition to close each cover IF it’s state is “open” or “unknown”
something like this

condition:
    condition: or
    conditions:
      - condition: state
        entity_id: cover.50758014840d8e918614
        state: "unknown"
      - condition: state
        entity_id: cover.50758014840d8e918614
        state: "open"

Will this work? Because if I understand right if one cover is closed (state “closed”) it will not continue to execute the rest of the script.

While executing a script you can add a condition to stop further execution. 
When a condition does not return true, the script will stop executing. 

Is there an alternative way to add the condition?

You could make a separate script with the condition for each cover, then in your goodnight script call the 4 cover scripts.

1 Like

Thanks I will try it

Or one script that takes a variable:

go_to_bed:
  alias: Goodnight
  icon: mdi:bed-empty
  sequence:
  - service: automation.trigger
    entity_id:
    - automation.set_alarm_at_night  
  - service: light.turn_on
    data:
      entity_id: light.bedside_lamp

  - service: script.close_cover
    data:
      entity_id: cover.50758014840d8e91f4fd #window1
  - delay: '00:00:40'
  
  - service: script.close_cover
    data:
      entity_id: cover.50758014840d8e91f036 #window
  - delay: '00:00:10'
  
  - service: script.close_cover
    data:
      entity_id: cover.50758014840d8e91eaa2 #bedroom
  - delay: '00:00:15'
  
  - service: script.close_cover
    data:
      entity_id: cover.50758014840d8e918632 #kid
  - delay: '00:00:02'

close_cover:
  sequence:
  - condition: template
    value_template: "{{ states(entity_id) in ['open', 'unknown'] }}"
  - service: cover.close_cover
    data_template:
      entity_id: "{{ entity_id }}"
1 Like

That’s a lot better! I will try it and let you know. As I can see I just have to add the new script and change service. I hope that it will work.
Thanks

Just run the script. Something is wrong so none of the covers worked.if you see something please let me know.

Can you post the YAML code exactly as you have it?

Do you see any warnings or errors in the log?

here it is. in the end I am trying to get a notification to my phone if something is still open-on but although it is working in the dev tools it is not working in the script yet

go_to_bed:
  alias: Goodnight
  icon: mdi:bed-empty
  sequence:
  - service: automation.trigger
    entity_id:
    - automation.set_alarm_at_night  
  - service: light.turn_on
    data:
      entity_id: light.bedside_lamp
  
  - service: script.close_cover
    data:
      entity_id:
      - cover.50758014840d8e91f4fd #window1
  - delay: '00:00:40'
  
  - service: script.close_cover
    data:
      entity_id:
      - cover.50758014840d8e91f036 #window
  - delay: '00:00:10'
  
  - service: script.close_cover
    data:
       entity_id:
       - cover.50758014840d8e91eaa2 #bedroom
  - delay: '00:00:15'
  
  - service: script.close_cover
    data:
      entity_id:
       - cover.50758014840d8e918632 #kid
  - delay: '00:00:02'
  
  - service: homeassistant.turn_off
    data:
      entity_id:
      - media_player.lg_tv_webos_2
      - climate.living_room_ac
      #- delay: '00:00:02'
      - switch.book
      - switch.book_and_door_book_2
  - delay: '00:00:02'
  - service: homeassistant.turn_off
    data:
      entity_id:
      #- delay: '00:00:02'
      - light.77626641ecfabc96e072
      - light.77626641ecfabc976395
      #- delay: '00:00:02'
      - switch.k1_kitchen
      #- delay: '00:00:02'
      - switch.balcony_main_switch
      - switch.balcony_balcony_1
      - switch.balcony_balcony_2
      - switch.kid_balcony
      - switch.bedroom_balcony
      - switch.trapezaria
      - switch.k1_kitchen_spot
      - switch.book_and_door_door
  - delay: '00:00:02'
  - service: homeassistant.turn_off
    data:
      entity_id:      
      #- delay: '00:00:02'
      - switch.sonoff_1000a05283
      - switch.sonoff_10009bd11e
      #- delay: '00:00:02'
      - switch.sonoff_10001bb5c7
#  - service: notify.pushbullet_notification
#    data: 
#      title: 'What is on'
#      message:
#      value_template: >
#        {{ states['cover'] | selectattr('state','eq', 'open') | map(attribute='name') | list | join(', ') }}
#        {{ states['cover'] | selectattr('state','eq', 'unknown') | map(attribute='name') | list | join(', ') }}
#        {{ states['switch'] | selectattr('state','eq', 'on') | map(attribute='name') | list | join(', ') }}
#        {{ states['light'] | selectattr('state','eq', 'on') | map(attribute='name') | list | join(', ') }}
      
##################################################################
close_cover:
  sequence:
  - condition: template
    value_template: "{{ states(entity_id) in ['open', 'unknown'] }}"
  - service: cover.close_cover
    data_template:
      entity_id: "{{ entity_id }}"

You didn’t do what I suggested. Rather, you passed the entity_id into the new script as a list, not as a single entity_id. It won’t work that way. This:

  - service: script.close_cover
    data:
      entity_id:
      - cover.50758014840d8e91f4fd #window1

is not the same as this:

  - service: script.close_cover
    data:
      entity_id: cover.50758014840d8e91f4fd #window1

I didn’t even noticed, that change. I just spotted the - service: script.close_cover.
Thanks. I am 99.99% that it will work this evening. will report back if it ok as a solution