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