Automation doesn't want to fire...? Any ideas? Thank you!

Can you spot anything wrong with this automation? I’ve checked if the sensors ‘stevework’ and workday_sensor are ‘on’ and they are.

I’m guessing it’s reluctant to work because of the two final checks, which are intended to be a check for whether one of two external doors has been opened within the last minute or not as this is where the automation gets a BIT more complicated than your average?

I checked and I was detected home this lunchtime within the very same minute that (13:14) that the pantry door opened (external door) so I’m at a bit of a loss.

  - id: steve_lunch
    alias: 'steve home for lunch'
    trigger:
      entity_id: group.steve
      platform: state
      to: 'home'
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: binary_sensor.workday_sensor
          state : 'on'
        - condition: state
          entity_id: input_boolean.stevework
          state: 'on'        
        - condition: template
          value_template: '{{ (as_timestamp(now()) - as_timestamp(states.automation.steve_home_for_lunch.attributes.last_triggered | default(0)) | int > 72000)}}'
        - condition: time
          after: '11:55:00'
          before: '14:10:00'
        - condition: or
          conditions:
            - condition: template
              value_template: "{{ (as_timestamp(now()) - as_timestamp((states.binary_sensor.pantry_door.last_updated)) <60)}}"
            - condition: template
              value_template: "{{ (as_timestamp(now()) - as_timestamp((states.binary_sensor.stable_door.last_updated)) <60)}}"
    action:
      - service: timer.start
        entity_id: timer.stevelunch
      - service: input_boolean.turn_off
        data:
          entity_id: input_boolean.stevework

Have you checked that state of the automation itself is ‘on’?

Good idea. But it is definitely on.

Hmmm. I think I might have ‘cracked it’. I think because I’m being detected home before I even open the door, that’s what’s doing it (or not doing it!)

Not sure how to work around that, apart from taking out the door checks entirely.

Or you can just put a delay in the trigger for few seconds to allow you time to open the door.

trigger:
  entity_id: group.steve
  platform: state
  to: 'home'
  for:
    seconds: 10

or switch the trigger to the door being to open and the condition to being ‘home’. since you always are seen as ‘home’ before you open the door then you won’t run into any timing issues.

Thanks @finity - I think your idea of the delay is better, because if my phone is off, then presence detection falls back to a bluetooth dongle being detected on my keychain, and that’s hard to predict. Alternatively I guess I could split the automation up into bits and use timers to track the different possibilities?

Possibilities of what? Can you give me an example?

Well, I guess the two possibilities:

  1. I’m not detected home until I’m inside the building, in which case, the automation above WOULD presumably work
  2. I’m detected home before I’m in the building, the automation does not work

you can create an automation that covers those two different possibilities by putting the “to:” states for everything in the trigger and the same states in the conditions as well.

that way whichever scenario occurs (home first then door open or door open the home) it should trigger the action.

kind of like this:

trigger:
  - platform: state
    entity_id: group.steve
    to: 'home'
  - platform: state
    entity_id: binary_sensor.pantry_door
    to: 'open'
  - platform: state
    entity_id: binary_sensor.stable_door
    to: 'open'
condition:
  - condition: state
    entity_id:  entity_id: group.steve
    state: 'home'
  - condition: state
    entity_id: binary_sensor.pantry_door
    state: 'open'
  - condition: state
    entity_id: binary_sensor.stable_door
    state: 'open'
  - the rest of your conditions
action:
  your action

that way if you show as home first nothing happens until the door opens, or if you open the door then nothing happens until you show as home.

1 Like

Thank you, I’ve modified my automation to this, will give it a try!! Thanks again

  - id: steve_lunch
    alias: 'steve home for lunch'
    trigger:
      - entity_id: group.steve
        platform: state
        to: 'home'
      - platform: state
        entity_id: binary_sensor.pantry_door
        to: 'open'
      - platform: state
        entity_id: binary_sensor.stable_door
        to: 'open'      
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: group.steve
          state : 'home'
        - condition: state
          entity_id: binary_sensor.workday_sensor
          state : 'on'
        - condition: state
          entity_id: input_boolean.stevework
          state: 'on'        
        - condition: template
          value_template: '{{ (as_timestamp(now()) - as_timestamp(states.automation.steve_home_for_lunch.attributes.last_triggered | default(0)) | int > 72000)}}'
        - condition: time
          after: '11:58:00'
          before: '14:16:00'
        - condition: or
          conditions:
            - condition: state
              entity_id: binary_sensor.pantry_door
              state: 'open'
            - condition: state
              entity_id: binary_sensor.stable_door
              state: 'open'
    action:
      - service: timer.start
        entity_id: timer.stevelunch
      - service: input_boolean.turn_off
        data:
          entity_id: input_boolean.stevework