Detecting Method Used to Turn on Light

Tags: #<Tag:0x00007fc414b2aa40>

Hi All,

Is it possible for HA to detect what method was used to turn on a light? I’d like to trigger an automation if the light switch was turned on manually via the wall vs an automation or user turning it on via the HA Dashboard.

Thanks!

1 Like

I don’t think so. Someone please correct me if I’m wrong.

An answer I’ve seen before (though never tried) is to create helper switches in HA and use them to turn on the light; one for each input method.

e.g. create a helper switch called “Light (Automation Control)” and add an automation to turn on the light based on that helper switch state.

Then when the manual switch is activated, you can add a Light (Automation)" helper switch. If it’s off you know it was the manual switch and not an automation. And vice versa.

This should work for as many inputs as you want as long as you trigger the helper switches and not the light directly.

What are you trying to achieve ?
Can you not just add a second action to the switch?

I’ve done it on mine by when turning on a light checking how long since the associated light automation last ran. This however only works if the light automation only has a single trigger, and single entitiy in that trigger. Multiple lights on one automation wouldn’t work (I dont believe there is anyway of accessing the last_triggered triggering entity_id).

If automation ran less than 2 seconds ago, assume automation turned the light on, if greater than 2 seconds, assume it was switched on at the switch manually.

I can probably grab a snippet later when at home.

Just a thought. Have you investigated using device triggers instead of entity triggers? And using event triggers instead of state triggers?

I turned on a light directly, then turned it off in HA

HA clearly knows that I turned it off using HA, and which logged in user performed that action.
The question is, how can we expose that information.

3 Likes

Every state change has s context id associated with it. When is turned off physically I believe is empty.

1 Like

So that is in the SQL db for the recorder log, can it be exposed during an automation?

The answer is yes!

self._context

I’m keen to see the outcome of this. I know that the Adaptive Lighting custom component can determine if a light was turned on by HA or locally but haven’t seen how to do it outside of that.

I DID IT!!! At least the part where a dashboard entry makes the change

I am simultaneously not proud of myself for spending this much time, but also proud for finding a solution.

This code specifically will detect if an automation was trigged from a a user id context, like from the UI. I have tested this with a light and nothing else, but it works. This SPECIFIC code looks for the absence of a user ID, like if I turned on the light from the wall, and not from the HA UI

alias: New Automation
description: ''
trigger:
  - platform: state
    entity_id: light.living_room_main_lights
condition:
  - condition: template
    value_template: '{{ trigger.to_state.context.user_id == none }}'
action:
  - event: lr_lights_changed
    event_data: {}
mode: single

The automation trigger is the entity STATE change you are looking for, the condition is lack of user id context, and the action is what you want to do when there is no user

7 Likes

That’s awesome! I can’t wait to test this out for my kitchen light. Thanks for sharing.

aaand I got it to ignore automations as well, add the following condition:

condition:
  - condition: template
    value_template: '{{ trigger.to_state.context.parent_id == none }}'

If there is no parent ID, there was no automation or script above this one
And if there i no user ID, it was not done from the UI
Therefore it was done at the device (if I am wrong let me know)

3 Likes

I kinda knew HA had this functionality now, but I hadn’t really tried it out yet. So don’t worry, your time was also useful for others :wink:

1 Like

I would like to receive events of who turned on the light, home assistant, or person. Reading a post here, I put together such automation and it works. Defines correctly, but I think that you can do it differently and correctly. How to write automation correctly so that you can get a notification who turned on the light, home assistant or a person?

Мне бы хотелось получать события кто включил свет, home assistant или человек. Читая тут пост, я собрал такую автоматизацию и она работает. Определяет правильно, но мне думается, что можно сделать по другому и правильно. Как правильно написать автоматизацию, чтобы можно было получить уведомление, кто включил свет, home assistant или человек?

alias: 'who turned on the light, the home assistant or the person?'
description: ''
trigger:
  - platform: state
    entity_id: light.yeelight_stripe_0x0000000007000
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ trigger.to_state.context.user_id == none }}'
          - condition: template
            value_template: '{{ trigger.to_state.context.parent_id == none }}'
        sequence:
          - service: notify.notify
            data:
              message: The light was turned on by a man
      - conditions:
          - condition: template
            value_template: '{{ trigger.to_state.context.user_id == none }}'
        sequence:
          - service: notify.notify
            data:
              message: The light turned on the Home Assistant
    default: []
mode: single
2 Likes

I created an automation that turns off the rest mode if the ceiling chandelier turns on automatically, and if I turn on the ceiling chandelier myself, the rest mode will not turn off. This code below works if I use a selection, but if I don’t use a selection and only a condition and an action, then this option doesn’t work. What is the right way to avoid using selection and to make automation work?

Создал автоматизацию которая выключает режим отдыха, если потолочная люстра включится автоматически, а если я включу потолочную люстру сам, то режим отдыха не выключится. Данный код ниже работает, если я использую выбор, но если я не буду использовать выбор и только условие и действие, то данный вариант не работает. Как правильно сделать, чтобы не использовать выбор и чтобы автоматизация работала?

  alias: 'Turn off the rest mode when the ceiling chandelier is turned on'
  description: If you turn on the ceiling chandelier via the voice assistant Alice, the rest mode will turn off
  trigger:
  - platform: device
    type: turned_on
    device_id: 1bx2b4c9a1b1bd26226sc4y13c090n12
    entity_id: light.yeelight_ceiling4_0x00000000100000
    domain: light
  condition:
  - condition: state
    entity_id: switch.tv
    state: 'on'
  - condition: state
    entity_id: input_boolean.relax_mode
    state: 'on'
  action:
  - choose:
    - conditions:
      - condition: template
        value_template: '{{ trigger.to_state.context.user_id == none }}'
      - condition: template
        value_template: '{{ trigger.to_state.context.parent_id == none }}'
      sequence: []
    - conditions:
      - condition: template
        value_template: '{{ trigger.to_state.context.parent_id == none }}'
      sequence:
      - service: input_boolean.turn_off
        target:
          entity_id: input_boolean.relax_mode
    default: []
  mode: single
1 Like

Nicely done, thanks for sharing!