Script condition help

I have the following script that works except for the condition.

'1533219231046':
  alias: Plant Moisture
  sequence:
  - data:
      message: The moisture is {{states.sensor.calatha_orbifolia_moisture.state}}
        percent.
    service: notify.downstairs_dot
  - condition: state
    entity_id: sensor.calatha_orbifolia_moisture
    state: =< 15
  - data:
      message: You really should water it.
    service: notify.downstairs_dot

I want to be able to trigger a secondary action (another Alexa tts) if the moisture is equal to or below 15 but the condition doesnā€™t seem to be triggering. I wonder if someone can point me in the right direction.

For greater than or equal to, you need to use a value template. If you just want greater than:

'1533219231046':
  alias: Plant Moisture
  sequence:
  - data:
      message: The moisture is {{states.sensor.calatha_orbifolia_moisture.state}}
        percent.
    service: notify.downstairs_dot
  - condition: numeric_state
    entity_id: sensor.calatha_orbifolia_moisture
    above: 15
  - data:
      message: You really should water it.
    service: notify.downstairs_dot

value template for greater than or equal to

'1533219231046':
  alias: Plant Moisture
  sequence:
  - data:
      message: The moisture is {{states.sensor.calatha_orbifolia_moisture.state}}
        percent.
    service: notify.downstairs_dot
  - condition: template
    value_template: "{{ states.sensor.calatha_orbifolia_moisture.state >= 15 }}"
  - data:
      message: You really should water it.
    service: notify.downstairs_dot

FYI all condition types are covered here in the docs:

1 Like

Thanks for that, I was trying to make it work via the HA UI, I know most people prefer to do it yaml and it looks like I need to go the same way.

The automation and script UI needs some love to bring it up to speed. For example duplication of actions/coditions, re-arranging actions and duplication of entire automations/scripts.

Yeah, itā€™s pretty bad. I donā€™t think many people use it. Only beginners usually. I suggest learning yaml. Youā€™ll have headaches at first, but once it clicks it becomes second nature.

1 Like

So I couldnā€™t get your example to work so I read the docs and came up with the following which works great. Notice the delay between one Alexa TTS and the other so they donā€™t cut each other off:

'1533219231046':
  alias: Plant Moisture
  sequence:
  - data:
      message: The moisture is {{states.sensor.calatha_orbifolia_moisture.state}}
        percent.
    service: notify.downstairs_dot
  - below: '15'
    condition: numeric_state
    entity_id: sensor.calatha_orbifolia_moisture
  - delay: 00:00:2
  - data:
      message: You really should think about watering it.
    service: notify.downstairs_dot

Thanks for pointing me in the right direction.

1 Like

Out of interest are you using alexa tts?

Yep, as per here: https://www.reddit.com/r/homeassistant/comments/8s0q66/alexa_text_to_speech_support_for_home_assistant/

Hi all,

same problem with different sensor.

I want to check if cover is under a particular position and in case condition is true set cover position to 100

I tried different configuratione: none worked.

- condition: template
  value_template: "{{ state_attr('cover.tapparella_studio', 'current_position') <= 25 }}"
# - condition: numeric_state
  # entity_id: cover.tapparella_studio
  # below: 25
- service: cover.set_cover_position
  data_template:
    position: 50
    entity_id: cover.tapparella_studio
# - condition: numeric_state
  # entity_id: cover.tapparella_stanza_di_adele
  # below: 25

any help appreciated!

assuming your ā€œcurrent_positionā€ returns something that can be converted to a number (no symbols such as %, etc) then this should work:

value_template: "{{ state_attr('cover.tapparella_studio', 'current_position') | int <= 25 }}"

and you donā€™t need to use ā€œdata_template:ā€ in your action service call. ā€œdata:ā€ will work since you arenā€™t arenā€™t actually using a template in that section.

well I changed based on your post and unfortunately nothing changed.

Hereā€™s the new code:

- condition: template
  value_template: "{{ state_attr('cover.tapparella_studio', 'current_position') | int <= 25 }}"
- service: cover.set_cover_position
  data:
    position: 50
    entity_id: cover.tapparella_studio

the current position returns a number and when the position is under 25 the condition is true.

Whereā€™s the error in the condition?

if the condition is true (as it should be from the template) iā€™m not understanding what you are saying isnā€™t working.

Maybe it would help if you post the entire automation.

Hereā€™s the script (itā€™s a script, not an automation)

