What triggered this?

How would I know what triggered anything that happens on my HA?

For example, I have setup an automation that turns on and off the entrance light, but only 10 minutes after we leave or come home.
Sometimes it happens that the light turns off right when I am at the entrance. There is no trigger or condition to turn off the light at that specific time, so I would like to know what triggered the light off.
Is there a way to know that? This question goes to anything that happens in HA. The light is just an example. I want to be able to see what triggered anything.

The log book is good for viewing this kind of sequence. It can be slow to load on a Pi or low spec hardware. Limiting the number of domains/devices exposed helps a bit.

1 Like

You are right about the logbook and I have already done that, but the automation that triggered the light off has many triggers in it. I would like to know which one turned off the light. There is no such information in the logbook.

3

Besides that, what I am actually looking for, is the ability to see what triggered an entity on or off. Was it an automation (which one?) was it a button?

are you using for: x minutes in your trigger? Or are you using a delay? If you are using a delay, you may run into issues where your device momentarily leaves and arrives 10 minutes before you actually leave.

I am using a delay. I am aware of the issue with the gps tracker and this happens a lot. But it would be useful to have something like this in the picture (this is photoshopped - obviously)
2

Or even better there could be an extra info in the device itself, where it could mention what triggered it - an automation, another device, or a button, or whatever.

1 Like

You can create a notification that sends you that trigger information. Or update a input_text to contain that information. But remember, not all triggers are the same. Some triggers don’t have entity_ids so your full automation would need to account for all trigger types. That’s part of the reason why it’s not listed as an attribute for the automation.

Right! Thank you very much for your help guys!

Here’s an example of an automation that can be triggered by either one of two input_booleans or time. It records to the logbook whichever trigger activated the automation.

- alias: 'My Automation'
  trigger:
  - platform: state
    entity_id: input_boolean.flipper, input_boolean.toggler
  - platform: time
    at: '16:34:00'
  action:
  - service: logbook.log
    data_template:
      name: "My Automation"
      message: "Triggered by {{'time' if trigger.now is defined else trigger.to_state.entity_id }}" 

As per Petro’s comment, you have to be careful with how you handle the trigger object. For example, if time triggered the automation, trigger.now will be defined but trigger.to_state will not. In contrast, if one of the input_booleans triggered it, then trigger.to_state will be defined but trigger.now will not.

The result of triggering the automation looks like this:

Screenshot%20from%202019-05-03%2016-34-38

It’s kind of interesting that the “Triggered by time” message appears before the system-generated “Time changed to 16:34”. Nevertheless, it’s clear from the log what was responsible for triggering “My Automation”.

4 Likes

@123 thanks for this! This is pretty awesome for troubleshooting.

Sorry for the delayed answer, but I am too busy.
I have implemented @123’s template and It did what I was looking for. Thank you for this! This is really helpful and it should be as a secondary attribute in every entity to troubleshoot things!

Now that I have this, I gotta say I am surprised to find out that my wife’s tracker triggered the light and turned it off.
Funny thing is, it doesnt make sense and here is why:

This is my automation to turn off the light:

- alias: '[LIGHTS] Turn off Light'
  trigger:
  - platform: time
    at: '08:00:00'
  - platform: state
    entity_id: alarm_control_panel.house
    to: 'armed_home'
  - platform: state
    entity_id: alarm_control_panel.house
    to: 'away'
  - platform: state
    entity_id: device_tracker.google_maps_me
    from: 'home'
    to: 'not_home'
  - platform: state
    entity_id: device_tracker.google_maps_wife
    from: 'home'
    to: 'not_home'
  - platform: state
    entity_id: device_tracker.google_maps_me
    from: 'not_home'
    to: 'home'
  - platform: state
    entity_id: device_tracker.google_maps_wife
    from: 'not_home'
    to: 'home'
  condition:
    condition: or
    conditions:
      - condition: template
        value_template: '{% if states.proximity.metohome.attributes.dir_of_travel != "towards" and states.proximity.metohome.state > "1000" %}true{%else%}false{%endif%}'
      - condition: template
        value_template: '{% if states.proximity.wifetohome.attributes.dir_of_travel != "towards" and states.proximity.wifetohome.state > "1000" %}true{%else%}false{%endif%}'
      - condition: state
        entity_id: binary_sensor.main_entrance
        state: 'off'
  action:
  - delay: '00:10:00'
  - service: light.turn_off
    entity_id: light.entrance_light
  - service: logbook.log
    data_template:
      name: 'Turn off the light'
      message: "Triggered by {{'time' if trigger.now is defined else trigger.to_state.entity_id }}" 

