Automation 101? want 4-in-1 zwave sensor trigger a switch

Dear all:

I want to do something basic.

The set up is simple enough: I have a zooz 4-in-1 zwave sensor (temperature, humidity, brightness, and motion), and I have a zwave switch.

I want zwave switch be turn on for 10 minutes when

  1. it is dark
  2. motion is detected

how to do this kind of thing? any pointer will be much appreciated. I don’t even know what keyword I should be searching for.

Thanks

Harv

Motion is the event that will triggers the automation, so you’ll create an automation with a state trigger for the motion sensor. You only want the automation to fire when it’s dark, so you’ll have to add a state condition (maybe a numeric_state condition) to the automation. The action of the trigger should be to turn on the switch, have a ten minute delay, and then turn off the switch.

With that information you should be able to find the necessary info to create the automation in the documentation. Take a stab at it! If you get stuck one of us will be happy to help.

1 Like

For one of those, there’s a cookbook example to help you.

1 Like

got stuck very quickly.

first thing i tried is trying to put everything into automations.yaml file.

nothing worked. the fact that original automations.yaml file has this
[]
confused me.

So, i tried the next best thing, add "automation:" to my configuration.yaml file.

Logically, it is simple. only thing that i am not sure of is how to “make the light only turn on for 10 minutes and automatically turn off” So, I leaving that part of logic out first… trying to make sure motion sensor triggers the switch.

Here is my configuration file:

automation:
- alias: bathroom night light by motion
    trigger:
      - platform: numeric_state
        entity_id: sensor.zooz_zse40_4in1_sensor_burglar_2
        above: 5
    condition:
      - condition: numeric_state
        entity_id: sensor.zooz_zse40_4in1_sensor_luminance_2
        below: 10.0
    action:
      - service: switch.turn_on
        entity_id: switch.switch

(i am using zooz 4-in-1 sensor, the motion part has two values i can leverage, one of the value is simply “burglar” sensor which raise value from zero to 8 when it detect motion)

and I got stuck immediately. The error message is very cryptic:

mapping values are not allowed here

in this line:
trigger:

I have no idea what went wrong. Any idea?
thanks in advance

The problem is your indenting doesn’t reflect what’s in the docs. Try this instead - note that alias and trigger line up:

automation:
  - alias: bathroom night light by motion
    trigger:
      - platform: numeric_state
        entity_id: sensor.zooz_zse40_4in1_sensor_burglar_2
        above: 5
    condition:
      - condition: numeric_state
        entity_id: sensor.zooz_zse40_4in1_sensor_luminance_2
        below: 10.0
    action:
      - service: switch.turn_on
        entity_id: switch.switch

thank you so much, it worked!!!

Dear all:

I am making progress!

now, my next question is as follows:

If i just want the light to be turn on for 10 minutes when motion is detected, how to do that?

I was hoping that the same trigger can just initiate 1. turn on the light, 2. delay for 10 minutes, 3. turn off the light. This is something I came up with, it passed all the syntax check:

- alias: bathroom night light by motion
  trigger:
    - platform: numeric_state
      entity_id: sensor.zooz_zse40_4in1_sensor_burglar_2
      above: 5
  condition:
    - condition: numeric_state
      entity_id: sensor.zooz_zse40_4in1_sensor_luminance_2
      below: 10.0
  action:
    - service: switch.turn_on
      entity_id: switch.switch
    - delay: 10:05
    - service: switch.turn_off
      entity_id: switch.switch

the turn off part of the this automation script is not working. Any idea?

That should work, I’ve done similar - though I now use multiple automations, as in the cookbook link, since otherwise it’ll turn it off after 10 minutes, regardless.

Have you checked for errors in the log?

I think it’s working, it’s just going to take ten hours and five minutes. :grin:

2 Likes

LOL. thanks!!

ok, guys, I have another silly problem.

the zwave switch I got is pleasantly surprised in many ways (Hank so01) it actually has voltage and current measurement), but one thing I absolutely hate is that apparently it has no separation between “on” and “off.”

So,
service: switch.turn_on
and
service: switch.turn_off

are exact same call.

This complicated my script because I can’t simply call switch.turn_off after 10 minutes. I need to add more conditions

