Scripts with multiple conditions

Hi,

I’ve been struggling with this for a little too long. Thought I’d put it to the community as I can’t string it together from the documentation…

The error message showing in the log is
2017-10-15 05:34:29 ERROR (MainThread) [homeassistant.config] Invalid config for [script]: [condition] is an invalid option for [script]. Check: script->script->timed_light->condition. (See /config/configuration.yaml, line 144). Please check the docs at https://home-assistant.io/components/script/

Any pointers/thoughts will be greatly received…

The is a section from my scripts.yaml:

  timed_light:
    alias: "Turn entrance light on"
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: switch.entrance_light
          state: off
        - condition: sun
          state: below_horizon
    sequence:
    - service: script.turn_off
      data:
        entity_id: script.light_timer
    - service: switch.turn_on
      data:
        entity_id: switch.entrance_light
    - service: script.turn_on
      data:
        entity_id: script.light_timer

  light_timer:
    alias: "Turn entrance light off after"
    sequence:
      - delay:
          minutes: 2
      - service: switch.turn_off
        data:
          entity_id: switch.entrance_light

I’m getting the following

You have to include conditions into the sequence part:

timed_light:
    alias: "Turn entrance light on"
    sequence:
      - condition:
          condition: and
          conditions:
            - condition: state
              entity_id: switch.entrance_light
              state: off
            - condition: sun
              state: below_horizon
      - service: script.turn_off
        data:
          entity_id: script.light_timer
      - service: switch.turn_on
        data:
          entity_id: switch.entrance_light
    -   service: script.turn_on
        data:
          entity_id: script.light_timer

Trying this returns this error message:

2017-10-15 06:13:32 ERROR (MainThread) [homeassistant.config]
Invalid config for [script]: [condition] is an invalid option for [script].
Check: script->script->timed_light->sequence->0->condition.
(See /config/configuration.yaml, line 144).
Please check the docs at https://home-assistant.io/components/script/

  timed_light:
    alias: "Turn entrance light on"
    sequence:
      - condition:
          condition: and
          conditions:
            - condition: state
              entity_id: switch.entrance_light
              state: off
            - condition: sun
              state: above_horizon
      - service: script.turn_off
        data:
          entity_id: script.light_timer
      - service: switch.turn_on
        data:
          entity_id: switch.entrance_light
      - service: script.turn_on
        data:
          entity_id: script.light_timer

  light_timer:
    alias: "Turn entrance light off after"
    sequence:
      - delay:
          minutes: 2
      - service: switch.turn_off
        data:
          entity_id: switch.entrance_light

I do mine with the conditions in the automation rather than in the scripts…

 - alias: 'Turn on kitchen light when there is movement and the sun is down'
   trigger:
     - platform: state
       entity_id: switch.pikitchenpir, switch.tellkitchenpir
       to: 'on'
   condition:
     condition: and
     conditions:
       - condition: state
         entity_id: sun.sun
         state: 'down'
       - condition: state
         entity_id: input_boolean.detect_motion
         state: 'on'
   action:
      - service: script.turn_on
        entity_id: script.timed_kitchen_lamp

Then in script.yaml

  timed_kitchen_lamp:
    alias: "Turn on kitchen light and set timer"
    sequence:
      # Cancel ev. old timers
      - service: script.turn_off
        data:
          entity_id: script.timer_kitchen_lamp_off
      - service: light.turn_on
        entity_id: light.kitchen_ceiling_light
        data:
          brightness: 180
      # Set new timer
      - service: script.turn_on
        data:
          entity_id: script.timer_kitchen_lamp_off

  timer_kitchen_lamp_off:
    alias: "Turn off kitchen light after 15 minutes"
    sequence:
        - delay: 00:15:00
        - service: light.turn_off
          data:
            entity_id: light.kitchen_ceiling_light            

Not sure if that helps :slight_smile:

Hi keithh666

Moving the conditions to the automation.yaml, in the format you suggested has worked.
Thank you for taking the time to advise.

Regards
Bungle

1 Like

I had trouble getting conditions to work inside a script also. I’m adding the configuration that worked for me to this thread to hopefully save someone else all the trouble I had.

Unlike your problem - using ‘and’ conditions - I wanted to use multiple separate conditions in my sequence of actions - and have the script stop when one of them was true. Keep in mind that this was just a script to test for the correct syntax as opposed to one that actually does anything useful.

This is what finally worked:

test_conditions:
  sequence:
  - service: light.turn_on
    entity_id: light.office_light_north
  - condition: state
    entity_id: light.lower_stairway
    state: 'on'
  - service: light.turn_on
    entity_id: light.office_light_south
  - condition: state
    entity_id: light.upper_stairway
    state: 'on'
  - service: light.turn_on
    entity_id: light.upstairs_hallway

The script runs when activated and turns on the North Office Light.
If the lower stairway light is off, the script ends. If it’s on, the script continues and turns on the South Office Light.
If the upper stairway light is off, the script ends. If it’s on, the script continues and turns on the Upstairs Hallway light.

Most of the trouble I had making this work was with the indents. There didn’t seem to be any examples of multiple conditions in a script - and those posts that I found asking about it (like this one) seemed to end with some advice to use automations instead of scripts. Anyhow - hope this example helps someone. It’s tested and working with HomeAssistant 0.59.2