At 10:26 we both were at the front entrance and the light turned off and on instantly. Looking at the logbook I see this:
3
Which indicates the light turned on and then off, but I guess that is probably something wrong with the logbook and has nothing to do with the light…
Anyway, what triggered the light off and on was my wife’s tracker and proximity accordingly.

If you see the actions at the automation I have a 10 minute delay. Taking a look back at the logbook 10 minutes ago, nothing happened that would trigger the light off.

So, what am I missing here? Why did the light turn off right before we walk into the house?

Just a funny sidenote: This sudden “turnoff the light” thing has happened many times but I didnt have the time to check what was wrong. Every time it was happening when my wife was in front of the door! Cool huh?

This message:
Turn off the light triggered by device_tracker.google ...
was probably produced by the automation you posted above, specifically this:

  - service: logbook.log
    data_template:
      name: 'Turn off the light'
      message: "Triggered by {{'time' if trigger.now is defined else trigger.to_state.entity_id }}" 

This message:
Turn on the light Triggered by proximity ...
could not have been produced by the same automation. It says “Turn on the light” which is different from what the automation is coded to report.

It means the “Turn on the light” log message must have been produced by a different automation.

Yup, I am aware of it. I know it was the action that recorded the message in the logbook. I also know the other message came from another automation. I did this. :wink:

All i said was just that the off and on logs should be the other way around. In reality the light first turned off and then on. But the logbook has it wrong. I dont mind this now, and I am not going to try to solve it.
For now, all I care about is what triggered the light off, since there is no automation that would trigger it off at that specific situation.
Since my last post, this hasnt occurred again, so I dont have other examples to examine. I will post again my findings when it happens again.
In the meanwhile, I edited my 2 automations like this:

  action:
  - service: logbook.log
    data_template:
      name: 'Turn off the light'
      message: "Light will turn off in 10 minutes by {{'time' if trigger.now is defined else trigger.to_state.entity_id }}" 
  - delay: '00:10:00'
  - service: light.turn_off
    entity_id: light.entrance_light
  - service: logbook.log
    data_template:
      name: 'Turn off the light'
      message: "Light turned off by {{'time' if trigger.now is defined else trigger.to_state.entity_id }}" 

I did this because I have the delay and I want to avoid any conflicts, or confusions. Do you think this is ok?

I believe it would have been helpful to everyone if this information had been available in your previous post. Perhaps more people would have responded to the question. Without it, the available evidence was challenging to comprehend let alone explain.

If you review your previous post, it does not contain the words “other way around” or anything similar. It simply questioned why the light was turning off:

If you see the actions at the automation I have a 10 minute delay. Taking a look back at the logbook 10 minutes ago, nothing happened that would trigger the light off.

So, what am I missing here? Why did the light turn off right before we walk into the house?

I don’t mean to belabor the point but if your previous post was a question about the order of the logbook’s entries, it was not evident to others.

As for the order of the data presented by the logbook, yes, it can be misleading (especially if the events occur in the same minute). I mentioned this in my original post.

It’s kind of interesting that the “Triggered by time” message appears before the system-generated “Time changed to 16:34”.

If it provides you with improved insight into the automation’s workings, it’s definitely OK.

1 Like

OK, I am back with news.

Yesterday, me and the wife came back from a 2 hours walk. The light turned on as we were approaching the house (as it should be) and when we were outside the door, the light turned off. There MUST be something I am doing wrong, so I am pasting my automation and my logs for a review.

Logs are oldest to newest from bottom to top (as in the logbook)

9:07 PM
Entrance light turned on
9:07 PM
Turn on the light Light turned on by proximity.argyristohome

The light turned on, by my proximity sensor as it should be.


9:12 PM
Entrance light turned off
9:12 PM
Turn off the light Light turned off by device_tracker.google_maps_argyrisgps
9:12 PM
[ARGYRIS] Argy Greeting has been triggered
9:12 PM
Argyris Direction of travel changed to towards
9:12 PM
argyris presence turned off
9:12 PM
[LIGHTS] Turn off Light has been triggered
9:12 PM
[ARGYRIS] Argy Boolean is home has been triggered
9:12 PM
Argyris is at home
9:12 PM
[FOTINI] Fotini Greeting has been triggered
9:12 PM
fotini presence turned off
9:12 PM
Turn off the light Light will turn off in 10 minutes by device_tracker.google_maps_fotinigps
9:12 PM
[LIGHTS] Turn off Light has been triggered
9:12 PM
[FOTINI] Fotini Boolean is home has been triggered
9:12 PM
presence_tracker changed to home
9:12 PM
Fotini is at home

As you can see presence detections are being made (wifi and gps) while the light is ON and suddenly my gps shows as being HOME (the log that says Argyris is at home is my gps tracker) and the light turns off immediately. This should occur 10 minutes AFTER I get home.

