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.
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
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
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:
you need to create the input text helper input_text.sub_response
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:
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
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:
Turn sub on via HA [HA state: on]
Turn sub off via Google [HA state: on]
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.