My automation doesn't work

Yesterday I tried to make my first automation that supposedly it turns un my light when a light sensor detects that is dark, it didn’t work so I have been tinkering with it trying to make it work but nothing.

here is the YAML configuration:

alias: autolights
description: Turn the lights on when dark
trigger:
  - type: value
    platform: device
    device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    entity_id: sensor.light
    domain: sensor
    below: 100
    for:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: device_tracker.iphone
        state: home
        attribute: device_tracker
      - condition: device
        type: is_off
        device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        entity_id: switch.xxxxxxxxxxxxxxxxxxxx_x
        domain: switch
      - condition: and
        conditions:
          - condition: time
            before: '23:00'
            after: '12:00'
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
              - sat
              - sun
action:
  - type: turn_on
    device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    entity_id: switch.xxxxxxxxxxxxxxxxxxxx_x
    domain: switch
  - type: turn_on
    device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    entity_id: switch.xxxxxxxxxxxxxxxxxxxx
    domain: switch
mode: restart

I am using a Smart Life light and a BH1750 sensor with ESPHome

Can someone help me🙏

Thanks in advance

there are a few troubleshooting steps you can take:

  1. you can use the new automation debugger that is found in the “automations” section of the configuration menu. click the little timer icon beside the automation. It should give you atrace and tell you where it failed.

  2. you can manually trigger the automation in the services section of developers tools. first trigger it with the “skip conditions” box unchecked and if it works then check the box and trigger it again. if it fails with skipping conditions then the problem is in the action. If it fails only after not skipping conditions then the failure is in the conditions. If it works under both conditions then the problem is with the trigger.

Does the entity called device_tracker.iphone actually have an attribute called device_tracker?

condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: device_tracker.iphone
        state: home
        attribute: device_tracker <-------- This

If that attribute doesn’t actually exist, remove the line referring to it from the State Condition.

I was going down the “teach a man to fish…” route. :wink:

Conditions are AND by default, and weekday is optional, so this can reduce to:

condition:
  - condition: state
    entity_id: device_tracker.iphone
    state: home
  - condition: device
    type: is_off
    device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    entity_id: switch.xxxxxxxxxxxxxxxxxxxx_x
    domain: switch
  - condition: time
    before: "23:00"
    after: "12:00"

As per finity’s post, does the light come on if you manually trigger the automation?

it does turn un when I trigger the automaton manually

When you manually execute an automation, it skips the automation’s trigger and condition; it only executes the action.

Your test confirms the problem lies either in the trigger or the condition. I believe the problem is in the condition, specifically, this line which should be removed:

attribute: device_tracker

I tried the automation debugger but I didn’t understand what’s happening, it shows that it fails on the condition but I don’t know why
An when I trigger it from the services section of developers tool, it turns on the light even if the sensor gives a value above 100 or if it is 9:00 am

Because your device_tracker doesn’t have an attribute called device_tracker. Remove the line I mentioned in my previous post.

Because manual execution of an automation only executes its action (the trigger and condition are ignored).

I already remove that,

There are three parts to your condition. Remove them one at a time to establish which is the problem.

You overlooked to mention that previously. All we know is what you tell us and all you have told us is that the light turns on when you trigger the automation manually. That simply means the action works and the problem is with either the trigger or the condition.

For testing purposes, how are you activating the automation’s trigger? You must make the light sensor’s value change from a value higher than 100 to a value lower than 100, for at least a minute, for it to trigger. If the value is currently 90, making it decrease will not trigger the automation. To trigger, it must start from a value above 100 then decrease below 100.

Sorry about that

I simply cover the sensor with a piece of electrical tape

During the day, usually the sensor marks way over 100 (usual between 6k and 3K)

Apparently the problem is on the tracking

If you say the sensor’s value is initially between 6K and 3K and covering it with tape makes it go below 100 (and stay that way for at least a minute), then that should be sufficient to trigger the automation. If the action isn’t executed then it’s because the condition (of the three conditions) prevents it.

Here is the equivalent of your three conditions. You can replace what you currently have with this:

condition:
  - "{{ is_state('device_tracker.iphone', 'home') }}"
  - "{{ is_state('switch.xxxxx_x', 'off') }}"
  - "{{ 12 <= now().hour < 23 }}"

Test procedure:

  • Remove the last two conditions, reload automations, then activate the trigger (with the tape method).
  • If it works, add the second condition, reload automations, and activate trigger. If it fails, then the problem is the first condition.
  • If the second condition works, continue testing with the third condition.

EDIT

Correction. Removed unnecessary brackets after the word hour in the third Template Condition.

It worked

It worked

Didn’t work, the automation debugger showed:
Error: In ‘template’ condition: TypeError: ‘int’ object is not callable

I changed it for how it was

- condition: time
    before: "23:00"
    after: "12:00"

and now it works

Thanks a lot

1 Like

That’s due to my mistake. Change the last template from this:

- "{{ 12 <= now().hour() < 23 }}"

to this:

- "{{ 12 <= now().hour < 23 }}"
1 Like

It tells you why right in the debugger.

here is an example of an automation of mine that didn’t execute the actions:

notice the red ‘X’ at the bottom. That’s the step that stopped it from continuing.

If you click on the decision step “A/B” that leads to it it tells you what the thing was that caused it to not run.

in this case it was the condition for the garage door being open returned ‘false’ (because the door was already closed).