Wyze cam motion detection - use email as automation trigger

I am a new HASS user, I have the latest version running on Rasberry PI 3. I have several smart switches already integrated via the Tuya integration and I can control them without issue. I now want to move on to create an automation based on an email, which seems like it should be simple using the imap_email_content but for the life of me after playing around with this and researching the better part of a day I can’t make it work the way I want.

What I want is pretty simple: 1) Wyze cam detects motion and send an email to my gmail account (working) 2) Email is detected by HASS and triggers a switch to turn on (sort of works once) 3) Have the switch turn off x minutes later (not working)

I’ve setup the imap_email_content in my yaml config, but the issue I seem to be having is that once there is an email with the right subject line, the state gets set and it never goes away. Hence, I get an email “Motion detected”, and then that seems to be the state thereafter and always. I think what I want is to only have the state change if there is an email received in the last x seconds and otherwise set the state to something else, but I’m not sure how to do that. What I have so far is below, any help appreciated.

sensor:

  • platform: yr
  • platform: imap_email_content
    server: imap.gmail.com
    name: wyze_cam_garage
    port: 993
    username: ! secret
    password: ! secret
    senders:
    • [email protected]
      value_template: >-
      {% if ‘Motion detected’ in subject %}
      motion_detected
      {% endif %}
1 Like

I did the same with imap but switch to a folder sensor. What I did was create a binary sensor that I could shut off after 5 secs when motion is detected. I used a binary mqtt sensor so I could change the state by publishing to mqtt.

automation dahua_motion:
  initial_state: 'on'  
  alias: dahua_motion
  trigger:
    platform: event
    event_type: folder_watcher
    event_data:
      event_type: created
  action:
    - service: mqtt.publish
      data_template:
        topic: "rpi1/binary_sensor/{{ trigger.event.data.file.split('_')[1] | replace('ch', '') | replace('1', 'backyard_motion') | replace('2', 'backdoor_motion') | replace('3', 'driveway_motion')| replace('4', 'frontlawn_motion') }}/device_class"
        payload: "\"motion\""
    - service: mqtt.publish
      data_template:
        topic: "rpi1/binary_sensor/{{ trigger.event.data.file.split('_')[1] | replace('ch', '') | replace('1', 'backyard_motion') | replace('2', 'backdoor_motion') | replace('3', 'driveway_motion')| replace('4', 'frontlawn_motion') }}/state"
        payload: "on"
    - delay: 00:00:05 
    - service: mqtt.publish
      data_template:
        topic: "rpi1/binary_sensor/{{ trigger.event.data.file.split('_')[1] | replace('ch', '') | replace('1', 'backyard_motion') | replace('2', 'backdoor_motion') | replace('3', 'driveway_motion')| replace('4', 'frontlawn_motion') }}/state"
        payload: "off"

Thanks for the response, I have a few questions (hopefully not too dumb):

  1. I understand that this is watching a folder for a file to be created, are the files emails that are created by imap? If so, where is the configuration to connect the folder to imap? (basically I don’t get the connection between imap and the folder watch)

  2. Once you publish to mqtt, how are you using that to trigger the on/off of the switch?

Thanks

  1. The folder sensor is totally seperate from imap sensor except my nvr - camera has the option to send email (imap) or put a snap shot on a ftp server. (folder sensor)

  2. I setup a mqtt sensor which watches the state of a mqtt topic. If I publish to that topic, that senors will switch on or off.

In retrospect, I think this might be easier. Using your imap sensor and a blank command line switch.

switch:
  - platform: command_line
    switches:
      garage_motion:
        command_on: ''
        command_off: ''

Then use an automation to turn it on for 5 secs when your imap sensor changes.

automation garage_motion:
  initial_state: 'on'  
  alias: garage_motion
  trigger:
    platform: state
    entity: sensor.wyze_cam_garage
    to: 'motion_detected'
  action:
    - service: switch.turn_on
      entity: switch.garage_motion
    - delay: 00:00:05 
    - service: switch.turn_off
      entity: switch.garage_motion
"

If you need it to be binary_sensor instead of a switch, you can use a binary_sensory template to create one.

As a follow up, I have this solved in a different way (in case anyone else has a similar question).

