Light switch through motion and switch, with timer resetted by timer

Hi,

I have a motion sensor in my toilet, and the light and ventilation after a Shelly switch behind the conventional light switch…

I am very new to HA, and just bought the motion sensor and raspberry PI last week.

What I want to have set up:
A) When someone manually switches on the switch Light → activate the light, for 10 minutes (for ventilation purposes, and because my kids keep forgetting to switch the light off)
B) Parrallel to A, the light also has to be switched on through the motion detector, without having to use the switch, also starting the timer for 10’.
C) In combination with A & B, the timer has to be reset if the motion detector detects motion during one of the 10’ runs.This is particularly handy when someone enters the toilet with the light on but with the timer at the end of its run, to avoid getting in the dark.

A & B I have managed to program with to automations as seen below. Where triggering B, also triggers A and sets the timer. But C I am struggling to integrate. I have installed eEntity controller via HACS but my YAML knowledge is not yet enough to programm this?
Any thoughts on how to code? Or does anyone has a tip for an extensive visual editor which can help me with more difficult automations?

Much appreciated in advance!

Automatisation A=

alias: toilet_switch_off_10minutes
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.shelly1_40f520016bda
    to: "on"
    for:
      hours: 0
      minutes: 10
      seconds: 25
condition: []
action:
  - service: switch.turn_off
    data: {}
    entity_id: switch.shelly1_40f520016bda
mode: single

Automatisation B=

alias: Toilet light on through motion
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 6827037dad6dd6817cc889a4d5d8e59a
    entity_id: c9243c6ce23330f53b6239500f96d844
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 0
condition: []
action:
  - service: switch.turn_on
    data: {}
    target:
      device_id: 82477961aeda7b644a171ad88ef24470
mode: single

One approach could be to create one automation with both A and B as trigger (it then responds to A or B), and switching the switch on for 10 minutes as action (switch on → wait for 10 minutes → switch off).
When you then set the automation mode to restart (instead of single) you also tackle C.

Personally I never used this, but Node-RED might be what you are looking for.

1 Like

node-red is how I approached the problem. My use case is slightly different, I wanted motion triggered lighting but a way to “lock on” the light if needed, using countdown-3000 (this allows for resetting the counter) I’ve got the motion state wired into the counter to start / reset as needed and set a “triggered by motion” flag (helper). However it’ll only set that if the light is off.

Motion clear will start the timer to turn off, but only actually fire if the motion flag is true.

There’s a separate but linked flow which triggers on light off, starts a 10 second timer, clears the motion flag, this gives me 10 seconds grace to turn off and turn on the light, which blocks the motion flow as the light is already on.

Put another way, go play with node-red, use helpers / injection nodes as placeholders for the motion sensor & switch to get the logic right before rolling out.

1 Like

For what it’s worth, my automation, the “meta” is a fake motion sensor which is based off two door sensors and one motion, presented as a single element which is clear only if all three are clear.