I am pasting the 2 automations for turning on and off the entrance light:

- alias: '[LIGHTS] Turn off Light'
  trigger:
  - platform: time
    at: '08:00:00'
  - platform: state
    entity_id: alarm_control_panel.house
    to: 'armed_home'
  - platform: state
    entity_id: alarm_control_panel.house
    to: 'away'
  - platform: state
    entity_id: device_tracker.google_maps_argyrisgps
    from: 'home'
    to: 'not_home'
  - platform: state
    entity_id: device_tracker.google_maps_fotinigps
    from: 'home'
    to: 'not_home'
  - platform: state
    entity_id: device_tracker.google_maps_argyrisgps
    from: 'not_home'
    to: 'home'
  - platform: state
    entity_id: device_tracker.google_maps_fotinigps
    from: 'not_home'
    to: 'home'
  condition:
    condition: or
    conditions:
      - condition: template
        value_template: '{% if states.proximity.argyristohome.attributes.dir_of_travel != "towards" and states.proximity.argyristohome.state > "1000" %}true{%else%}false{%endif%}'
      - condition: template
        value_template: '{% if states.proximity.fotinitohome.attributes.dir_of_travel != "towards" and states.proximity.fotinitohome.state > "1000" %}true{%else%}false{%endif%}'
      - condition: state
        entity_id: binary_sensor.main_entrance
        state: 'off'
  action:
  - service: logbook.log
    data_template:
      name: 'Turn off the light'
      message: "Light will turn off in 10 minutes by {{'time' if trigger.now is defined else trigger.to_state.entity_id }}" 
  - delay: '00:10:00'
  - service: light.turn_off
    entity_id: light.entrance_light
  - service: logbook.log
    data_template:
      name: 'Turn off the light'
      message: "Light turned off by {{'time' if trigger.now is defined else trigger.to_state.entity_id }}" 

- alias: '[LIGHTS] Turn on Light'
  trigger:
  - platform: state
    entity_id: binary_sensor.main_entrance
    to: 'on'
  - platform: template
    value_template: '{% if states.proximity.argyristohome.attributes.dir_of_travel == "towards" and states.input_boolean.argyris_presence.state == "on" and states.proximity.argyristohome.state|int < 1000 %}true{%else%}false{%endif%}'
  - platform: template
    value_template: '{% if states.proximity.fotinitohome.attributes.dir_of_travel == "towards" and states.input_boolean.fotini_presence.state == "on" and states.proximity.fotinitohome.state|int < 1000 %}true{%else%}false{%endif%}'
  - platform: state
    entity_id:
      - list of guests wifi tracker
    to: 'home'
  condition:
    condition: or
    conditions:
      - condition: sun
        after: sunset
        after_offset: "-01:00:00"
      - condition: sun
        before: sunrise
        before_offset: "00:10:00"
  action:
  - service: light.turn_on
    entity_id: light.entrance_light
  - service: logbook.log
    data_template:
      name: 'Turn on the light'
      message: "Light turned on by {{'time' if trigger.now is defined else trigger.to_state.entity_id }}" 

Side note:
When I am coming home separately from my wife, it works fine. The light turns on and off, as it should be, right on its time.

just found this post, and the above template would really enhance my own automation logging system very nicely! Just wouldn’t know yet how to incorporate that in the all automation tracking automation I use. Please let me ask by starting to post the relevant automation I have:

- alias: Automation ran
  id: 'Automation ran'
  initial_state: 'off'
  trigger:
    platform: event
    event_type: state_changed
  condition:
    condition: template
    value_template: >
      {% set skip_list = ['automation_ran', 'count_warnings', 'count_errors',
                           'activate_map_sensors_actueel', 'call_family_home',
                           'timed_python_scripts_per_20_secs'] %}
      {{ trigger.event.data.entity_id.startswith('automation.') and
         trigger.event.data.entity_id.split('.')[1] not in skip_list and
         'old_state' in trigger.event.data and 'new_state' in trigger.event.data }}
  action:
    - condition: template
      value_template: >
        {{ trigger.event.data.new_state.attributes.last_triggered !=
           trigger.event.data.old_state.attributes.last_triggered }}
    - service: notify.filed_automations
      data_template:
        message: >
         {{ as_timestamp(trigger.event.data.new_state.last_updated)|timestamp_custom("%d %b: %X") }}: {{ trigger.event.data.new_state.name }} by {{'time' if trigger.now is defined else trigger.to_state.entity_id }}
    - service: python_script.last_automation
      data_template:
        event: '{{ trigger.event }}'

as you can see, I’ve added the template to the message for the filed_automations service.
This will not work, as the automation is triggered by the event state_changed of all automations (selected in the condition).
Still, I hope the idea is clear: have the trigger of each automation recorded in the filed_notification.

