How to trigger a switch that is already turned ON?

I need to turn on a toggle switch that is already turned on.

A couple of situations:

Voice assistant turn on the Kettle (it won’t boil if the switch is already in the ON state)
Voice assistant turn off computer screens (they won’t turn off if the switch is already in the OFF state)

What is the best way to approach this needing to be able to turn a switch on or off that is already in the same state I need it to trigger?

A push button is not the answer because I also want to be able to turn on the computer screens or turn off the kettle.

1 Like

The first question would be why are they in the wrong state?
Why don’t you simply turn it off first and then on again (or the other way round)?

Sometimes I show the voice control kettle to guests “Voice assistant turn on kettle” and then “Voice assistant turn off kettle”. Sometimes I trigger the kettle “Voice assistant turn on kettle” and don’t bother to turn off the switch.

The next time I go to to say “Voice assistant turn on kettle” I want it to work if I haven’t turned the switch off.

Why don’t you turn it off and then turn it on again? I would create a script and that script would be my switch.

alias: Turn on kettle
sequence:
  - type: turn_on
    entity_id: switch.kettle
    domain: switch
  - type: turn_on
    entity_id: switch.kettle
    domain: switch
mode: single

Are you saying I can setup a script to respond to both off and on commands?

I have a script right now for each on and off command. An automation chooses which script to run when the switch get’s toggled.

alias: Kettle_on
sequence:
  - service: shell_command.kettle_on
    data: {}
mode: single

How would I format that to respond to both on and off commands?

create an input boolean to act as a dummy trigger for an automation.

use the voice assistant to turn on the boolean to trigger the automation instead of calling the script.

in the automation use a choose action to select which actions to take based on the state of the kettle switch.

if the kettle is on → turn it off then back on
if the kettle is off → just turn it on.

as the last step of the automation turn off the boolean.

The problem is if the boolean is already “ON” it won’t trigger the automation.

If I immediately turn the boolean back off after turning it on then I lose the ability to say “Voice assistant turn off the kettle”

That’s why I said this:

I doubt you ice the kettle for more than say 5 minutes. Why not have an automation that turns it off after 5 it so minutes if it’s on longer than that.

Still doesn’t answer the question, there are plenty of applications that need to be able to trigger a switch that is already turned on or off.

I disagree. I suppose in your case you are working wit no correct state at all (or assumed state?). Using a switch means HA needs to be aware of the state. If you can’t provide that you shouldn’t work with switches but services or scripts. You can also integrate scripts with voice assistants. If you want to use a switch you need to provide a correct state, by either polling the state or working with automations as explained above.

1 Like

Ok so I say “Voice assistant turn on bedroom light” but someone turned it off manually last time and the switch in home assistant is still turned on, as a result the bedroom light does not turn on…

I recently changed my home from Domoticz to Home Assistant. In Domoticz a switch could be triggered multiple times (for example if it was ON it could be turned ON a second time without first having to turn the switch off).

To me this is pretty basic functionality.

The solution for me:

  1. configuration.yaml (example)
homeassistant:
  name: Home
  unit_system: metric
  # etc
  customize:
    # Add an entry for each entity that you want to overwrite.
    input_boolean.kettle:
      assumed_state: true
      fire_event: true

  1. Then to get an automation to trigger each time I turn the switch on without first having to turn it off, I tell the automation to listen for an EVENT, here is my automation yaml (example)
alias: Kettle
description: ""
trigger:
  - platform: event
    event_data:
      domain: input_boolean
      service: turn_on
      service_data:
        entity_id: input_boolean.kettle
    event_type: call_service
condition: []
action:
  - service: script.turn_on
    data: {}
    target:
      entity_id: script.kettle
mode: single

Another example automation yaml:

alias: Kettle
description: ""
trigger:
  - platform: event
    event_data:
      domain: input_boolean
      service: turn_on
      service_data:
        entity_id: input_boolean.kettle
    event_type: call_service
  - platform: event
    event_data:
      domain: input_boolean
      service: turn_off
      service_data:
        entity_id: input_boolean.kettle
    event_type: call_service
condition: []
action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 500
  - if:
      - condition: state
        entity_id: input_boolean.kettle
        state: "on"
    then:
      - service: shell_command.kettle_on
        data: {}
  - if:
      - condition: state
        entity_id: input_boolean.kettle
        state: "off"
    then:
      - service: shell_command.kettle_off
        data: {}
mode: single

When you operate something manually, the correct state should be reflected in Home Assistant. If it’s not, then you’re not addressing the root problem.

So in your examples what turns the input boolean back off?

Why do you think an event listener is any better than a state change listener?

Both need a state change to work.

And how are you using the voice assistant to start the automation?

I don’t think there’s ever an application that you need to turn a switch on that’s on.

If my wall switch in my house is on, I don’t think I’m going to try to turn it on again while it’s on.

Your issue is the state behind the switch isn’t correctly reflecting is state.

The event listener can trigger even if already ON it does not need to go from OFF to ON or ON to OFF, it can trigger when a device turns ON and then ON again (even though the device history log will not show the multiple ON events), the event listener can see each ON event. Watch in: Developer Tools > Events, in “Listen to Events” enter “call_service” and press “Start Listening”, now trigger a boolean switch multiple times in the same state “example: voice assistant turn on x, voice assistant turn on x”. You should see both entries in the listener

Note that I believe the configuration.yaml modification (posted in one of my previous posts) for the boolean switch is needed before it will show in the event listener although I have not tested this so am not certain.

I can now say “Voice assistant turn on kettle” again and again and it turns on the Kettle just like it’s supposed to. I also have the flexibility of saying “Voice assistant turn off kettle” and it can turn off the kettle.

If I understood your explanation correctly, input_boolean.kettle is the entity that your voice assistant believes to be your actual kettle. When you tell the voice assistant to turn on the kettle it executes an input_boolean.turn_on service call. However, if input_boolean.kettle is already on then it would fail to trigger a State Trigger because there’s no state-change involved. Therefore you are using an Event Trigger to listen for the service call (input_boolean.turn_on) as opposed to a State Trigger listening for a (non-existent) state-change.

Is that a correct interpretation of what you have done?

yes, correct

If you don’t mind me asking, which voice assistant is it and which integration is being used to make it work with Home Assistant?