3 Likes

I was having just this problem and found this thread while trying to solve it. I eventually found the solution so thought I’d post it back here to help anyone else struggling with this.
Despite what you might think looking at the documentation on ‘condition’, in a script it appears you can put the logical operator on the line with the first ‘condition’ statement. ie

    sequence:
  - condition: and
      conditions:
        - condition: state
          entity_id: switch.entrance_light
          state: off
        - condition: sun
          state: above_horizon

That should work just fine. (But you also need to put quotes round the state values - e.g. state: ‘off’)

Hi Prejury, I am trying to run a simple script similar to yours. have two conditions and if they are both true, turn golf light on. I have been trying for a while and couldn’t get it working. just wonder if you can show me your working script? it will be much appreciated if you can. My script as shown below.

golf_light_double_conditions:
  alias: "Test golf light"
  sequence:
  condition:
    condition: and
    conditions:
      - condition: state
        entiy_id: binary_sensor.living_tv_status
        state: 'off'
      - condition: state
        entity-id: input_boolean.living_tv_power_bool
        state: 'on'
      - service: switch.turn_on
        entity_id: switch_golf

You have too many ‘condition’ s
Here’s some code that works for me:

    alias: Notify Phil   
    sequence:
      - condition: or
        conditions:
          - condition: state
            entity_id: device_tracker.phils_phone
            state: 'home'
          - condition: and
            conditions:             
            - condition: state
              entity_id: input_boolean.notifyphil
              state: 'on'
            - condition: state
              entity_id: device_tracker.phils_phone
              state: 'not_home'
      - service: notify.philpo
        data_template:
          title: "{{ title|default('Home Assistant') }}"
          message: "{{ message }}"
          data:
            priority: "{{ priority|default('0') }}"
5 Likes

thank u very much, i will try it out tonight

I am 100% certain that you don’t have any entities listed as above.

It would most likely be ‘switch.golf’

Hi perjury, what is the (- condition: or) for? from what I understand by reading the code, if input_boolean.notifyphil is ON AND phil is not home. Send phil a message. is that correct? you were correct with switch.golf as well.

If you guys just want to and things, you don’t need to explicitly call out and. Conditions default to and. You only need to call out or.

Also, you had a bunch of formatting issues and typos in your code. This should work with what you originally wrote.

golf_light_double_conditions:
  alias: "Test golf light"
  sequence:
    - condition:
      - condition: state
        entiy_id: binary_sensor.living_tv_status
        state: 'off'
      - condition: state
        entity_id: input_boolean.living_tv_power_bool
        state: 'on'
    - service: switch.turn_on
      entity_id: switch.golf

if conditions are confusing too, you could always use value_templates. IMO they are easier to read.

golf_light_double_conditions:
  alias: "Test golf light"
  sequence:
    - condition: template
      value_template: "{{ is_state('binary_sensor.living_tv_status','off') and is_state('input_boolean.living_tv_power_bool','on') }}"
    - service: switch.turn_on
      entity_id: switch.golf

thank you and got it all working with your suggestion of less condition; code as shown below.

correcting_living_tv_status1:
  alias: 'Correct Living TV Status To Off'
  sequence: 
  - condition: state
    entity_id: binary_sensor.living_tv_status
    state: 'off'  
  - condition: state
    entity_id: input_boolean.living_tv_power_bool
    state: 'on'   
  - service: input_boolean.turn_off
    entity_id: input_boolean.living_tv_power_bool
correcting_living_tv_status2:
  alias: 'Correct Living TV Status To On'
  sequence: 
  - condition: state
    entity_id: binary_sensor.living_tv_status
    state: 'on'  
  - condition: state
    entity_id: input_boolean.living_tv_power_bool
    state: 'off'   
  - service: input_boolean.turn_on
    entity_id: input_boolean.living_tv_power_bool

@petro: I try to run a script with two “or” condition. I thought, i can adapt your solution (from “and” too “or”) but it doesn’t work.

scripts:
  stop_rollladen_tuere:
    alias: 'stop rollladen_tuere'
    sequence:
    - condition: template
      value_template: "{{ is_state('cover.rollladen_tuere','unten') or is_state('cover.rollladen_tuere','oben') }}"
    - service: cover.stop_cover
      data:
        entity_id: cover.rollladen_tuere # cover to activate
    - delay: 00:00:01
    - service: python_script.set_state
      data_template:
        entity_id: cover.rollladen_tuere_cover # cover "copy" for lovelace
        state: gestoppt
    - service: python_script.set_state
      data_template:
        entity_id: cover.rollladen_tuere_cover
        icon: mdi:square

Do you can help me?
Thanks (again)

cover’s don’t have translated states. It will be open or closed. Verify the state of the cover in developer tools -> states page.

I overwrite the state of the cover with a phyton script. This work well.

The only problem i have: the script shoud just run, if one of two state are “true”.

   - condition: template
      value_template: "{{ is_state('cover.rollladen_tuere','unten') or is_state('cover.rollladen_tuere','oben') }}"

Cheers

so what’s not working then? Or should work just fine.