Why is this automation turning light on when entering, but not off after 10 minutes inactivity?

Why is this automation turning light on when entering the room and triggering the camera sensor, but not turning off after 10 minutes of inactivity? Got help in making this work (and finally stop throwing errors), but light doesn’t turn off after 10 minute time. Actually, I think it might flash off and then off again, but does not stay off.

Why is this automation turning light on when entering, but not off after 10 minutes inactivity?

alias: Lights On and Off on Timer in Office
description: Lights On and Off on Timer in Office
trigger:

  • platform: state
    entity_id:
    • binary_sensor.birch_neolink_1_motion
      from: “off”
      to: “on”
  • platform: state
    entity_id:
    • binary_sensor.birch_neolink_1_motion
      from: “on”
      for:
      minutes: 10
      to: “off”
      condition: []
      action:
  • service: homeassistant.turn_{{ states(‘binary_sensor.birch_neolink_1_motion’) }}
    entity_id: light.jasco_products_43080_light
    mode: single

If you put:

homeassistant.turn_{{ states(‘binary_sensor.birch_neolink_1_motion’) }}

Into the TEMPLATES area of developer tools, what do you get? What do you get if there is motion and if there isn’t?

Also, should this be:

light.turn_{{ states(‘binary_sensor.birch_neolink_1_motion’) }}

Also, although writing clever code like this is compact, I think it is much clearer, and much better for self documentation, to use trigger IDs and the choose action.

When I change it as you suggested to:

light.turn_{{ states(‘binary_sensor.birch_neolink_1_motion’) }}

in the template editor, I get:

TemplateSyntaxError: unexpected char '‘' at 21

When I leave it as it was, I get this in the template editor:

TemplateSyntaxError: unexpected char '‘' at 29

Maybe the quote character has somehow been replaced by something that looks like a quote but isn’t the correct quote.

Try deleting the first quote and then type the quote to put a quote in.

Where? Not sure what 29 is, there is no line 29.

Try this:

light.turn_{{ states('binary_sensor.birch_neolink_1_motion') }}

normal quotes, not fancy quotes

Try this:

service: homeassistant.turn_{{ states(‘binary_sensor.birch_neolink_1_motion’) }}
target:
  entity_id: light.jasco_products_43080_light

I’m adding the target: to the service call.

By the way, if you can share your full automation formatting it properly it would be much easier for the community to help. :wink:

Simple answer and how I have mine setup.

One automation to turn on with motion.

Second automation after the motion sensor is clear for X minutes.

I can share code if necessary.

Format your code properly. Because you have not done so, all of the quote characters have been turned into “smart quotes” by the forum software which are not valid in Jinja / YAML.

It also means we can’t see indentation errors you may have made.

The code that @templeton_nash asked you to try was copy/pasted from your original, containing the wrong quotes.

My apologies. Still new here. I’ve been using the pre-formatted text button. I think it is block quote that’s needed? Here is my code, modified with EdwardTFN suggestion. It now turns off after the prescribed time, but immediately turns back on, even with nobody in the room. So getting closer.

alias: Lights On and Off on Timer in Office
description: Lights On and Off on Timer in Office
trigger:
  - platform: state
    entity_id:
      - binary_sensor.birch_neolink_1_motion
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.birch_neolink_1_motion
    to: "off"
    for:
      minutes: 1
condition: []
action:
  - service: light.turn_{{ states('binary_sensor.birch_neolink_1_motion') }}
    target:
      entity_id: light.jasco_products_43080_light
mode: single

Sounds like the light turning off is being picked up by the motion sensor…

Have a look at the automation trace.

Or maybe look at the log trace for the device to see what was the last thing to turn the light on again. Maybe there’s another automation in play? I’ve had instances where HA accidentally saved a copy of the automation I was working on, with flawed logic in it.

I’m at a loss. Trace is beyond my scope. Let me make a comment and tell me I’m wrong. When new people here ask how to do an automation to make lights go on with motion, and off with absense of motion, they get comments like “there are a hundred of these, do a search”. Well, I’ve tried about 5 that were posed, did the modifications as necessary, and none of them worked. When I get good people trying to help me, none of their solutions work. And these are experienced people (like you reading this) with an automation that would seem to be very very common.

I’m aware that HA is a work in progress, but for new people, this is really, really hard. I’m going to have to give up on this.

1 Like

Rick, I would love it if you would provide those for me.

Turn on Automation:

- id: '1628660691232'
  alias: Lights-Turn On Front Porch Light When Front Door Cam Detects Motion
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.ds_hd1_motion_alarm
    from: 'off'
    to: 'on'
  action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.front_porch

Turn Off Automation:

- id: '1628661743315'
  alias: Lights-Turn Off Front Porch Light When Front Door Cam Motion Stops for 5
    Minutes
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.ds_hd1_motion_alarm
    from: 'on'
    to: 'off'
    for:
      hours: 0
      minutes: 5
      seconds: 0
  action:
  - service: switch.turn_off
    target:
      entity_id:
      - switch.front_porch

By the way, have you tried to use the Blueprint already available with your Home Assistant?
When creating a new automation, you are offered to create your automation from an existing blueprint and some are offered, including one named “Motion-activated Light”. That is tested by a lot of users and it is quite good and simple.

1 Like

Maybe I’m misunderstanding something fundamental. Rick are you saying with this automation, I can go into Services → Automation → Create Automation → Create New Automation → 3 dots Edit in YAML, then paste it in exactly save for the name of my sensor and switch, and that should work.

Because every time I do that, I get errors and it won’t save. Specifically: Message malformed: extra keys not allowed @ data[‘0’]

My automation looks like this:

- id: '1628660691232'
  alias: Lights-Turn On Front Porch Light When Front Door Cam Detects Motion
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.birch_neolink_1_motion
    from: 'off'
    to: 'on'
  action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: light.jasco_products_43080_light

Can you tell me where I would find that?

The blueprint @EdwardTFN is suggesting can be found on the list that is shown when you click the button to create a new automation.

Used the automation from the blueprint, and that did turn the light off after I left for seconds, but immediately turned it back on. In other words, the light will not stay off.

Would this still happen if I was using a motion detector instead of a camera? Is that the issue? Does the camera see the light off as motion?

Picture of automation attached.