script:
  buonanotte:
    sequence:
    - service: automation.turn_on
      entity_id: automation.accendi_luce_disimpegno_se_rilevato_movimento
    - service: automation.turn_on
      entity_id: automation.accendi_luce_corridoio_se_rilevato_movimento
    - service: switch.turn_off
      entity_id: switch.notte_comodino_marco
    - service: switch.turn_off
      entity_id: switch.notte_comodino_irene
    - service: cover.set_cover_position
      data:
        position: 35
        entity_id: cover.tapparella_stanza_da_letto
    - condition: template
      value_template: "{{ state_attr('cover.tapparella_cucina', 'current_position') | int <= 25 }}"
      # value_template: "{{ state_attr('cover.tapparella_cucina', 'current_position') <= 25 }}"
    # - condition: numeric_state
      # entity_id: cover.tapparella_cucina
      # below: 25
    - service: cover.set_cover_position
      data:
        position: 100
        entity_id: cover.tapparella_cucina
    - condition: template
      value_template: "{{ state_attr('cover.tapparella_soggiorno', 'current_position') | int <= 25 }}"
      # value_template: "{{ state_attr('cover.tapparella_soggiorno', 'current_position') <= 25 }}"
    # - condition: numeric_state
      # entity_id: cover.tapparella_soggiorno
      # below: 25
    - service: cover.set_cover_position
      data:
        position: 50
        entity_id: cover.tapparella_soggiorno
    # - condition: template
      # value_template: "{{ state_attr('cover.tapparella_studio', 'current_position') | int <= 25 }}"
      # # value_template: "{{ state_attr('cover.tapparella_studio', 'current_position') <= 25 }}" 
    # - service: light.turn_on
      # entity_id: light.yeelight_mono1_286c07b192d2
    - condition: template
      value_template: "{{ state_attr('cover.tapparella_studio', 'current_position') | int <= 25 }}"
      # value_template: "{{ state_attr('cover.tapparella_studio', 'current_position') <= 25 }}" 
    # - condition: numeric_state
      # entity_id: cover.tapparella_studio
      # below: 25
    - service: cover.set_cover_position
      data:
        position: 50
        entity_id: cover.tapparella_studio
    # # - condition: numeric_state
      # # entity_id: cover.tapparella_stanza_di_adele
      # # below: 25
    - condition: template
      value_template: "{{ state_attr('cover.tapparella_stanza_di_adele', 'current_position') | int <= 25 }}"
      # value_template: "{{ state_attr('cover.tapparella_stanza_di_adele', 'current_position') <= 25 }}"
    - service: cover.set_cover_position
      data:
        position: 50
        entity_id: cover.tapparella_stanza_di_adele

you need to correct the formatting of the post. its really hard to read like that especially one that long.

edited. Sorry.

are you trying to check the state of the cover or an attribute of the cover in the condition?

Yes: condition checks cover position and if below a percentage the script should open the cover.

Then if the condition is the state and not an attribute of the entity then you are using the wrong method.

Change ā€œstate_attrā€ to ā€œstateā€.

Your script contains multiple conditions. I assume you know the script will stop at the first condition that evaluates to false.

    - condition: template
      value_template: "{{ state_attr('cover.tapparella_cucina', 'current_position') | int <= 25 }}"
     ...

    - condition: template
      value_template: "{{ state_attr('cover.tapparella_soggiorno', 'current_position') | int <= 25 }}"
     ...

    - condition: template
      value_template: "{{ state_attr('cover.tapparella_studio', 'current_position') | int <= 25 }}"
     ...

    - condition: template
      value_template: "{{ state_attr('cover.tapparella_stanza_di_adele', 'current_position') | int <= 25 }}"
     ...

So if the value_template for cover.tapparella_soggiorno evaluates to false, the script ends processing there and wonā€™t continue to check the other conditions for cover.tapparella_studio and cover.tapparella_stanza_di_adele.

so what do you suggest to manage it? create different scripts?

Based on your response, Iā€™m going to assume you were not aware of how conditions work in a script. Therefore the answer to your original question is that there was nothing wrong with the conditionā€™s template. The problem was the script never processed it because it halted execution at the previous condition (that had evaluated to false).

Scripts are a sequence of instructions. Script processing stops at the first condition that evaluates to false. Scripts do not have flow-control like ā€˜if this is true then do something else do other thingā€™.

Your script contains a long sequence of checking the position of several covers and moving them if required. I suggest you create separate automations to handle each cover. The script calls each automation. Thatā€™s what youā€™re already doing at the beginning of the script (calling other automations).

Alternately, you can create a python_script to replace parts of your script (perhaps just the parts handling the covers) or even the entire script. A python_script provides more flexibility and control than a script.

1 Like