Alexa Intent - State of Switch

Hey Everyone.
I’m trying to write an intent for alexa to figure out if a switch is on, or off. I’ve tried using ‘state’, ‘is_state’, and a few others but I keep getting errors. Has anyone done this?

Here’s the code I’ve been messing with:

TestIntent:
  speech:
    type: plaintext
    text: >
      {% if state('switch.front_dooropened', 'on') %}
        The front door is open.
      {% elif state('switch.front_dooropened', 'off') %}
        The front door is closed.
      {% endif %}

Any suggestions, or help would be greatly appreciated.

Thanks!

I am using something like this:

    QueryGarageDoorIntent:
      speech:
	type: plaintext
        text: >
	  {% if is_state('switch.garage_door', 'on') %}
            the garage door is open
          {% else %}
            the garage door is closed
          {% endif %}

(My garage door is setup to look like a switch, not a cover)

I’m not 100% sure, but I think you want to replace

{% if state('switch...

with

{% if is_state('switch...

Check the templating help here for a lot of examples of how to reference states and attributes inside templates.

Thanks for the response guys, that’s what I was looking for!
So if I use this, it Works:

TestIntent:
  speech:
    type: plaintext
    text: >
      {% if is_state('switch.front_dooropened', 'on') %}
        The front door is open.
      {% else %}
        The front door is closed.
      {% endif %}

Also, if i use this, it Works!

TestIntent:
  speech:
    type: plaintext
    text: >
      {% if is_state('switch.front_dooropened', 'on') %}
        The front door is open.
      {% elif is_state('switch.front_dooropened', 'off') %}
        The front door is closed.
      {% endif %}

i understand that I don’t need to use the ‘elif’ but it will help me make more comprehensive loops.
Thanks again!

1 Like