Best way to implement an on/off switch with a controlled device?

Hi, working on building a new system (coming from a years’ long Homeseer environment) and was wondering if there’s a simpler way to have an on/off switch (in this case an Ikea TRADFRI) control a device - basically directly controlling the target with on or off commands.

I built this and it worked, first time (yay! for me), but I feel like there’s probably a better way to do this. I basically have either trigger on or off start the automation, then use a triggerID to pass what the button press was to a choose action and handle the target device accordingly. The IKEA switch sends different signals whether the ON or OFF buttons were pressed. The target in this scenario is an Insteon on/off module.

But again, maybe there’s a better way to do this? I’m just curious to see how those more advanced folks out there might accomplish this simple task.

alias: 'Family Room: Ikea switch test'
description: ''
trigger:
  - device_id: 3dd95cdc0aa30996be5f58d2
    domain: deconz
    platform: device
    type: remote_button_short_press
    subtype: turn_on
    id: onbutton
  - device_id: 3dd95cdc0aa30996be5f58d2
    domain: deconz
    platform: device
    type: remote_button_short_press
    subtype: turn_off
    id: offbutton
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: onbutton
        sequence:
          - type: turn_on
            device_id: 5159d8f8edcffb10244507
            entity_id: switch.on_off_module_4f_27_d2
            domain: switch
      - conditions:
          - condition: trigger
            id: offbutton
        sequence:
          - type: turn_off
            device_id: 5159d8f8edcffb10244507
            entity_id: switch.on_off_module_4f_27_d2
            domain: switch
    default: []
mode: single

I always take the commands to HA, interpret them then send the commands to the bulb/device.
That way I can create custom commands that the switch didn’t have before.

Such as double click on the on/off switch you mention.
Or double click and hold.
I would lose all those possibilities if I had sent the command directly to the bulb/device.

But regarding your yaml.
That’s fine.
I would probably do like that if I was in that position.

The only thing you could do to make it more advanced is to name the trigger ID turn_on or _off
That way the trigger ID could be the type and you wouldn’t need the choose or a second action.

If that makes sense.

I don’t use the UI for creating automations so I don’t do device automations but you could do the same with state automations using a template in the service and it will “simplify” things (templates might not be simple tho depending on your abilities).

Anyway here is how I would do it:

alias: 'Family Room: Ikea switch test'
description: ''
trigger:
  - platform: state
    entity_id: your_entity_id_here
    to: 'on'
    id: onbutton
  - platform: state
    entity_id: your_entity_id_here
    to: 'off'
    id: offbutton
action:
  -  service: switch.turn_{{ 'on' if trigger.id == 'on' else 'off'}}
     entity_id: switch.on_off_module_4f_27_d2
mode: single

Not sure what your deconz entity is tho.

or you could mix and match by using the existing device triggers with the templated service call.

1 Like

I’m fairly sure you can’t use state triggers since this is events that fire.
So either use the device or the raw event data.
State could be possible if you set up an entity that triggers based on the event, but I believe that would require an automation so your back to square one.

I don’t understand why you always do yaml if statements with the trigger ID, why not just name the IDs what they should do?
Or does this not work?

.....
    id: switch.turn_on
....
....
....
....
action:
  -  service: {{ trigger.id }}
....

Oh just noticed a typo in your yaml.

-  service: switch.turn_{{ 'on' if trigger.id == 'onbutton' else 'off'}}

You forgot ‘button’

1 Like

That’s true and I was thinking the same thing as I was writing it. But I figured I’d throw it out and then if they had an entity it would work. But if there was no entity then I would advise to use events.

And that’s why I also put that they could use the device triggers as well. That would also solve the events problem.

I’m not really sure. I’ve never tried it.

I guess you could always do something like this too if the whole service name doesn’t work:

.....
    id: on
....

action:
  -  service: switch.turn_{{ trigger.id }}

I seem to lean a bit on the “wordy” side in my templates (and posts :laughing:) but this would shorten things a bit. If it works (and I can’t think of a reason that it wouldn’t) I’ll likely start using that in the few places I actually use trigger id’s.

thanks for the suggestion.

1 Like