[{"id":"edac509289975c49","type":"subflow","name":"Automation & CoolDown","info":"","category":"","in":[{"x":40,"y":40,"wires":[{"id":"0c3887d5df7bd05a"}]}],"out":[{"x":360,"y":220,"wires":[{"id":"935e9bcb0d45fc20","port":0}]}],"env":[],"meta":{},"color":"#DDAA99"},{"id":"ab6df0564cd2118d","type":"api-current-state","z":"edac509289975c49","name":"Automation Check","server":"116501d2.cc5c6e","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_boolean.enable_automations","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"PIRState","valueType":"flow"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":210,"y":100,"wires":[["111d3f25773dfd82"],[]]},{"id":"111d3f25773dfd82","type":"api-current-state","z":"edac509289975c49","name":"Cooldown is off","server":"116501d2.cc5c6e","version":3,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_boolean.conservatory_switch_cooldown","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"PIRState","valueType":"flow"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":200,"y":160,"wires":[["935e9bcb0d45fc20"],[]]},{"id":"0c3887d5df7bd05a","type":"function","z":"edac509289975c49","name":"Store PIR State","func":"flow.set(\"SavedState\", msg.payload)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":200,"y":40,"wires":[["ab6df0564cd2118d"]]},{"id":"935e9bcb0d45fc20","type":"change","z":"edac509289975c49","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"SavedState","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":200,"y":220,"wires":[[]]},{"id":"239445065111a8ca","type":"server-state-changed","z":"1aeedfc25043cc75","name":"Conservatory Human \"Off\"","server":"116501d2.cc5c6e","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.conservatory","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":150,"y":1200,"wires":[["845a03b627afe8e1"]]},{"id":"80e4f3d3cf0c0051","type":"api-call-service","z":"1aeedfc25043cc75","name":"Cooldown On","server":"116501d2.cc5c6e","version":5,"debugenabled":false,"domain":"input_boolean","service":"turn_on","areaId":[],"deviceId":[],"entityId":["input_boolean.conservatory_switch_cooldown"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":500,"y":1200,"wires":[["8228952988b30a44","b472b0ec82ac1069"]]},{"id":"c127c2d4eb5cce0f","type":"api-call-service","z":"1aeedfc25043cc75","name":"Cooldown Off","server":"116501d2.cc5c6e","version":5,"debugenabled":false,"domain":"input_boolean","service":"turn_off","areaId":[],"deviceId":[],"entityId":["input_boolean.conservatory_switch_cooldown"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":660,"y":1260,"wires":[[]]},{"id":"845a03b627afe8e1","type":"switch","z":"1aeedfc25043cc75","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":350,"y":1200,"wires":[[],["80e4f3d3cf0c0051"]]},{"id":"8228952988b30a44","type":"delay","z":"1aeedfc25043cc75","name":"","pauseType":"delay","timeout":"10","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":480,"y":1260,"wires":[["c127c2d4eb5cce0f"]]},{"id":"2949c358d43e9f5d","type":"switch","z":"1aeedfc25043cc75","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":190,"y":1040,"wires":[["5a394cd64209270e","aaed1c9d641442c7"],["408d25f132382913"]]},{"id":"09045881a632b4b2","type":"api-current-state","z":"1aeedfc25043cc75","name":"Switch must be off","server":"116501d2.cc5c6e","version":3,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","entity_id":"switch.sonoff_zbminil2_switch","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":690,"y":960,"wires":[["9125d57672677dc7"],[]]},{"id":"5a394cd64209270e","type":"api-current-state","z":"1aeedfc25043cc75","name":"Is daylight OK?","server":"116501d2.cc5c6e","version":3,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_boolean.lux_tracker","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entity"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":680,"y":900,"wires":[["09045881a632b4b2"],[]]},{"id":"9125d57672677dc7","type":"api-call-service","z":"1aeedfc25043cc75","name":"TriggeredbyMotion True","server":"116501d2.cc5c6e","version":5,"debugenabled":false,"domain":"input_boolean","service":"turn_on","areaId":[],"deviceId":[],"entityId":["input_boolean.conservatorylight_triggered_by_motion"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":970,"y":900,"wires":[["95f3cb08457041eb"]]},{"id":"95f3cb08457041eb","type":"api-call-service","z":"1aeedfc25043cc75","name":"Lights on","server":"116501d2.cc5c6e","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.conservatory"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":940,"y":960,"wires":[[]]},{"id":"9c9b16cbc7d76bf4","type":"api-call-service","z":"1aeedfc25043cc75","name":"Lights off","server":"116501d2.cc5c6e","version":5,"debugenabled":false,"domain":"light","service":"turn_off","areaId":[],"deviceId":[],"entityId":["light.conservatory"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1100,"y":1260,"wires":[[]]},{"id":"b472b0ec82ac1069","type":"api-call-service","z":"1aeedfc25043cc75","name":"TriggeredbyMotion False","server":"116501d2.cc5c6e","version":5,"debugenabled":false,"domain":"input_boolean","service":"turn_off","areaId":[],"deviceId":[],"entityId":["input_boolean.conservatorylight_triggered_by_motion"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1150,"y":1200,"wires":[["9c9b16cbc7d76bf4"]]},{"id":"e5bf372e7681fc51","type":"api-current-state","z":"1aeedfc25043cc75","name":"TriggeredbyMotion ON","server":"116501d2.cc5c6e","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_boolean.conservatorylight_triggered_by_motion","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entity"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1140,"y":1080,"wires":[["b472b0ec82ac1069"],[]]},{"id":"8b7ef1b5c0a608c9","type":"comment","z":"1aeedfc25043cc75","name":"Conservatory Lighting","info":"","x":140,"y":820,"wires":[]},{"id":"3e6256a2e9d86e5d","type":"server-state-changed","z":"1aeedfc25043cc75","name":"Conservatory Meta","server":"116501d2.cc5c6e","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.conservatory_motion_meta","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":130,"y":860,"wires":[["8b9dc361b7a4db4a"]]},{"id":"8b9dc361b7a4db4a","type":"subflow:edac509289975c49","z":"1aeedfc25043cc75","name":"Trigger Check","x":340,"y":880,"wires":[["2949c358d43e9f5d"]]},{"id":"6f9468245bf18ff1","type":"api-current-state","z":"1aeedfc25043cc75","name":"Conservatory Meta","server":"116501d2.cc5c6e","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_boolean.conservatory_motion_meta","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":890,"y":1080,"wires":[[],["e5bf372e7681fc51"]]},{"id":"aaed1c9d641442c7","type":"change","z":"1aeedfc25043cc75","name":"STOP","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":1040,"wires":[["959549ea1d5a6ee7"]]},{"id":"90f236d77b660d02","type":"change","z":"1aeedfc25043cc75","name":"START","rules":[{"t":"set","p":"payload","pt":"msg","to":"10","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":1100,"wires":[["959549ea1d5a6ee7"]]},{"id":"959549ea1d5a6ee7","type":"countdown","z":"1aeedfc25043cc75","name":"","topic":"","payloadTimerStart":"true","payloadTimerStartType":"bool","payloadTimerStop":"false","payloadTimerStopType":"bool","timer":"180","resetWhileRunning":false,"setTimeToNewWhileRunning":true,"startCountdownOnControlMessage":false,"minuteCounter":false,"x":530,"y":1040,"wires":[["5b9c32a68f335a07"],["cd527a788f250c63"]]},{"id":"cd527a788f250c63","type":"change","z":"1aeedfc25043cc75","d":true,"name":"Remap","rules":[{"t":"change","p":"payload","pt":"msg","from":"^(\\d*)\\ sec$","fromt":"re","to":"$1","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"$number(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":1140,"wires":[["7a627fa592b875eb"]]},{"id":"5b9c32a68f335a07","type":"switch","z":"1aeedfc25043cc75","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":730,"y":1080,"wires":[[],["6f9468245bf18ff1"]]},{"id":"7a627fa592b875eb","type":"ha-number","z":"1aeedfc25043cc75","name":"light_timer_conservatory","version":1,"debugenabled":false,"inputs":1,"outputs":1,"entityConfig":"7451c6ec963b0dd5","mode":"set","value":"payload","valueType":"msg","outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"value"},{"property":"previousValue","propertyType":"msg","value":"","valueType":"previousValue"}],"x":870,"y":1140,"wires":[[]]},{"id":"408d25f132382913","type":"api-current-state","z":"1aeedfc25043cc75","name":"Light must be on","server":"116501d2.cc5c6e","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.conservatory","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":150,"y":1100,"wires":[["90f236d77b660d02"],[]]},{"id":"a87e87d731365404","type":"server-state-changed","z":"1aeedfc25043cc75","name":"Conservatory Motion","server":"116501d2.cc5c6e","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.conservatory","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":130,"y":920,"wires":[["8b9dc361b7a4db4a"]]},{"id":"116501d2.cc5c6e","type":"server","name":"Home Assistant","addon":true},{"id":"7451c6ec963b0dd5","type":"ha-entity-config","server":"116501d2.cc5c6e","deviceConfig":"","name":"light_timer_conservatory","version":"6","entityType":"number","haConfig":[{"property":"name","value":"light_timer_conservatory"},{"property":"icon","value":""},{"property":"entity_category","value":""},{"property":"entity_picture","value":""},{"property":"device_class","value":""},{"property":"unit_of_measurement","value":""},{"property":"min_value","value":""},{"property":"max_value","value":""},{"property":"step_value","value":""},{"property":"mode","value":"auto"}],"resend":false,"debugEnabled":false}]

Have a look at Entity Controller from HACS - it will do what you want in half a dozen lines. Here’s mine:

  - bathroom_light:                               
      sensor: 
      - binary_sensor.bathroom_motion_sensor_motion  
      entities: 
      - light.bathroom_1
      delay: 600
      block_timeout: 600

If the motion sensor detects movement, the light comes on for ten minutes. (If movement is detected in that time the period is extended for another ten minutes.) If the light is turned on or off manually it stays in that state for ten minutes, then goes off if there is no movement.

PS… Avoid device triggers if you can…

1 Like

I downloaded entity controller indeed a couple of days ago, but I haven’t ‘grasped’ how to write and where to put the code.

I copied this code (based on yours) in my configuration.yaml, but it doens’t seem to work

wclicht

  • bathroom_light:
    sensor:
    - binary_sensor.lumi_lumi_motion_ac02_motion
    entities:
    - switch.shelly1_40f520016bda
    - to: “on”
    delay: 600
    block_timeout: 600

Sorry, my fault - it should begin with entity_controller:

entity_controller:
  - bathroom_light:                               
      sensor: 
      - binary_sensor.bathroom_motion_sensor_motion  
      entities: 
      - light.bathroom_1
      delay: 600
      block_timeout: 600

You only need entity_controller once in your configuration.yaml, followed by all the lights, indented as above. Then do a restart.

Incidentally, when you paste yaml into a post, you need to use preformatted text (</> in the toolbar). This preserves the indentations, which are quite important.

1 Like

thx,

This did the thrick

alias: Nieuwe automatisering
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.shelly1_40f520016bda
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.lumi_lumi_motion_ac02_motion
    to: "on"
condition: []
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.shelly1_40f520016bda
  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.shelly1_40f520016bda
mode: restart

Hmm… are you sure it is working OK like this?
You are using the same entity switch.shelly1_40f520016bda both as a trigger and in the action to turn the light on and off. That can’t be right?

This is a Shelly 1, right?
I also have Shelly 1 devices to control lights, but these have separate entities to be used for the trigger and for the action.
For instance for my Shelly 1 SH02 I have a binary_sensor.sh02_input entity that can be used as trigger (which is the physical wall switch), and a light.sh02 entity (which is the relay that controls the light).
Your switch.shelly1_... entity probably is the relay, but don’t you have a binary_sensor_shelly1... as well, to be used as trigger?

1 Like