the original logic is as follows:

if motion detected   
  if dark
    turn on light
    delay 10 minute
    turn off light

the actual code, which almost worked, is following.
Note that lack of if/then statement make the code a lot more complicated than it should be:

- alias: bathroom night light by motion
  trigger:
    - platform: numeric_state
      entity_id: sensor.zooz_zse40_4in1_sensor_burglar_2
      above: 5
  condition:
    - condition: numeric_state
      entity_id: sensor.zooz_zse40_4in1_sensor_luminance_2
      below: 10.0
  action:
    - service: switch.turn_on
      entity_id: switch.switch
    - delay: 00:01:05
    - service: switch.turn_off
      entity_id: switch.switch

since this silly switch’s “on” and “off” are the same, I need to determine rather the light is already on or not (by looking at the power which the switch is consumed… a 4-watts light bulb).

if motion detected   
  if dark
    if light is off
      turn on light
      delay 10 minutes
      turn off light
  if light is already on
    delay 10 minutes
    turn off light

but apparently, i can’t just embed “condition” inside “action.”
here is the code i wrote which didn’t pass the syntax test:

- alias: bathroom night light by motion
  trigger:
    - platform: numeric_state
      entity_id: sensor.zooz_zse40_4in1_sensor_burglar_2
      above: 5
  condition:
    - condition: numeric_state
      entity_id: sensor.zooz_zse40_4in1_sensor_luminance_2
      below: 10.0
  action:
  - condition: or
    conditions:
      - condition: numeric_state
        entity_id: sensor.hank_hkzwso01_smart_plug_power
        blow: 3.9
        action:
        - service: switch.turn_on
          entity_id: switch.switch
        - delay: 00:10:00
        - service: switch.turn_off
          entity_id: switch.switch
      - conditions: numeric_state
        entity_id: sensor.hank_hkzwso01_smart_plug_power
        above: 3.9
        action:
        - delay: 00:10:00
        - service: switch.turn_off
          entity_id: switch.switch
  1. what went wrong with my code?
  2. and/or is there a more elegant way to do this? this seems to be a lot more complicated than it should for such simple scenario.

thanks in advance

I’m just looking at this quickly, but you CAN add conditions in the middle of scripts. So instead have your automation call a script you create called like script.ten_minute_light that does what you want.

my confusion is more basic than that.

the automation script have three major sections:

  1. trigger
  2. condition
  3. action

as long as there is only ONE action, I understand the structure completely.

I start confused when I need perform different actions based upon conditions.

can home automation script support such basic logical scenario?

trigger
condition
action:

  • condition1
    action1
  • condition2
    action2

based upon your advice, I will need two scripts:
First script is called “turn on light for 10 minutes.yaml”
Second script is called “turn off light after 10 minutes.yaml”

and depend upon rather light is already on or not, I need to perform two distinct actions, and I don’t know how to do that without breaking it up the automation script into completely separate script

1 Like

I’d also suggest creating a binary sensor for your switch, to tell you if it’s on or off, or maybe a template switch:

sensor:
  - platform: template
    sensors:
      hank_hkzwso01_smart_plug:
        value_template: "{{ states('sensor.hank_hkzwso01_smart_plug_power') > 3 }}"

You could then use that in the automation:

- alias: bathroom night light by motion
  trigger:
    - platform: numeric_state
      entity_id: sensor.zooz_zse40_4in1_sensor_burglar_2
      above: 5
  condition:
    - condition: numeric_state
      entity_id: sensor.zooz_zse40_4in1_sensor_luminance_2
      below: 10.0
    - condition: state
      entity_id: sensor.hank_hkzwso01_smart_plug
      state: 'off'
  action:
      - service: switch.turn_on
        entity_id: switch.switch
      - delay: 00:10:00
      - condition: state
        entity_id: sensor.hank_hkzwso01_smart_plug
        state: 'on'
      - service: switch.turn_off
        entity_id: switch.switch

For the template switch:

switch:
  - platform: template
    switches:
      hank_hkzwso01_smart_plug_power:
        value_template: "{{ states('sensor.hank_hkzwso01_smart_plug_power') > 3 }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.switch
        turn_off:
          service: switch.turn_on
          data:
            entity_id: switch.switch