Google Assistant - Using Scripts for On and Off

I have a script for turning on my sprinkers, where it creates a sequence of actions including turning on a switch but also using a delay which is a input_number before turning off.

Obviously the turn off function either needs to be the switch directly, or call a different script, but it would be a different script then the turn on script.

How do we tell google assistant to differentiate the turn on, vs the turn off scripts ensuring it calls the right scripts?

You could use an input Boolean to trigger the on and off scripts. The input Boolean is what u expose to google assistant as a switch

Define a switch that has one script as turn on action and the other as turn off. Expose only the switch to Google and not the scripts

Thanks to you both.
i created a switch template - dont know if it will work until tomorrow. I’ll use these switches which in turn call the necessary script. Hopefully it works but will report back tomorrow.

switches:
ghveggies:
  value_template: "{{ is_state('switch.veggies', 'on') }}"
  turn_on:
    service: script.turn_on
    entity_id: script.water_veggies
  turn_off:
    service: script.turn_off
    entity_id: script.water_veggies_off
  
ghvacuum:
  value_template: "{{ is_state('vacuum.robotvacuum', 'on') }}"
  turn_on:
    service: homeassistant.turn_on
    entity_id: vacuum.robotvacuum
  turn_off:
    service: homeassistant.turn_off
    entity_id: vacuum.robotvacuum

Is:

switch:
  - platform:template
    switches:
      nameofswitch:

the only way to ‘define a switch’?

It needs a value_template which returns true or false - and creates a slider switch - but I don’t know what the current state is and would rather have two on/off buttons.

You could define two switches, which does not have areal value template, but rather an ‘Off’ template.
That way you can initiate the switch and it will return to off state afterwards.

For example I use that for the Google Assistant sync:

  switch:
    - platform: template
      switches:
        google_assistant_sync:
          value_template: "{{'off'}}"
          turn_on:
            service: google_assistant.request_sync
          turn_off:
            service: google_assistant.request_sync

Thanks but that won’t quite do what I want, I have a script which turns on all the tv stuff (amp, sky box, set inputs) and one that turns them off. So if I define a switch called ‘tv’ which calls those scripts for on/off then I can say to Google Home ‘turn on tv’ or ‘turn off tv’ and we’re good.
Hardcoding the state to false is ok for Google but the UI has a slider switch which is useless half the time.

Using a template switch is a way to do what your describing. It took me a little while to figure it out. You need to make sure that you set the correct type for the switch in the customize.yaml file

Heres what I use to turn on and off the nintendo switch activity on my harmony remote

config.yaml

switch:
  nintendo_switch:
    value_template: "{{ is_state('media_player.hdmi_8', 'on') }}"
    turn_on:
      service: script.turn_on
      data:
        entity_id: script.activity_nintentdoswitch
    turn_off:
      service: script.turn_on
      data:
        entity_id: script.activity_poweroff

customize.yaml

switch.nintendo_switch:
  google_assistant: true
  google_assistant_name: Nintendo Switch
  google_assistant_type: switch

@Orthodoxmonster What happens when someone changes the TV’s input but leaves the Nintendo on? media_player.hdmi_8 will be off which will flip the switch in your UI to ‘off’ so you won’t be able to turn it off again?

Is yours a - platform: template switch or is your yaml complete and there’s a way to define a switch I can’t find in the docs?

It doesn’t usually turn it to off, but for my use it doesn’t need to. All I care about is whether the correct harmony activity is active.

define your “template switch” as a switch in your customize.yaml for google assistant purposes. like in my example

So I created a template switch named “nintendo switch” that runs scripts depending on the template switch’s state. Then in the customize.yaml I define that template switch as a switch for google assistant purposes. google_assistant_type: switch

Blockquote[quote=“Orthodoxmonster, post:10, topic:37284”]
It doesn’t usually turn it to off, but for my use it doesn’t need to.
[/quote]

My problem is I do care. If devices are turned off any other way the switch is still sat at ‘on’ so I can’t activate ‘on’ again.

I’m not sure if you’ve actually tried it, or if this will change in the future, but asking google to turn something on/off just recalls the on/off script regardless of the switch’s actual state. So you can activate ‘on’ again, even if the switch in currently at ‘on’.

Ah, yes that works OK from Google, thanks.

But on the UI I have a toggle switch that is stuck in the wrong position. I’d prefer it if they were two buttons but being forced to return a true/false value makes it a toggle switch.

Hey, I am currently having problems with syncing my entity to google. It cant seem to be added. Any tips?

Edit: I was logged in to the wrong google account.