I found the email alert setup to be confusing and convoluted and not really what I wanted, so I looked for an alternative. Fortunately, Wyze cams do have an integration with IFTTT that allows you to fire a “webhook” in response to motion instead of an email. I already had an IFTTT account (easy to setup if you don’t have one), so all I had to do was:

  1. Setup an IFTTT applet that has an “if” Wyze cam motion “then” make a web request (the request format is in the HASS documentation)
  2. Setup IFTTT integration with HASS (follow the instructions in the HASS docs for sending events from IFTTT, you’ll need to setup SSL and forward ports inbound from the Internet from your router)
  3. Setup the automation using a template (also in the IFTTT instructions), here is what I have setup in my configuration.yaml and it’s working exactly the way I wanted, including only turning on the garage light at night and then turning it off after 2 minutes:

automation old:
alias: ‘Garage Cam Motion’
trigger:
platform: event
event_type: ifttt_webhook_received
event_data:
action: call_service
condition:
condition: or # ‘when dark’ condition: either after sunset or before sunrise - equivalent to a state condition on sun.sun of below_horizon
conditions:
- condition: sun
after: sunset
- condition: sun
before: sunrise
action:
- service_template: ‘{{ trigger.event.data.service }}’
data_template:
entity_id: ‘{{ trigger.event.data.entity_id }}’
- delay: ‘00:02:00’
- service: switch.turn_off
data:
entity_id: switch.id_of_switch

Hopefully this may be of use to others

3 Likes

Obviously a bit more complicated to setup but I like my setup as I feel it improves on things a bit by adding a timer set and cancel based on the automation.

If movement happens, timer is started. If motion happens again, timer is started again. The only way that the input_boolean.motion_sensor turns off is if we receive a timer end event.

So we aren’t constantly turning off motion when there is still motion happening in case you’re automating things…

# ON MOTION DETECTED
- id: ifttt_wyzecam_living_room_motion
  alias: "Webhook: IFTTT > Living Room Motion"
  trigger:
    - platform: event
      event_type: ifttt_webhook_received
  condition:
    - condition: template
      value_template: "{{ trigger.event.data.action == 'living_room_motion' }}"
  action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.living_room_motion
    - service: timer.cancel
      entity_id: timer.living_room_timer
    - service: timer.start
      entity_id: timer.living_room_timer
- id: living_room_turn_off_motion_sensor_if_timer_completes
  alias: "Motion Timer: Turn Off Motion Sensor if Timer Completes for LivingRoom"
  description: ''
  trigger:
  - event_data:
      entity_id: timer.living_room_timer
    event_type: timer.finished
    platform: event
  condition: []
  action:
  - data:
      entity_id:
        - input_boolean.living_room_motion
    service: input_boolean.turn_off
# binary_sensor.living_room_motion
- platform: template
  sensors:
    living_room_motion:
      friendly_name: Living Room Motion
      value_template: >
        {{ is_state("input_boolean.living_room_motion","on") }}
# input_boolean.living_room_motion
living_room_motion:
  name: Living Room Motion
  initial: off
  icon: mdi:walk

Further Reading on Timers:

When calling the start service on a timer that is already running, it resets the duration it will need to finish and restart the timer without triggering a canceled or finished event. This, for example, makes it easy to create timed lights that get triggered by motion. Starting a timer triggers a started event unless the timer is paused, in that case, it triggers a restarted event.

^ this isn’t exactly true (in the docs)… you must call timer.cancel first.

1 Like

Are you able to share your applet?
Drive there are more limited free applets if you share it would it count as a free applet?

I’ve used home assistant a whole but am new to IFTTT and am only using it for Wyze Cam.

webhooks is not free anymore on IFTTT, so one workaround is to use a Kasa smart plug as the “hook” instead.

In IFTTT, use Wyze cam motion to turn on a Kasa smart plug. Both Wyze and Kasa services are free to be connected on IFTTT.

Then in HA, automations will trigger on the Kasa smart plug turning on, whenever motion is detected on the Wyze cam. Add one automation to turn off the Kasa smart plug, so that it can be triggered again by the IFTTT applet.

The downside is that you have to keep the Kasa smart plug plugged into an outlet.