How to find out what was the original trigger of a trigger?

Is there a way to find out what triggered a light to trigger my automation?

I have 18 zigbee spots in the living room. I do have a switch that switches the power to those lights off, when all lights have been of for over 2 hours, thus saving electricity. Only when we use a switch again or it is at the time we would wake up, the switch switches the electricity back on. As soon as a light is detected in Zigbee, the state goes from unavailable to on. I detect that, and will switch the light to the required state (often Off). However, sometimes such a light will not respond, and within HA I will see the light is switched on again, without a ‘triggered by’.

I want to catch those un-triggered changes of state.

I seen an automation option of ‘triggered by’, but it states I must set ID’s to my automations, to use this function. However, I want those who do not have an ID. I just want to create one automation for all these lights, that sees when any of these lights go from off to on. There I want to filter out those that are triggered by automation, scripts, scenes and those who we switch on manually on our mobile (thus no automation). Only those who I see in my log as ‘switched on’, without any reason why, will then be switched off again in this automation.

Any idea’s?

Have you looked at the traces for the automation?

It’s an option in the right hand corner when the automaton is selected.

My appologies if I didn’t mke it clear enough.

  • My normal automations work and send out a switch off to all lights. The traces reflect that
  • HA will show the ‘Off’ state for a few seconds
  • Without any automation or manual intervention, the state of the light changes to ‘On’ a couple of seconds later
  • I want to detect that any of my lights are turned ON again, without any automation, script, or user asking for it. And if so, be able to act on it.

That is unpossible. How can you detect something and act on it if you do not set up the detection and automation to act on it?

Well, as mentioned, if you check the history of state of the lamp, you see things like ‘Triggered by automation ’ or ‘Triggered by action: Light switch off’. I want to have a trigger that acts when NO trigger is detected, thus when the log currently states ‘Switched On’. This happens when the light status is changed outside of the home automation, or (in my case) when the light has not processed the ‘Off’ request, and HA detects later that the state is currently (still) On and changes this in HA aswell.

I want to create an automation when that happens, so I can switch these lights off, when they should have stayed/been off.

So in simplified terms, you want to record power interruptions that are separate from automation triggers?

Power Interuptions, perhaps not per se, but state changes that are not initiated by HA

This made no sense dude:

What you really want is a trigger when there is no context.

See: How to use context

Write an automation.to trigger on any state change of the entities,

triggers:
  - trigger: state
    entity_id:
      - light.my_lamp
      - fan.my_fan
    not_to:
      - unavailable
      - unknown

then you want these conditions:

      - conditions:
          - condition: template
            value_template: '{{ trigger.to_state.context.id != none }}'
          - condition: template
            value_template: '{{ trigger.to_state.context.parent_id == none }}'
          - condition: template
            value_template: '{{ trigger.to_state.context.user_id == none }}'

This will trigger every time something is changed outside home assistant, e.g. someone turns on a smart light switch manually.

You can access which entity caused this like so:

actions:
  action: homeassistant.toggle
  target:
    entity_id: "{{ trigger.entity_id }}"
1 Like

Thank you, I was not aware this was called ‘Context’. As English is not my first language, the only way I knew how to descripe it, was ‘a change of state without automation’. I am greatfull that you were able to decipher my broken English.

I will try your sollution tonight, and will let you know if this was indeed the solution, as it now to me seems to be.

My first test seemed to work. Thank you once again for your solution.

1 Like