Script not working as expected. Please help

I’m guessing I’m missing something elementary with my script. Right now most of my script works. When I ask Alexa, “Are the garage doors open?”, She replies either Left Garage Door is open or Right Garage Door is open correctly. The problem is, I want her to reply “both doors are closed” if both doors are closed, but she does not. Below is the script. Any help or thoughts are much appreciated…

Please know that I know the alexa tts isn’t really supported, but I have tried other things like turn on light instead of the alexa tts if both doors are closed and that isn’t working either. That part of the code if both doors are closed (off) isn’t executing.

'1539301820073':
  alias: Check Garage Door
  sequence:
  - condition: state
    entity_id: binary_sensor.0x00158d000243672e
    state: 'on'
  - alias: ''
    data:
      entity_id: media_player.echo_plus
      message: left garage door is open
    service: media_player.alexa_tts
  - condition: state
    entity_id: binary_sensor.0x00158d000232c071
    state: 'on'
  - data:
      entity_id: media_player.echo_plus
      message: right garage door is open
    service: media_player.alexa_tts
  - condition: state
    entity_id: binary_sensor.0x00158d000243672e
    state: 'off'
  - condition: state
    entity_id: binary_sensor.0x00158d000232c071
    state: 'off'
  - data:
      entity_id: media_player.echo_plus
      message: both doors are closed
    service: media_player.alexa_tts

It seems like the script is catching either the left or right and never getting the chance to check the last condition (both doors open/closed).

This is just a guess, but have you tried putting

  - condition: state
    entity_id: binary_sensor.0x00158d000243672e
    state: 'off'
  - condition: state
    entity_id: binary_sensor.0x00158d000232c071
    state: 'off'
  - data:
      entity_id: media_player.echo_plus
      message: both doors are closed
    service: media_player.alexa_tts

at the top?

If a condition in a script evaluates to false the entire rest of the script is aborted.

Just tried it. The script does work if both doors are closed but renders the rest of the script never executes if one of the doors are open.

Apparently it is as @pnbrucker pointed out, once a condition is false, the script is exited. Thanks

Thanks. I see that you’re right. I guess I need to come up with a work around.

Ahh, I didn’t realise that either. Every day is a school day :slight_smile:

Just an off the wall thought; Two separate scripts with the results put in a common group?

I’m not sure I follow what you mean.

Have a separate script for each door. Put the sensors for both doors into a group. Check the entities with another and/or type script:

{% if is_state("binary_sensor.door1", "on") and
      is_state("binary_sensor.door2", "on") -%}
  Both doors are closed
{%- else -%}
  Door 1 is {{ states("binary_sensor.door1") }}
  Door 2 is {{ states("binary_sensor.door2") }}
{%- endif %}

I think I know what you’re suggesting. I will experiment. Thanks.

I tried the following, but I have the syntax wrong somehow.

check_garage_door:
  alias: 'Check Garage Door'
  sequence:
  - data_template:
      entity_id: media_player.echo_plus
      message: >
        {% if states.binary_sensor.0x00158d000243672e.state == 'on' %}
          The left garage door is open
        {% elif states.sensor.0x00158d000232c071.state == 'on' %}
          The right garage door is open
        {% else %}
          both doors are closed
        {% endif %}
    service: media_player.alexa_tts
check_garage_door:
  alias: 'Check Garage Door'
  sequence:
    service: media_player.alexa_tts
    entity_id: media_player.echo_plus
    data_template:
      message: >
        {% if states.binary_sensor.0x00158d000243672e.state == 'on' and states.binary_sensor.0x00158d000243672e.state == 'on'  %}
          Both garage doors are open
        {% elif states.binary_sensor.0x00158d000243672e.state == 'on' %}
          The left garage door is open
        {% elif states.sensor.0x00158d000232c071.state == 'on' %}
          The right garage door is open
        {% else %}
          both doors are closed
        {% endif %}

Thanks. I tried, but I get the following error when I attempt configuration validation:

Invalid config for [script]: invalid template (TemplateSyntaxError: expected token 'end of statement block', got 'x00158d000243672e') for dictionary value @ data['script']['check_garage_door']['sequence'][0]['data_template']['message']. Got None. (See /config/configuration.yaml, line 311). Please check the docs at https://home-assistant.io/components/script/

I just finished commenting on another problem just like this. You cant use entity ids starting with numbers in templates without a bit of fiddling (see note at the bottom of this section: https://www.home-assistant.io/docs/configuration/templating/#home-assistant-template-extensions ). So you will need this:

check_garage_door:
  alias: 'Check Garage Door'
  sequence:
    service: media_player.alexa_tts
    entity_id: media_player.echo_plus
    data_template:
      message: >
        {% if states('binary_sensor['0x00158d000243672e']', 'on') and states('binary_sensor['0x00158d000243672e']', 'on') %}
          Both garage doors are open
        {% elif states('binary_sensor['0x00158d000243672e']', 'on') %}
          The left garage door is open
        {% elif states('binary_sensor['0x00158d000232c071']', 'on') %}
          The right garage door is open
        {% else %}
          both doors are closed
        {% endif %}

EDIT: I stuffed up the formatting. Have fixed it above now.

Thanks Tom, That got rid of the configuration validation errors. Unfortunately no combination of garage doors open and/or closed yield a message via alexa_tts. Every time I try to execute the script (even after a reboot), I get this in the log:

2018-10-12 00:04:38 WARNING (MainThread) [homeassistant.components.script] Script script.check_garage_door already running.

Hmm. Could be due to this issue:

https://github.com/home-assistant/home-assistant/issues/12656

Try renaming the script and reloading.

Didn’t see your edit. Retried the code and I’m getting a syntax error on config validation:

Invalid config for [script]: invalid template (TemplateSyntaxError: expected token ',', got 'integer') for dictionary value @ data['script']['check_garage_door']['sequence'][0]['data_template']['message']. Got None. (See /config/configuration.yaml, line 311). Please check the docs at https://home-assistant.io/components/script/

I’ve never had to use the square bracket notation so am flying a bit blind here.

What do the following four templates return in your dev tools template editor:

{{ states('binary_sensor['0x00158d000243672e']' ) }}

{{ states('binary_sensor[0x00158d000243672e]' ) }}

{{ states('binary_sensor.['0x00158d000243672e']' ) }}

{{ states('binary_sensor.[0x00158d000243672e]' ) }}

Ah. I think I see the problem. Though I’m not sure how to fix it. Maybe try this in the template editor:

{{ states("binary_sensor['0x00158d000243672e']" ) }}

The 2nd, 4th, and 5th return unknown
The 1st and 3rd return errors

How about:

{{ states.binary_sensor['0x00158d000243672e'].state }}