Toggling a light in an automation

Hi,

Please could someone tell me why this automation doesn’t work? Am I using the homeassistant.toggle correctly?

I have a 4 button rf remote (and rflink), all I want is one of the buttons to toggle a MiLight on or off. My MiLights generally behave themselves and usually work nicely, I have other buttons on the same remote turning the light on.

  - alias: Toggle bedroom light when remote D pressed
    initial_state: True
    trigger:
      platform: state
      entity_id: switch.handheld_remote_button_d
    action:
      service: homeassistant.toggle
      entity_id: light.Bedroom_MiLight

This is the remote switch config, appears fine, buttons appear to work and HA responds accordingly.

switch:
  platform: rflink
  device_defaults:
    fire_event: True
    signal_repetitions: 2
  devices:
    ev1527_0cb829_08:
      name: Handheld remote button A
    ev1527_0cb829_04:
      name: Handheld remote button B
    ev1527_0cb829_02:
      name: Handheld remote button C
    ev1527_0cb829_01:
      name: Handheld remote button D

Many Thanks

Change the service call from:

action:
  service: homeassistant.toggle
  entity_id: light.Bedroom_MiLight

to:

action:
  service: light.toggle
  entity_id: light.Bedroom_MiLight
3 Likes

Thanks for the reply.

Thats pretty much what I started with, but the result of that is that it switches on once, but the second button press does not turn it off and the light stays on forever. If I then turn it off in the frontend, the button doesn’t turn it on again and it stays off.

Button press 1: Light turns on
Button press 2: nothing (light still on)
Button press 3: nothing (light still on)
and so on

Manual light turn off in front end, then
Button press 4: nothing (light still off)
Button press 5: nothing (light still off)
and thats it.

I’m pretty OK with basic automations and scripts, but this has me stumped.

Hmm, I don’t have any experience with remotes (except the Harmony) so I’m not sure if I can be much help beyond that. Sorry.

Thanks anyway. I’m suspecting something with the toggle action and Milight component. WiIl do some more testing.

Maybe its because when you press the the button on the rf remote it turns on the switch and it stays that way, and when you press the same button again, it does nothing because the switch is already on. So there is no state change and the automation wont trigger. If this is true then a possible fix might be

  - alias: Toggle bedroom light when remote D pressed
    initial_state: True
    trigger:
      platform: state
      entity_id: switch.handheld_remote_button_d
      state: 'on'
    action:
      - service: homeassistant.toggle
        entity_id: light.Bedroom_MiLight
      - service: switch.turn_off
        data:
          entity_id: switch.handheld_remote_button_d

This is just a guess.

Finally figured out a work around.
You were right about the switch state, its state was changing to ‘on’ at the first press, and then staying on - hence nothing happened when the button was pressed again as the switch was already on.

Created an action to set the switch to off when it had been triggered and toggle the light, but for some reason I don’t understand, the switch state was somehow coupled with the light toggle, even though there was no config to do that. This meant that when the switch state was changed to off by the automation, the light was going off at the same time. I don’t know if this is by design or not.

So I ended up doing two automations each with the same button press trigger, but with a condition to switch the light off if its already on and vice versa.

  - alias: Toggle bedroom light ON when remote D pressed
    trigger:
      platform: state
      entity_id: switch.handheld_remote_button_d
    condition:
      condition: state
      entity_id: light.Bedroom_MiLight
      state: 'off'
    action:
      - service: homeassistant.turn_off
        entity_id: switch.handheld_remote_button_d
      - service: light.turn_on
        entity_id: light.Bedroom_MiLight
        data:
          rgb_color: [255,188,75]
     
  - alias: Toggle bedroom light OFF when remote D pressed
    trigger:
      platform: state
      entity_id: switch.handheld_remote_button_d
    condition:
      condition: state
      entity_id: light.Bedroom_MiLight
      state: 'on'
    action:
      - service: homeassistant.turn_off
        entity_id: switch.handheld_remote_button_d
      - service: light.turn_off
        entity_id: light.Bedroom_MiLight

This also had the added benefit of being able to set the RGB colour which I don’t think you can do with a toggle.

Thanks.

5 Likes