Turn on/off input_boolean (or alternative) with Google Assistant independent of actual state?

Hi, your suggestions please:

My set-up includes LightwaveRF gen 1 433 switches which do not report on/off state. The frontend status is usually wrong, but I can live with that. I mainly use Google Assistant or the physical switches. HA doesn’t seem to care what the reported “actual” state of a switch is. It will accept and execute an on or off command regardless. For example, even if HA thinks that the bedroom light is already on when it’s actually off, saying “Hey, Google, turn on the bedroom light” does just that.

I’d like to have the same behavior from Google Assistant with a input_boolean switch (“Sonos Everywhere”) that I use with automations to trigger separate on or off scripts that either [input_boolean ON] group and turn on (media_play), or [input_boolean OFF] turn off (media_pause) all speakers. Currently, a command like “Hey, Google, turn on Sonos Everywhere” only works if HA thinks that Sonos Everywhere is off. The problem is that someone may have manually started or paused Sonos using an app or the buttons on the speaker. HA is unaware of those manual changes.

I imagine that I could query media player status and have the input_boolean update accordingly. But the many possible scenarios mean that’s not something I want to tackle now. There must be a simpler way as long as I don’t care what the switch status shows in the front end.

I’m happy to use something other than an input_boolean as the trigger, as long a “turn on” and “turn off” work with Google Assistant.

Here’s the input_boolean:

sonos_everywhere_on_off:
    name: Sonos Everywhere
    initial: off
    icon: mdi:speaker

And here are the automations:

- id: '1549024279717'
  alias: Sonos Everywhere On
  trigger:
    platform: state
    entity_id: input_boolean.sonos_everywhere_on_off
    to: 'on'
  action:
    service: script.turn_on
    entity_id: script.sonos_play_whole_house

- id: '1549024407869'
  alias: Sonos Everywhere Off
  trigger:
    platform: state
    entity_id: input_boolean.sonos_everywhere_on_off
    to: 'off'
  action:
    service: script.turn_on
    entity_id: script.sonos_pause_whole_house

Thanks!

Keep your input boolean.

sonos_everywhere_on_off:
    name: Sonos Everywhere
    initial: off
    icon: mdi:speake

Make a template switch and expose that to google.

switch:
  - platform: template
    switches:
      sonos_everywhere_on_off:
        friendly_name: All Sonos
        value_template: "{{ is_state('input_boolean.sonos_everywhere_on_off') }}"
        turn_on:
          - service: script.sonos_play_whole_house
          - service: input_boolean.turn_on
            entity_id: input_boolean.sonos_everywhere_on_off
        turn_off:
          - service: script.sonos_pause_whole_house
          - service: input_boolean.turn_off
            entity_id: input_boolean.sonos_everywhere_on_off

Input boolean will ‘store the state’. But the script will always fire. The switch doesn’t care, it get’s the state from the input boolean. And if you don’t even care about storing the state and you just want a switch that you can call on or off with…

switch:
  - platform: template
    switches:
      sonos_everywhere_on_off:
        friendly_name: All Sonos
        value_template: "{{ True }}"
        turn_on:
          - service: script.sonos_play_whole_house
        turn_off:
          - service: script.sonos_pause_whole_house

It will always be on but you can call turn on or turn off and it will execute.

EDIT: Oh and you don’t need your automations, the switch just act’s like a switch. It’ll look like an input boolean / switch in the interface.

2 Likes

Wow. Thanks for spending so much time on your reply. I really appreciate it.

Both methods you suggested work perfectly. I decided to use the second method, and I’ve hidden the switch from the front end.

Hello,

I exposed switch (templates) to Google, but using it makes Google say “Sorry [homeassistant] is not available right now” / “I can’t reach homeassistant”, or any connection error (maybe because it’s made with template platform, which is a UI platform, not a data platform?)
So what I did is:

1. Create appropriate reusable scripts so everything is clean:

projector_on:
  alias: Projector on
  sequence:
    - service: remote.send_command
      data:
        [data: turn on my projector]

projector_off:
  alias: Projector off
  sequence:
    - service: remote.send_command
      data:
        [data: turn on my projector]

2. Create input_boolean

projector:
    name: Projector
    initial: off

3. Create switch templates to have switches available in the UI

    projector_onoff:
      value_template: '{{ states("input_boolean.projector") }}'
      friendly_name: "Projector"
      turn_on:
        - service: input_boolean.turn_on
          data:
            entity_id: input_boolean.projector
        # Other service will be linked with automation due to Google home voice command errors
        # - service: script.projector_on

      turn_off:
        - service: input_boolean.turn_off
          data:
            entity_id: input_boolean.projector
        # Other service will be linked with automation due to Google home voice command errors
        # - service: script.projector_off

4. Then created corresponding automations to link input_booleans to scripts

- id: "1613500967652"
  alias: Turn on projector
  description: ""
  trigger:
    - platform: state
      entity_id: input_boolean.projector
      to: "on"
  condition: []
  action:
    - service: script.projector_on
  mode: single

- id: "1613500967653"
  alias: Turn off projector
  description: ""
  trigger:
    - platform: state
      entity_id: input_boolean.projector
      to: "off"
  condition: []
  action:
    - service: script.projector_off
  mode: single

5. Then exposed the input_boolean to Google (not the switch)

  input_boolean.projector:
    name: Projector
    room: Lounge
    expose: true

If anyone has a simpler idea to avoid errors, I’m interested :slight_smile: