Need help to automate response for the door opening

Hello, and happy easter!

I’m trying to automate the following: If the door openes while I am tagged as away, the code will wait for 15 seconds (giving the sensor time to tag me as home or just arrived). If I am still not tagged as home/just arrived, the automation will send me a notification and alert me that the door has been opened while I am gone.

If the door openes as I arrive, and HA changes me from gone to just arrived, it will greet me with one of the selected messages. I thought this would be pretty straight forward, but I am far from an experienced HA user, so does anyone have any input to make this code work as I described above? Thank you so very much in advance for any help or tips you may have!

alias: Door reaction
description: ''

condition:
  - condition: state
    state: 'on'
    entity_id: automation.tag_me_as_away

- wait_template: "{{ is_state('binary_sensor.ytterdor', 'opened') }}"
  timeout: 15
  - choose:
    condition:
     - condition: not
       conditions:
        - condition: state
          entity_id: automation.tag_me_as_home
          state: 'on'
        - condition: state
          entity_id: automation.tag_me_as_just_arrived
          state: 'on'
        sequence:
          action:
          - service: notify.notify
                data:
              message: Outer door opened without you being home!
              title: Outer door opened!
  default:
    - data:
      message: '{{ (''Velkommen hjem, deres majestet'', ''Hipp hipp, endelig er hans majestet hjemme igjen'', ''Vi har savnet deg, deres majestet'', ''Håper du har hatt en fin dag i dag, deres majestet'', ''Heisann, deres majestet, deilig å se deg igjen'' )|random }}'
      entity_id: media_player.stue
      service: tts.google_translate_say
      language: Norwegian
  continue_on_timeout: false
mode: single

hi there, why are you using a script for this? Are you calling this script from some automation? If not we have to build an automation.

Yes, sorry I should’ve clarified and been more clear with the terminology. This is all YAML-code from automation in HA.

use red node, it’s more powerful and easy to use

This must be a script because automations must have a trigger in it. The difference between automation and script is that the former has a trigger which ensures that it works when the trigger is achieved.

Now to our requirement. We can build an automation. This the basic outline of the automation.

alias: Door Open Notification
description: ''
mode: single
trigger:
  - platform: state
    entity_id: binary_sensor.ytterdor
    to: 'on'
condition: []
action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: state
                entity_id: device_tracker.your_device
                state: home
              - condition: state
                entity_id: device_tracker.your_device
                state: just_arrived
        sequence:
          - service: notify.notify
            data:
              message: Outer door opened without you being home!
              title: Outer door opened!
      - conditions:
          - condition: state
            entity_id: device_tracker.your_device
            state: not_home
        sequence:
          - service: notify.alexa_media
            data:
              message: Outer door opened with you being home!
    default: []

But this is not complete also I have build it from the limited into that I got form the first post. Accordingly you need to make some modifications. If you need more help, please ask. Always happy to help.

Tried installing node and ended up with error code “bad gateway: 502” when I tried to launch it, which I looked up, and is a different problem entirely. So I think I’ll stick with my current approach in the meantime.

Woah, thank you so incredibly much for your help!!! I’ll look into this later and get back to you, thank you again!

the first time i install node red i have the same problem
i solved by reinstalling node red i make the change on the configuration. clear the web history and after starting i open node red on my phone and it’s work now i’m able to open it everywhere .
don’t know why and how try it

Okay, so below is the code I’ve tried, but it doesn’t seem to work. Nothing happens, regardless of me opnening the door while tagged as present, or whilst being tagged as away. Any ideas?

alias: Door reaction
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.ytterdor
    to: opened
condition:
  - condition: state
    state: 'on'
    entity_id: automation.tag_me_as_away
action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: state
                entity_id: automation.tag_me_as_home
                state: 'on'
              - condition: state
                entity_id: automation.tag_me_as_just_arrived
                state: 'on'
        sequence:
          - service: tts.cloud_say
            entity_id: media_player.stue
            data:
              message: >-
                {{ ('Velkommen hjem, deres majestet', 'Hipp hipp, endelig er
                hans majestet hjemme igjen', 'Vi har savnet deg, deres
                majestet', 'Håper du har hatt en fin dag i dag, deres majestet',
                'Heisann hoppsann, deres majestet, deilig å se deg igjen'
                )|random }}
      - conditions:
          - condition: state
            entity_id: automation.tag_me_as_away
        sequence:
          - service: notify.notify
            data:
              message: Outer door opened while you where away!
              title: Door opened!
    default: []
mode: single

Please copy paste my code as a new automation and change the enity_id if you have to. Then try it and if it works please edit it to your specifications.

That was what I did. What I changed from your code was

  • adding the condition for me being away to begin with (otherwise the automation will trigger everytime the door opens regardless of me already being present or not),

  • changing the order of the conditions and sequences, since it looked to me in your code that HA will notify me that someone entered the door when detecting me at home, and that it will greet whoever comes trough the door when I’m not.

This checks if the automation is on or off and not the door sensor.

Correct, that’s the condition I want to have before the automation is triggered. If that automation is in state ‘on’, that means that I am tagged as away from the house. The trigger, which will initate the automation that will react to the door then opening is:

trigger:
  - platform: state
    entity_id: binary_sensor.ytterdor
    to: opened

Which checks wether or not the door has been opened.

Oh ok…I didnt think of that. Binary sensor state is either on or off. It cant be opened. Please check this in your developer console.

You can change the opened to on and try the automation.

Oh wow, that definetly helped! The automation triggers now, as my speaker greeted me. But I think I messed up the order, since it should have sent me a notification since I was tagged as away. Did I do the order wrong in my code above?

The entity ids you are using are not right. You are using automations instead of device trackers. If you see my script you can see that I have used device_trackers which is the way to check if you are home or not. Please check. What is the device_tracker entity id of your device?

Also the condition should not be automation as I said before, it should be again a device_tracker entity.

No, this just means, your automation is on, as in activated. It simply does show that this automation is ready to run and not disabled. You do know, that you can deactivate automations, eg. if you are away disable the morning-coffee-machine-automation, so the machine doesn’t get turned on.

This has nothing to do with how or when or why the automation is run… :wink:

Thanks for spoonfeeding me, @sheminasalam and @paddy0174, I clearly needed it :see_no_evil:

The code is as follows:

alias: React
description: "
trigger:
  - platform: state
    entity_id: binary_sensor.ytterdor
    to: 'on'
condition:
  - condition: state
    state: not_home
    entity_id: device_tracker.galaxy_s8
  - condition: state
    state: not_home
    entity_id: device_tracker.galaxy_buds_e879
action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: state
                entity_id: device_tracker.galaxy_s8
                state: home
              - condition: state
                entity_id: device_tracker.galaxy_buds_e879
                state: home
        sequence:
          - service: tts.cloud_say
            entity_id: media_player.stue
            data:
              message: >-
                {{ ('Velkommen hjem, deres majestet', 'Hipp hipp, endelig er
                hans majestet hjemme igjen', 'Vi har savnet deg, deres
                majestet', 'Håper du har hatt en fin dag i dag, deres majestet',
                'Heisann hoppsann, deres majestet, deilig å se deg igjen'
                )|random }}
      - conditions:
          - condition: state
            entity_id: device_tracker.galaxy_s8
            state: not_home
          - condition: state
            entity_id: device_tracker.galaxy_buds_e879
            state: not_home
        sequence:
          - service: notify.notify
            data:
              message: Ytterdøren ble åpnet uten at du var hjemme!
              title: Ytterdør åpnet!
    default: []
mode: single

I’ll do some testing and see how this turns out! Let me know if you see som obvious errors! :smiley:
Thanks again for all the help, I really appreciate it!

Made my day :rofl: :rofl: :rofl: :rofl: