Alexa Intent for turn lights On/Off with attributes (brightness, color, etc)

Hi,

I’m new to the HA, only poked around for like for 2 weeks. I’m configuring one Intent to be used from Alexa. I’m missing something that seems trivial.

To simplify I’ll only use one light.

I first tried this code:

TurnGoLightIntent:
  action:
    service_template: >
      light.turn_{{ OnOff }}
    data_template:
      entity_id: light.go
      brightness: >
        {% if Brightness is defined %}
          {{Brightness}}
        {% else %}
          255
        {% endif %}

This will work when {{OnOff} is On but if I try to turn off the light it will say “Invalid slot information received for this intent.”. From my research, I’m getting that because of the light.turn_off service doesn’t have the brightness data.

So I split the service template with scripts, something like this:

TurnGoLightIntent:
  action:
    service_template: >
      {% if OnOff == "On" %}
        script.light_go_on
      {% else %}
        script.light_go_off
      {% endif %}

Now I can turn off the light but when I try to turn on it goes always to the “default” value (the else part). The {{Brightness}} variable isn’t set in the script:

light_go_on:
  sequence:
    - service: light.turn_on
      data_template:
        entity_id: light.innr
        brightness: >
          {% if Brightness is defined %}
            {{Brightness}}
          {% else %}
            255
          {% endif %}

Does anyone have a solution to this?

  1. A solution without using scripts making a conditional around the data that doesn’t work with some service. In this case, the brightness of the service light.turn_off.
  2. A solution that passes the Alexa intents variables values to the scripts. In this case, {{Brightness}}

I have also another issue, if I don’t put the default value 255 it will also give the error when the command doesn’t go with a brightness value. I would like to when I don’t specify a brightness it turns the light with the previous brightness (without the brightness data it works like that). I’ve tried using something like this: states.light.go.attributes.brightness, but doesn’t work.

Thanks,

This isn’t possible unless you move to toggle, but then you won’t be able to set brightness.

I mean, you shouldn’t have to do any of these scripts. If you are using haaska, the light should just work with ‘turn on go’ or ‘turn off go’ if 'go is the name.

Anyways to make this method work, you need to use variables to pass to scripts. Check out the variable passing below. The scripts will work with any light, not just light.go. So you can make as many intents as you want. Just remember, scripts can only execute one at a time so if you use this with multiple lights it may be slow.

on script:

light_on:
  sequence:
    - service: light.turn_on
      data_template:
        entity_id: "{{ light_entity_id }}"
        brightness: "{{ light_brightness }}"

off script

light_off:
  sequence:
    - service: light.turn_off
      data_template:
        entity_id: "{{ light_entity_id }}"

intent

TurnGoLightIntent:
  action:
    service_template: >
      script.light_{{ OnOff | lower }}
    data_template:
      light_entity_id: light.go
      light_brightness: >
        {% if Brightness is defined %}
          {{ Brightness }}
        {% else %}
          255
        {% endif %}
1 Like

Thanks petro! I knew that should be something trivial. I was missing how to pass the data to the scripts.

I’m unable to test it right away but I’m sure it’s what I wanted and this knowledge will come handy in the future.

Hum, like I said newb here :joy:. I only knew about emulated_hue that I couldn’t make it work with a 3rd generation echo dot. More things to read about.

Well, it’s been a while since i’ve worked on haaska. I was under the impression that someone streamlined the on/off for haaska. I could be completely wrong though.