How to use the response of an action as a state for a switch?

I am trying to create a switch card for turning lights and plugs on and off via Google Assistant SDK Custom that I can’t connect to Home Assistant. I have started with a subwoofer connected to a smart plug.

This is the code for the switch card:

show_name: true
show_icon: true
type: button
tap_action:
  action: toggle
entity: switch.max_subwoofer
name: Sub
show_state: false
grid_options:
  columns: 3
  rows: 2

This is the code for the custom switch (in configuration.yaml):

switch:
  - platform: template
    switches:
      max_subwoofer:
        value_template: "{{ ... }}"
        turn_on:
          action: google_assistant_sdk_custom.send_text_command
          target: {}
          data:
            command: toggle Max's Subwoofer
        turn_off:
          action: google_assistant_sdk_custom.send_text_command
          target: {}
          data:
            command: toggle Max's Subwoofer

I can get the state of the plug via the following command in the text chat with Google Assistant in HA:

How can I use this to get a ‘state’ for the value_template variable for the switch? Everything I see online is about using sensors or entities already defined with HA, however I only have the response from this Google command. Any help would be greatly appreciated :slight_smile:

value_template: "{{ is_state('switch.sub','on') }}"

Sorry I don’t think I was clear. I change the state of the plug via google home seperatly from HA, so I need it to be able to adjust the state based off the google assistant command

No you don’t.

You have the entity available as evidenced by its use in your card:

That switch is the one I defined in configuration.yaml, as shown below in my original post.

I can’t use the state of that as it doesn’t change when I change the sub from Google Home.

No, that one would be called switch.max_subwoofer

Ah that’s my fault: I renamed the switch from sub to max_subwoofer and didn’t change the card reference, apologies for the confusion.

Home Assistant is not connected to the plug in any way, so it has no idea about the current state. I need to be able to use the output from the Google Assistant command to determine the state for the switch somehow - as I turn the sub off regularly via Google voice commands.

Then you will have to issue two commands for each switch action. One to change the state and one to ask what the new state is. Then save that response in something you can use in the value template, like the first example in the docs you linked to.

e.g.

switch:
  - platform: template
    switches:
      max_subwoofer:
        value_template: "{{ is_state('input_text.sub_response','on') }}"
        turn_on:
          - action: google_assistant_sdk_custom.send_text_command
            target: {}
            data:
              command: toggle Max's Subwoofer
          - action: google_assistant_sdk_custom.send_text_command
            target: {}
            data: 
              command: what is the status of Max's Subwoofer
            response_variable: response
          - service: input_text.set_value
            data:
              value: "{{ response.responses[0].text }}"
            target:
              entity_id: input_text.sub_response
        turn_off:
          - action: google_assistant_sdk_custom.send_text_command
            target: {}
            data:
              command: toggle Max's Subwoofer
          - action: google_assistant_sdk_custom.send_text_command
            target: {}
            data: 
              command: what is the status of Max's Subwoofer
            response_variable: response
          - service: input_text.set_value
            data:
              value: "{{ response.responses[0].text }}"
            target:
              entity_id: input_text.sub_response

Note:

  1. you need to create the input text helper input_text.sub_response

  2. you need to change this template to extract the states ‘on’ or ‘off’ depending on what you actually receive as a response to the get state command:

value: "{{ response.responses[0].text }}"

Surely this will have the same issue, it will only update the state when I press the button in HA? Unless I have misunderstood.

I want HA to show the state of the switch based off the repsonse from Google (I guess it would have to poll the Google api repeatedly to check) so that the state of the switch in my HA dashboard reflects the actual state - not just what the state was when HA last changed it.

Perhaps using the input_text thing I could create a script that checks every x minutes or whatever and updates the variable? I feel like there must be a better way to do it though

That is what this does, it asks google what is the state and stores it in the response variable so you can use it:

Yes but it only asks after I press the button in HA right? Since it is under the turn_on stack? Therefore the following won’t work:

  1. Turn sub on via HA [HA state: on]
  2. Turn sub off via Google [HA state: on]
  3. Turn sub back on in HA [HA state: on]

Since it wouldn’t check the state until after I turn it on the second time. I need it to change the tracked state when I turn it off via Google somehow.

I would think that the value_template is where I need to ask Google since I imagine this is a regular checker of some kind? I just don’t know how to put the action for asking Google into the value_template.

Not possible sorry.

Change the way you use google. Ask it to turn on/off the HA switch.

Ah okay, I guess that’s why I couldn’t find anything online about it.

So there’s no way to like define a sensor to track the Google response?

HA can not track what you ask Google.

Not unless you are directly controlling it with the SDK.

Okay, that’s a shame.

Thank you for your help :slight_smile: