Turn on lights with door lock -- One fly in ointment

I set up my first automation. Yay! It works…

…sort of.

I have the foyer lights come on when my Schlage FE469NX lock is unlocked. They come on fine when the door unlocks, but the problem is, if the lights are off when one leaves, when the door locks again, the lights come on. How can I stop that from happening?

- id: '1578617159127'
  alias: Foyer lights on when front door unlocked
  description: ''
  trigger:
  - entity_id: lock.schlage_allegion_be469_touchscreen_deadbolt_locked
    platform: state
  condition: []
  action:
  - device_id: 13b836e5c86b474aa4380535337b0d58
    domain: switch
    entity_id: switch.leviton_dz15s_1bz_decora_smart_switch_switch
    type: turn_on

What is the state of lock.schlage_allegion_be469_touchscreen_deadbolt_locked when it’s unlocked? I need to know the value you see on the STATES tab of the Developer Tools page.

Never mind. I checked the code. It should be unlocked. So:

- id: '1578617159127'
  alias: Foyer lights on when front door unlocked
  description: ''
  trigger:
  - entity_id: lock.schlage_allegion_be469_touchscreen_deadbolt_locked
    platform: state
    to: 'unlocked'
  condition: []
  action:
  - device_id: 13b836e5c86b474aa4380535337b0d58
    domain: switch
    entity_id: switch.leviton_dz15s_1bz_decora_smart_switch_switch
    type: turn_on

Thanks for the quick reply Phil. Unfortunately, this did not work. The state definitely says “Locked” and “Unlocked” accordingly, but the light is still going back on after the door re-locks. Can you think of something else I missed? Thanks again!

You need to look at the state on the STATES tab of the Developer Tools page. The state as shown elsewhere is not the “real” state as automations use. This is Home Assistant Automation 101. :slightly_smiling_face:

Did you reload the automations after making the change? I’m 99.44% sure what I suggested is what you need.

Phil, I found the reload state section in Dev and reloaded. Now it looks like it works, so you’re closer to 100% than 99.44%! Thanks again! This stuff is pretty cool.

So can you enlighten us as to what changed in the 11 minutes between your postings ?

I didn’t reload the automations. I didn’t know you needed to do that and after Phil recommended it, I poked around and found it in the Dev Services tab. I forgot that most times computers can’t assume.

Sorry, I see that you said that.

I just thought it may help others but you had already cleared up the problem.
You’ll know better for next time, don’t worry, we’ve all been there and sometimes just because I’m not sure if I’ve done it I’ll do it again. :rofl:
Cheers

Phil, another question regarding this setup:

If I wanted to have this lighting function work only from sunset to sunrise, could you recommend an automation to do that?

I thought about using sunset as a starter, but I think my script would only have the light work at the exact time of sunset, not after.

Any suggestions?

Use a condition
sun.sun == below_horizon

I’ll try that, thanks!

I’m probably a newbie idiot, but pasting that line into my yaml file under condition made it puke. It came back “unsupported”

See https://www.home-assistant.io/docs/scripts/conditions/#state-condition

Any time you need to try something new or find out stuff in general you should always consult the docs.
My snippet was meant as a clue for a template condition but if you got to the docs on conditions then one of the examples they give you is : -

condition:
  condition: state  # 'day' condition: from sunrise until sunset
  entity_id: sun.sun
  state: 'above_horizon'

You need the opposite so just invert the state, but check the states as they are not always transparent.
This one is ‘below_horizon’

Ah, perfect. I keep trying to do searches before posting, but sometimes I still can’t find the stuff. I have a lot of reading to do, but thanks again to you and @pnbruckner for the help!

It’s all good.
Keep trying and you’ll get there :+1:

Mutt, sorry for revisiting this, but I have a quick question:

The sun.sun above_horizon entity is working great. Is there a way to set an offset on the sunset side of that of, say, an hour? I’d like this to work an hour before sunset all the way through sunrise.

Thanks!

not trying to be rude, but 4 posts up is a link that has EXACTLY what you asking for…

condition:
  condition: sun
  after: sunset
  # Optional offset value - in this case it must from -1 hours relative to sunset, or after
  after_offset: "-01:00:00"

Right, and this is what I’m using, but I want it to do exactly that, with the addition of subtracting an hour from sunset.

So, based on your post, if I added your condition above the condition for ‘above_horizon’, would that work?