Template Light state integration with Google Assistant

I’m trying out Home Assistant and I have a smart lamp that doesn’t have a Home Assistant integration, but it does support Google Assistant.

I’ve managed to create a light template for this lamp that calls Google Assistant to trigger events:

light:
  - platform: template
    lights:
      office_lights:
        unique_id: office_lights_template_001
        friendly_name: "Office Lights"
        # level_template: WIP
        # value_template: WIP
        # temperature_template: WIP
        turn_on:
          action: google_assistant_sdk.send_text_command
          data:
            command: turn on the office lights
        turn_off:
          action: google_assistant_sdk.send_text_command
          data:
            command: turn off the office lights
        set_level:
          action: google_assistant_sdk.send_text_command
          data:
            command: "{{ 'Set my office lights brightness to ' + (((brightness/255*100)|int)|string) + '%' }}"
        max_mireds_template: 1000 
        min_mireds_template: 100
        set_temperature:
          action: google_assistant_sdk.send_text_command
          data:
            command: "{{ 'Set my office lights to ' + (((1000000/color_temp)|int)|string) + 'K' }}"

With this I can control the on/off, brightness and temperature events, but I would like to implement value_template and level_template as well, so that the state in the UI matches the actual state of the lamp. Thinkering with google_assistant_sdk_custom, I can ask about the state of my lamp and get a text answer (“Office light is on.”/“Office light is set to 100 percent brightness.”).

I’m stuck at: 1) how to call actions inside value_template and level_template to use the return variable as a value (if possible at all, or if there’s another correct way of doing it); and 2) how to extract the number from the text (should I use regex or another built-in function?).

I’m new to home assistant, so if there’s another way of doing what I need please let me know! Any help is appreciated!

You can’t call the action in either of those. Your best option is likely to use Trigger-based template sensors to retrieve and store the values. You can raise custom events to use as triggers.

light:
  - platform: template
    lights:
      office_lights:
        unique_id: office_lights_template_001
        friendly_name: "Office Lights"
        level_template: "{{ states('sensor.office_light_brightness') }}"
        value_template: "{{ states('binary_sensor.office_light_state') }}"
        temperature_template: "{{ states('sensor.office_light_temperature') }}"
        turn_on:
          - action: google_assistant_sdk.send_text_command
            data:
              command: turn on the office lights
          - delay:
              milliseconds: 100
          - event: custom_template_light_update  
        turn_off:
          - action: google_assistant_sdk.send_text_command
            data:
              command: turn off the office lights
          - delay:
              milliseconds: 100
          - event: custom_template_light_update
        set_level:
          - action: google_assistant_sdk.send_text_command
            data:
              command: "{{ 'Set my office lights brightness to ' + (((brightness/255*100)|int)|string) + '%' }}"
          - delay:
              milliseconds: 100
          - event: custom_template_light_update
        max_mireds_template: 1000 
        min_mireds_template: 100
        set_temperature:
          - action: google_assistant_sdk.send_text_command
            data:
              command: "{{ 'Set my office lights to ' + (((1000000/color_temp)|int)|string) + 'K' }}"
          - delay:
              milliseconds: 100
          - event: custom_template_light_update

An outline of the Template sensors would be something like:

template:
  - trigger:
      - platform: homeassistant
        event: start
      - platform: event
        event_type: custom_template_light_update
    action:
      - #Google Assistant SDK Actions
    sensor:
      - name: Office Light Brightness
        state: |
          {# Regex to get brightness from response #}
      - name: Office Light Temperature
        state: |
          {# Regex to get temp from response #}
    binary_sensor:
      - name: Office Light State
        state: |
          {# Regex to get state from response #}
1 Like