Or would this only be possible if we’d add the {{'time' if trigger.now is defined else trigger.to_state.entity_id }} template to each individual automation as a variable, and then pass along that variable to my logging automation?

@123 btw, could this not simply be replaced by {{trigger.entity_id}} ?

So I just saw this post and I think you’re having a timing issue. It appears as if your device_tracker.google_maps_argyrisgps device tracker flip flops on the edge of home/away prior to your direction of travel changing. That’s the root cause of your issues. How are you calculating towards/away?

EDIT:
Or maybe that’s what you want? I guess I might be confused about you saying that you are returning home. I would assume that means you should be moving to home and towards.

EDIT: Ignore the first paragraph.

Your automation is firing twice. The second fire cancels the delay in the first. That’s your root cause.

It appears as if you have 2 people triggering this automation. First person starts the automation and delay, second person cancels the delay because that’s how automations work for some reason. So solutions moving forward:

1

Add an input_boolean that you turn on and turn off at the start and end of the automation. Don’t fire again if that boolean is on. (I personally don’t like this route because you can get out of sync if you restart during this and someone can turn the boolean off.

2

Strip the action section out and turn it into a script. Then in the automation, cancel the script (by turning it off) and turn it on. In essence, it will turn off the script and restart it. The last person to trigger is what the 10 minutes will be based on.

automation

- alias: '[LIGHTS] Turn off Light'
  trigger:
  - platform: time
    at: '08:00:00'
  - platform: state
    entity_id: alarm_control_panel.house
    to: 'armed_home'
  - platform: state
    entity_id: alarm_control_panel.house
    to: 'away'
  - platform: state
    entity_id: device_tracker.google_maps_argyrisgps
    from: 'home'
    to: 'not_home'
  - platform: state
    entity_id: device_tracker.google_maps_fotinigps
    from: 'home'
    to: 'not_home'
  - platform: state
    entity_id: device_tracker.google_maps_argyrisgps
    from: 'not_home'
    to: 'home'
  - platform: state
    entity_id: device_tracker.google_maps_fotinigps
    from: 'not_home'
    to: 'home'
  condition:
    condition: or
    conditions:
      - condition: template
        value_template: '{% if states.proximity.argyristohome.attributes.dir_of_travel != "towards" and states.proximity.argyristohome.state > "1000" %}true{%else%}false{%endif%}'
      - condition: template
        value_template: '{% if states.proximity.fotinitohome.attributes.dir_of_travel != "towards" and states.proximity.fotinitohome.state > "1000" %}true{%else%}false{%endif%}'
      - condition: state
        entity_id: binary_sensor.main_entrance
        state: 'off'
  action:
    - service: script.turn_off
      entity_id: script.light_presence
    - service: script.light_presence

script

script:
  light_presence:
    sequence:
      - service: logbook.log
        data_template:
          name: 'Turn off the light'
          message: "Light will turn off in 10 minutes by {{'time' if trigger.now is defined else trigger.to_state.entity_id }}" 
      - delay: '00:10:00'
      - service: light.turn_off
        entity_id: light.entrance_light
      - service: logbook.log
        data_template:
          name: 'Turn off the light'
          message: "Light turned off by {{'time' if trigger.now is defined else trigger.to_state.entity_id }}" 
1 Like

The point of this automation is to calculate whether or not turn off the light based on several conditions.
For example in this case (since you asked specifically for this) I want to turn on the light when I am closer than 1000 meters from my house but also if I am on my way to it.
So, that wont turn on the light when i am leaving the house and also it wont turn it on if I am stationary (at some friends house which is close to mine)
I am using a proximity sensor for this to make it work:

However, there is no trigger to turn off the light based on my proximity. I am using the proximity sensor only for turning the light on.
In my last example (which happened yesterday) the light immediately turned off when my gps tracker showed me at home…!
I am trying to figure out, what happened. I also looked for other automations that I may have missed, but nothing is messing with the entrance light except than these 2 automations.

If this is helpful, the light is switching on and off by a sonoff basic switch flashed with tasmota 6.5.

Yeah, I was editing my reply, check it out again.

That explains it!
Thank you very much for this!
I will try your second suggestion and will get back to you. I have no way to test this, but I am hoping after this, it will never happen again.
Cheers

In the following example, what would {{trigger.entity_id}} be if the trigger was time?

- alias: 'My Automation'
  trigger:
  - platform: state
    entity_id: input_boolean.flipper, input_boolean.toggler
  - platform: time
    at: '16:34:00'
  action:
  - service: logbook.log
    data_template:
      name: "My Automation"
      message: "Triggered by {{'time' if trigger.now is defined else trigger.to_state.entity_id }}"