External Switch Button to turn light on or off (service template? script? Other)?

Hey all,
I have what seems to be a fairly simple use case…

I have a Xiaomi button switch that, when pressed, with either tun a light off if the light was on OR will turn a light on if the light was off.

I’ve read through a number of posts that seem to be similar in use case, some of which have used service templates / switch template and others that suggest using scripts, but for the life of me in multiple iterations of configuration have not been able to get anything to work.

Is there an easier way to accomplish this? Am I missing something easy?

Progress!

Got it working for the single light, and am now trying to figure out how to incorporate a series of lights into the data_template:

- alias: 'Ryan`s Office External Light Switch'
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001b86a10
      click_type: single
  action:
    service_template: >
      {%- if is_state('switch.ryans_office_overhead_lights_switch', 'off') -%}
        homeassistant.turn_on
      {%- else -%}
        homeassistant.turn_off
      {%- endif -%}
    data_template:
      entity_id: >
        {%- if is_state('light.ryans_office_bloom', 'off') -%}
          switch.ryans_office_overhead_lights_switch
        {%- else -%}
          switch.ryans_office_overhead_lights_switch
        {%- endif -%}

I’m a little confused by what you’re trying to do, but what you have can be significantly simplified. Unless, of course, you are trying to do more that’s not obvious (to me) so far.

Try this:

- alias: "Ryan's Office External Light Switch"
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001b86a10
      click_type: single
  action:
    service: switch.toggle
    entity_id: switch.ryans_office_overhead_lights_switch

Yep, do what @pnbruckner suggested, just use a toggle service. It’s worth noting that you can also use the switch.toggle service for a group of lights. My script below:

- alias: "Bedroom Light Switch"
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001209892
      click_type: single
  action:
    service: switch.toggle
    entity_id: group.bedroom

I believe there is a home_assistant.toggle that will simplify your service.

Thanks for the input, everyone.

What I provided doesn’t make sense for just a single switch, as the toggle service would flip it between on & off.

The reason I went down the more complicated road was in the following scenario:

  • I have a group that’s comprised of multiple lights / switches
  • Those lights & switches could be in differing states of on & off
  • I want the state of the overhead light switch to be what drives the state of all lights/switches in my office

My assumption was if I used a toggle against a group, it would either:

  • Flip those that are on to off
  • Flip those that are off to on
  • Flip all of them to a single state, which may not be the one that I want

The automation that I’m currently running is:

- alias: 'Ryan`s Office External Light Switch'
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001b86a10
      click_type: single
  action:
    service_template: >
      {%- if is_state('switch.ryans_office_overhead_lights_switch', 'off') -%}
        homeassistant.turn_on
      {%- else -%}
        homeassistant.turn_off
      {%- endif -%}
    data_template:
      entity_id: >
        {%- if is_state('switch.ryans_office_overhead_lights_switch', 'off') -%}
          group.ryans_office_lights
        {%- else -%}
          group.ryans_office_lights
        {%- endif -%}

Am I over-complicating still? I’m all for doing this easier if possible.

Thanks!

You have an if statement testing a condition, but the end result will always be “group.ryans_office_lights”. You might also be able to refactor your service template.

You could also create two separate automations with a condition, one for on and one for off. The code will be cleaner because you won’t need to use a template, but the drawback is you have created an additional automation.

- alias: 'Ryan`s Office External Light Switch'
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001b86a10
      click_type: single
  action:
    service_template: >
        homeassistant.turn_{{ states.switch.ryans_office_overhead_lights_switch.state }}
    data_template:
      entity_id: >
        group.ryans_office_lights

Ok, with the additional explanation and the code you wrote I think it’s now clear what you want to do. I.e., when the click happens, turn all the lights in the group on if the overhead lights are off, and turn all the lights in the group off if the overhead lights are on. Is that it? If so, then what you have will work, but it can be simplified a little:

- alias: 'Ryan`s Office External Light Switch'
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001b86a10
      click_type: single
  action:
    service_template: >
      {% if is_state('switch.ryans_office_overhead_lights_switch', 'off') %}
        homeassistant.turn_on
      {% else %}
        homeassistant.turn_off
      {% endif %}
    entity_id: group.ryans_office_lights

Regarding the toggle service, yes, it will toggle each entity, whether you list it individually or it’s in a group. So all that were off will go on, and all that are on will go off. Now that I understand (I think) what you’re trying to do, that is not the right service to use in this case.

@BrendanMoran, yes, the service template could be re-factored like you say, but I think what you wrote is the reverse of what the OP is looking for.

2 Likes

This is fantastic, thank you @pnbruckner - thanks to this code I’ve got my basement lights group working almost 100% as I want them to. Quick question (and it REALLY doesn’t matter if that answer isn’t a simple one) - is it possible to instead of

service_template: >
  {% if is_state('switch.ryans_office_overhead_lights_switch', 'on') %}
    homeassistant.turn_off
  {% else %}
    homeassistant.turn_on
  {% endif %}
entity_id: group.ryans_office_lights

turning on EVERY light, turn on only one whilst retaining the turn off ALL ?

If I understand you correctly, you could do something like this:

service_template: >
  {% if is_state('switch.ryans_office_overhead_lights_switch', 'on') %}
    homeassistant.turn_off
  {% else %}
    homeassistant.turn_on
  {% endif %}
data_template:
  entity_id: >
    {% if is_state('switch.ryans_office_overhead_lights_switch', 'on') %}
      group.ryans_office_lights
    {% else %}
      light.some_light
    {% endif %}
1 Like

Thank you, will try this! :slight_smile:

Hi I have this to turn off or on a switch

‘’’

  • alias: xxxxxxx
    trigger:
    • entity_id: binary_sensor.switch_158d0002696f43
      platform: state
      action:
      service: switch.toggle
      entity_id: switch.8624207568c63add0a99
      id: 6d4095a682cc4795a4b63dcde6109bb6
      ‘’’

But it seems not to work
Any Idea?

Please read:

and