Questions regarding non-documented usage of template switches

I’m trying to create template switches that can control the brightness of a light. I need to do this because it is for API.AI integration that converts speech to text and is then matched via templating (example below).

The current flow is like this:
Google Home voice recognition -> HASS action triggered with recognized entities (room, on/off, brightness, etc)

Here is the API.AI configuration that will illustrate why I need to use the template switch platform rather than the light platform:

  light.onoff:
      speech: Turning {{ room }} lights {{ on_off }}
      action:
        - service_template: >
            {%- if on_off == "on" -%}
              switch.turn_on
            {%- else -%}
              switch.turn_off
            {%- endif -%}
          data_template:
            entity_id: "switch.light_{{ room | replace(' ', '_') }}"

So as you can see, I have to match exactly the variable room which comes from API.AI with my entity_id ("switch.light_{{ room | replace(' ', '_') }}"). Example: If I say “Turn off the living room lights”, “living room” gets sent to HASS in the room variable and is matched with my template switch switch.light_living_room. Since so many lights have generated garbage on their entity ids (my actually living room light entity_id is light.recessed_lights_level_9_0), I need to use the template switch to effectively rename the entity.

switch:
  - platform: template
    switches:
      light_living_room:
        value_template: "{{ is_state('light.recessed_lights_level_9_0', 'on') }}"
        turn_on:
          service: light.turn_on
          entity_id: light.recessed_lights_level_9_0
        turn_off:
          service: light.turn_off
          entity_id: light.recessed_lights_level_9_0

Now my problem arises when I try to perform some other action besides simply turning on the switch. My next project is to get brightness manipulations working, which requires passing the brightness parameter in to the light.turn_on service method. From the documentation of the template switches, I do not see an option for passing in attributes to the configured turn_on/turn_off service calls.

Is there some way to do something like this? Or am I looking at a feature request here?

Thanks for any help on this.

I’ve been given this some thought. How about you call a script instead of the switch which does a quick mapping of the entities from room names? And calls the correct service. Would this work for you?

~Cheers

I’m not sure if this is exactly what you meant, but I believe a script would work for me. Looks like based on the PR + example here I can pass variables to the script. The question now is whether I am able to do that from the API.AI configuration, or if it only works under the automation platform…

Anything you pass to the script in the “data” can be used.

  intents:
    TurnLightOnOff:
      speech: !include templates/alexa_confirm.yaml
      action:
        - service: script.turn_on
          data_template:
            entity_id: script.room_mapper
            room: {{ room }}
            on_off: {{ on_off }}
            brightness: {{ brightness }}

You could also use a service_template and do the mapping right here.

  intents:
    TurnLightOnOff:
      speech: !include templates/alexa_confirm.yaml
      action:
        - service_template: >
            {%- if on_off == "on" -%}
              light.turn_on
            {%- else -%}
              light.turn_off
            {%- endif -%}
          data_template:
            entity_id: >
              {%- if room == "livingroom" -%}
                light.recessed_lights_level_9_0
              {%- elif room == "kitchen" -%}
                light.recessed_lights_level_10_0
              {%- endif -%}
            brightness: {{ brightness }}

No need for those “proxy switches”

~Cheers

Another thing I just realized is why don’t you use synonyms in your API.ai? You can map anything to anything in the Entities in API.ai you can map livingroom to recessed_lights_level_9_0. If you enter “recessed_lights_level_9_0” as a value and “livingroom” as a synonym…so the value HA gets will be recessed_lights_level_9_0 :slight_smile:

~Cheers

Now that is a good idea! The switches certainly create a lot of clutter in my configuration, so getting rid of them would be nice. Thanks!

I’ll update here again when I have the brightness working… getting a script going for it now.

Actually, just had a thought regarding the synonyms… That works fine if they are all lights, but I have some mapped across lights and switches, so I would still need some mapping logic on the HASS side I believe. Any ideas for that?

Sure you can do the best of both worlds just use synonyms but also use the service template…something like this maybe?

  intents:
    TurnLightOnOff:
      speech: !include templates/alexa_confirm.yaml
      action:
        - service_template: >
            {% set switches = ['recessed_lights_level_9_0', 'recessed_lights_level_10_0'] %}
            {%- if room in switches and on_off == "on" -%}
              switch.turn_on
            {%- elif room in switches and on_off == "off" -%}
              switch.turn_off
            {%- elif on_off == "on" -%}
              light.turn_on
            {%- else -%}
              light.turn_off
            {%- endif -%}
          data_template:
            entity_id: ...

Of course you would not use switches if you have more switches then lights…but that’s basically how I would do it.

~Cheers

1 Like

Here is the solution I came up with using the API.AI synonyms and basic templating:

intents:
  light.brightness:
    speech: >
      {% for state in states %}
        {%- if state.entity_id.split(".")[1] == room -%}
          Adjusting brightness for {{ state.name }} lights.
        {%- endif -%}
      {% endfor %}
    action:
      - service_template: >
          {%- set switches = ["outside_lights_switch_6_0"] -%}
          {%- set groups = ["upstairs_lights"] -%}
          {%- if room in switches -%}
            switch.turn_on
          {%- else -%}
            light.turn_on
          {%- endif -%}
        data_template:
          entity_id: >
            {%- if room in switches -%}
              {%- set prefix = "switch." -%}
            {%- elif room in groups -%}
              {%- set prefix = "group." -%}
            {%- else -%}
              {%- set prefix = "light." -%}
            {%- endif -%}
            {{ prefix }}{{ room }}
          brightness: >
            {%- set perc_val = api_percentage | replace('%', '') | int -%}
            {%- set val = ((perc_val / 100) * 255) | int -%}
            {{ val }}

Thanks for your help @PhyberApex!

2 Likes