#How would I do this: camera detects

1 messages · Page 1 of 1 (latest)

stiff flame
#

Ugly but simple:

  • restart mode in the automation. Actions to turn on the light, delay 10 minutes, then turn off the light

Less ugly but more complex:

  1. (Re)start a 10 minute timer when motion is detected, turn on the light
  2. Turn off the light when the timer expires
kind perch
#

Essentially: turn light on when motion sensor goes to the “motion detected” state. Turn light off when the motion sensor has been in the “motion not detected” state for 10 minutes

supple helm
#

@kind perch so I need to setup two separate automation rules right?

kind perch
#

It requires two trigger, but you can have both triggers in the same automation. You can just copy the code from the link and change the entity_id’s to match yours.

supple helm
#
alias: "Living room motion - after sunset- turn on light "
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.living_room_motion
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.living_room_motion
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 5
condition:
  - condition: sun
    after: sunset
    after_offset: "-01:00:00"
    enabled: false
action:
  - service: light.turn_on
    metadata: {}
    data:
      color_temp: 500
      brightness_pct: 100
    target:
      device_id: 942b00d7c45f9ce9cb9eae1f8be18369
mode: single
#

This is not working... the light just stays on...

#

It's a camera that is detecting motion.

stiff flame
#

Check the trace

supple helm
#

I'm totally new to this... what am I looking for in the trace?

balmy crescentBOT
#

To test an automation there's three stages you can follow. Testing the action, the condition and action, and the whole automation:

  1. Use Configuration -> Automations to find the automation and then select Run in the three dots menu. If this fails your problem is in the action: section, and details should be found in your log file
  2. Use Developer tools -> Services and call automation.trigger on the automation with skip_condition: false. If the first passes but this fails then the problem is in your condition: block
  3. Use Developer tools -> States to find the trigger entity, click the name, then change the state (at the top) to something that'll trigger the automation before pushing Set State. If this fails then the problem is with your trigger: section, or the automation is turned off (you can check that in Automations, automations that are turned off will show Disabled)

You can also see this section in the docs about testing and automation traces.

stiff flame
#

You're looking to see what happened

supple helm
#

I run the automation manually. The light turns on immediately. Never goes off.

stiff flame
#

Right

#

What else would happen?

#
  1. Your automation never turns off the light
#

Try two automations, one with the on trigger, and another with the off trigger that turns off the light

#

To make what you've done above work you'll need choose with a trigger id or templates

supple helm
#

I guess I don't understand your code:

#
trigger:
  - platform: state
    entity_id: binary_sensor.your_motion_sensor_here # change this
    to: 'on'
  - platform: state
    entity_id: binary_sensor.your_motion_sensor_here # change this
    to: 'off'
    for:
      minutes: 2 # adjust as necessary 
action:
  - service: "light.turn_{{ trigger.to_state.state }}"
    target:
      entity_id: light.your_light_here # change this
mode: single
#

is trigger.to_state.state a variable?

#

Sorry. Before I ask more questions, I should learn the syntax.

stiff flame
#

That's a template

#

Well, {{ ... }} is

#

What you have there will work, but now you can't manually run the automation

#

So, follow step 3 of the testing

kind perch
#

Here is documentation on trigger variables; specifically the link is for trigger variables that are available for state triggers: https://www.home-assistant.io/docs/automation/templating/#state

So in the example code, trigger.to_state.state will be on when the automation triggers from the motion sensor turning on, and it will be off when triggered by the motion sensor turning off

supple helm
#

@kind perch Got it. That makes sense.

#

Would a 'repeat' construct work? Or is that too processor intensive.

#

repeat turn on light...until motion detection is off.

#
action:
  - service: light.turn_{{ trigger.to_state.state }}
    metadata: {}
    data:
      color_temp: 500
      brightness_pct: 100
    target:
      device_id: 942b00d7c45f9ce9cb9eae1f8be18369
#

My YAML editor would not keep "light.turn{{ trigger.to_state.state}}"

#

It would always remove the quotes...

#

@kind perch @stiff flame Is my syntax not correct?

stiff flame
#

What editor?

#

@supple helm?

supple helm
#

Using Home Assistant to edit YAML.

stiff flame
#

The built in automation editor?

supple helm
#

Yeah.

stiff flame
#

Then that's fine

supple helm
#

I get this error in the Trace panel:

#

Error: Error rendering service name template: UndefinedError: 'dict object' has no attribute 'to_state'

stiff flame
#

You pushed Run?

supple helm
#

yup

stiff flame
#

As I said above, you can't do that

balmy crescentBOT
#

To test an automation there's three stages you can follow. Testing the action, the condition and action, and the whole automation:

  1. Use Configuration -> Automations to find the automation and then select Run in the three dots menu. If this fails your problem is in the action: section, and details should be found in your log file
  2. Use Developer tools -> Services and call automation.trigger on the automation with skip_condition: false. If the first passes but this fails then the problem is in your condition: block
  3. Use Developer tools -> States to find the trigger entity, click the name, then change the state (at the top) to something that'll trigger the automation before pushing Set State. If this fails then the problem is with your trigger: section, or the automation is turned off (you can check that in Automations, automations that are turned off will show Disabled)

You can also see this section in the docs about testing and automation traces.

stiff flame
#

Follow step (3)

supple helm
#

I eventually did this:

id: '1715670321601'
alias: Toggle Living Light - based on Motion (after Sunset + 3 hrs)
description: ''
trigger:
  - type: motion
    platform: device
    device_id: ---------------------------
    entity_id: ---------------------------
    domain: binary_sensor
    id: living_room_yes_motion
  - type: no_motion
    platform: device
    device_id: --------------
    entity_id: --------------
    domain: binary_sensor
    id: living_room_no_motion
    for:
      hours: 0
      minutes: 10
      seconds: 0
condition:
  - condition: sun
    after: sunset
    after_offset: '3:00:00'
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - living_room_yes_motion
        sequence:
          - service: light.turn_on
            metadata: {}
            data:
              brightness: 255
            target:
              device_id: -----------------
      - conditions:
          - condition: trigger
            id:
              - living_room_no_motion
        sequence:
          - service: light.turn_off
            metadata: {}
            data: {}
            target:
              device_id: -----------------
mode: single
#

Used the UI to configure this.

#

Maybe it's overkill and it's not as simple as your original suggestion but it works.

stiff flame
#

FYI devices and entity ids aren't sensitive... knowing them is as useful to an attacker as knowing the IP addresses of your home network aka not at all

kind perch