Light with motion - Override with switch?

So I had a combination of scripts, switch template, input booleans and automation to have the ability to do this:

  • Light on/off with motion
  • Light on/off manually disables motion

Is there a simpler way that I could do this with NodeRED in one flow?
I feel I might still need input booleans or a switch template.

I’ve been trying a few things, but I think I might be over complicating things.
Any ideas please?

2 Likes

this is mine is this what your looking at doing?

1 Like

Similar mate, but I don’t want the switch to turn on the timer. I want the switch to just leave the light on until I turn it off.

So the motion will turn on the lights, and then they will turn off automatically after a set amount of time after the last motion.
However, if I turn the switch on manually, the auto on/off is basically disabled.

Add a state condition to monitor your switch after the timer and to halt if on.

image

1 Like

The problem I see with that, is that my switch entity is the same as what is being triggered to turn on.

I don’t have a switch and then a seperate light, for example.

So if I make a state condition to halt if switch on, it will permanently halt all off actions because the motion trigger will also set the switch to on.

I hope that made sense.

Is there any reason they need to have the same entity_id?

I dont either the initial input is the motion and the other is the switch. Ignore the node that says just switch as its just the node function and not the entity for my switch, its function is to switch the motion states to the correct flow.

Yes, they have to be the same entity.

Say its switch.lamp, for example. When I switch that on manually, its directly turning on the lamp.
The motion sensor, say binary_sensor.livingroom will trigger to turn on switch.lamp.
Its pretty easy to overcome if I use a different physical switch, like binary_sensor.physicalswitch to trigger the lamp.
But I want to be able to turn switch.lamp on from the frontend, or google, or whatever, and have it cancel the motion sensor turning off the light automatically.

You will need to feed the switch and the lamp into 2 switch nodes feeding the on and off outputs into the service call nodes. like this

This was my setup for yaml:

Scripts.yaml

######## MOTION ON
night_light_on:
  sequence:
    - condition: state
      entity_id: input_boolean.light_manually_on
      state: 'off'
    - service: switch.turn_on
      entity_id: switch.plug_158d0001a68b97
    - service: input_boolean.turn_on
      entity_id: input_boolean.light_on_by_automation

######## MOTION OFF
night_light_off:
  sequence:
    - condition: state
      entity_id: input_boolean.light_on_by_automation
      state: 'on'
    - service: switch.turn_off
      entity_id: switch.plug_158d0001a68b97
    - service: input_boolean.turn_off
      entity_id: input_boolean.light_on_by_automation
 
######## SIMPLE ON
living_room_lamp_auto_on:
  sequence:
    - service: input_boolean.turn_on
      entity_id: input_boolean.light_manually_on
    - service: switch.turn_on
      entity_id: switch.plug_158d0001a68b97 

######## SIMPLE OFF  
living_room_lamp_auto_off:
  sequence:
    - service: input_boolean.turn_off
      entity_id: input_boolean.light_manually_on
    - service: switch.turn_off
      entity_id: switch.plug_158d0001a68b97      
    - service: input_boolean.turn_off
      entity_id: input_boolean.light_on_by_automation

Switches.yaml:

    - platform: template
      switches:
        living_room_lamp_auto:
          friendly_name: Desk Lamp
          icon_template: mdi:lamp
          value_template: "{{ is_state('switch.plug_158d0001a68b97' , 'on') }}"
          turn_on:
            service: script.turn_on
            entity_id: script.living_room_lamp_auto_on
          turn_off:
            service: script.turn_on
            entity_id: script.living_room_lamp_auto_off

input_boolean.yaml:

light_manually_on:
  name: Check if lights were on manually
  initial: off
  icon: mdi:lightbulb-on-outline
light_on_by_automation:
  name: Check if lights were on by automation
  initial: off
  icon: mdi:lightbulb-on-outline

automations.yaml:

- alias: Study Lamp On with PIR
  action:
  - service: script.turn_on
    entity_id: script.night_light_on
  condition: []
  id: '1516324241625'
  trigger:
  - entity_id: binary_sensor.motion_sensor_158d00012cbb78
    from: 'off'
    platform: state
    to: 'on'
- alias: Study Lamp Off with PIR
  action:
  - service: script.turn_on
    entity_id: script.night_light_off
  condition: []
  id: '1516334059736'
  trigger:
  - entity_id: binary_sensor.motion_sensor_158d00012cbb78
    platform: state
    to: 'off'
    for:
      seconds: 5

With this flow, once there is no more motion, the light will still turn off.

So say I’m having a party, and I turn the lights on manually, if the room becomes vacant, I don’t want the lights to turn off.

sorry I deleted the state check for you light switch on state

so now the only time it will turn off the light is from the wall switch or if the wall switch is off already

Can you send me the export of this please?
I’ll give it a go!

Thanks man!

[{"id":"b5b1166e.bee4b8","type":"switch","z":"8b159f8e.80d86","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":340,"wires":[["923d1329.2bb7a","9b45d2dc.963c"],[]]},{"id":"9b45d2dc.963c","type":"stoptimer","z":"8b159f8e.80d86","duration":"2","units":"Minute","payloadtype":"num","payloadval":"0","name":"2min","x":530,"y":360,"wires":[["7d3b2a39.88a8d4"],[]]},{"id":"fa7265df.38b688","type":"api-call-service","z":"8b159f8e.80d86","name":"OFF","server":"e7c576e.5663a88","service_domain":"switch","service":"turn_off","data":"{\"entity_id\": \"switch.patio_light_switch\"}","mergecontext":"","x":1170,"y":400,"wires":[[]]},{"id":"80783dc7.5909f","type":"api-call-service","z":"8b159f8e.80d86","name":"ON","server":"e7c576e.5663a88","service_domain":"switch","service":"turn_on","data":"{\"entity_id\": \"switch.patio_light_switch\"}","mergecontext":"","x":950,"y":300,"wires":[[]]},{"id":"aa5c2bc7.37f3b8","type":"api-current-state","z":"8b159f8e.80d86","name":"Already On?","server":"e7c576e.5663a88","halt_if":"on","override_topic":true,"override_payload":true,"entity_id":"switch.patio_light_switch","x":750,"y":300,"wires":[["80783dc7.5909f"]]},{"id":"7e7b6d5f.8cdd14","type":"server-state-changed","z":"8b159f8e.80d86","name":"Patio Motion","server":"e7c576e.5663a88","entityidfilter":"binary_sensor.motion_sensor_158d000200ea5b","entityidfiltertype":"substring","haltifstate":"","x":170,"y":340,"wires":[["b5b1166e.bee4b8"]]},{"id":"923d1329.2bb7a","type":"time-range-switch","z":"8b159f8e.80d86","name":"After Sunset","lat":"","lon":"","startTime":"sunsetStart","endTime":"sunrise","startOffset":"","endOffset":"","x":550,"y":300,"wires":[["aa5c2bc7.37f3b8"],[]]},{"id":"81b16b0.44c6598","type":"server-state-changed","z":"8b159f8e.80d86","name":"Patio Switch Flipped On","server":"e7c576e.5663a88","entityidfilter":"switch.patio_light_switch","entityidfiltertype":"exact","haltifstate":"off","x":560,"y":440,"wires":[["67a4464f.39e998"]]},{"id":"7d3b2a39.88a8d4","type":"api-current-state","z":"8b159f8e.80d86","name":"Still Motion?","server":"e7c576e.5663a88","halt_if":"on","override_topic":true,"override_payload":true,"entity_id":"binary_sensor.motion_sensor_158d000200ea5b","x":750,"y":360,"wires":[["ffe9d6d8.fb7068"]]},{"id":"67a4464f.39e998","type":"switch","z":"8b159f8e.80d86","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":750,"y":440,"wires":[["80783dc7.5909f"],["fa7265df.38b688"]]},{"id":"ffe9d6d8.fb7068","type":"api-current-state","z":"8b159f8e.80d86","name":"Light Switch on Halt","server":"69a8824.8c4167c","halt_if":"","override_topic":true,"override_payload":true,"entity_id":"","x":960,"y":360,"wires":[["fa7265df.38b688"]]},{"id":"e7c576e.5663a88","type":"server","z":"","name":"HA - Automations","url":"http://localhost:8123","pass":"yourpassword"},{"id":"69a8824.8c4167c","type":"server","z":"","name":"HA - Main","url":"http://localhost:8123","pass":"yourpassword"}]

Actually, I still see a problem.

The action at the end “ON” is the same entity as “Patio switch flipped On” in my situation.

So the state check will always be on, even when its the motion triggered.

yes i see that now also the switch where i injected it is only performing its normal functions anyway lol so there was no point to adding it there. So the way around this is to trigger a boolean whenever the light is tuned on by motion and another when turned on by switch

try this

[{"id":"ef8f475e.c3c1d8","type":"switch","z":"5d954064.bd83f","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":370,"y":180,"wires":[["b3711be9.f8fc38","e30b6e95.ddb16"],[]]},{"id":"e30b6e95.ddb16","type":"stoptimer","z":"5d954064.bd83f","duration":"2","units":"Minute","payloadtype":"num","payloadval":"0","name":"2min","x":550,"y":200,"wires":[["268426f6.e8ee2a"],[]]},{"id":"b4df2e83.e3404","type":"api-call-service","z":"5d954064.bd83f","name":"OFF","server":"e7c576e.5663a88","service_domain":"switch","service":"turn_off","data":"{\"entity_id\": \"switch.patio_light_switch\"}","mergecontext":"","x":1190,"y":240,"wires":[[]]},{"id":"5b1f8d0f.db5354","type":"api-call-service","z":"5d954064.bd83f","name":"ON","server":"e7c576e.5663a88","service_domain":"switch","service":"turn_on","data":"{\"entity_id\": \"switch.patio_light_switch\"}","mergecontext":"","x":970,"y":140,"wires":[[]]},{"id":"98087de3.a6123","type":"api-current-state","z":"5d954064.bd83f","name":"Already On?","server":"e7c576e.5663a88","halt_if":"on","override_topic":true,"override_payload":true,"entity_id":"switch.patio_light_switch","x":770,"y":140,"wires":[["5b1f8d0f.db5354","db730f3.de43af"]]},{"id":"3fb751f5.dd87ce","type":"server-state-changed","z":"5d954064.bd83f","name":"Motion","server":"e7c576e.5663a88","entityidfilter":"binary_sensor.motion_sensor_158d000200ea5b","entityidfiltertype":"substring","haltifstate":"","x":170,"y":180,"wires":[["ef8f475e.c3c1d8"]]},{"id":"b3711be9.f8fc38","type":"time-range-switch","z":"5d954064.bd83f","name":"After Sunset","lat":"","lon":"","startTime":"sunsetStart","endTime":"sunrise","startOffset":"","endOffset":"","x":570,"y":140,"wires":[["98087de3.a6123"],[]]},{"id":"6f705601.f95878","type":"server-state-changed","z":"5d954064.bd83f","name":"Light Switch","server":"e7c576e.5663a88","entityidfilter":"switch.patio_light_switch","entityidfiltertype":"exact","haltifstate":"off","x":170,"y":380,"wires":[["79d1c17f.f0262"]]},{"id":"268426f6.e8ee2a","type":"api-current-state","z":"5d954064.bd83f","name":"Still Motion?","server":"e7c576e.5663a88","halt_if":"on","override_topic":true,"override_payload":true,"entity_id":"binary_sensor.motion_sensor_158d000200ea5b","x":770,"y":200,"wires":[["1758a84b.06d868"]]},{"id":"79d1c17f.f0262","type":"switch","z":"5d954064.bd83f","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":380,"wires":[["9c4a0635.0e76a8","a60247ab.328c78","a626b089.5738a"],["5ec1576.ca481a8","c03829c8.e16c38"]]},{"id":"1758a84b.06d868","type":"api-current-state","z":"5d954064.bd83f","name":"Light Switch on Halt","server":"69a8824.8c4167c","halt_if":"","override_topic":true,"override_payload":true,"entity_id":"","x":980,"y":200,"wires":[["b4df2e83.e3404","efa5710f.ce217"]]},{"id":"db730f3.de43af","type":"api-call-service","z":"5d954064.bd83f","name":"Boolean Motion On","server":"69a8824.8c4167c","service_domain":"input_boolean","service":"turn_on","data":"","mergecontext":"","x":1010,"y":100,"wires":[[]]},{"id":"efa5710f.ce217","type":"api-call-service","z":"5d954064.bd83f","name":"Boolean Motion Off","server":"69a8824.8c4167c","service_domain":"input_boolean","service":"turn_off","data":"","mergecontext":"","x":1230,"y":180,"wires":[[]]},{"id":"9c4a0635.0e76a8","type":"api-call-service","z":"5d954064.bd83f","name":"Boolean Light On","server":"69a8824.8c4167c","service_domain":"input_boolean","service":"turn_on","data":"","mergecontext":"","x":570,"y":300,"wires":[[]]},{"id":"a60247ab.328c78","type":"api-call-service","z":"5d954064.bd83f","name":"Light On","server":"69a8824.8c4167c","service_domain":"switch","service":"turn_on","data":"","mergecontext":"","x":540,"y":340,"wires":[[]]},{"id":"a626b089.5738a","type":"api-call-service","z":"5d954064.bd83f","name":"Boolean Motion Off","server":"69a8824.8c4167c","service_domain":"input_boolean","service":"turn_off","data":"","mergecontext":"","x":570,"y":380,"wires":[[]]},{"id":"5ec1576.ca481a8","type":"api-call-service","z":"5d954064.bd83f","name":"Light Off","server":"69a8824.8c4167c","service_domain":"switch","service":"turn_off","data":"","mergecontext":"","x":540,"y":420,"wires":[[]]},{"id":"c03829c8.e16c38","type":"api-call-service","z":"5d954064.bd83f","name":"Boolean Light Off","server":"69a8824.8c4167c","service_domain":"input_boolean","service":"turn_off","data":"","mergecontext":"","x":570,"y":460,"wires":[[]]},{"id":"e7c576e.5663a88","type":"server","z":"","name":"HA - Automations","url":"http://localhost:8123","pass":"yourpassword"},{"id":"69a8824.8c4167c","type":"server","z":"","name":"HA - Main","url":"http://localhost:8123","pass":"yourpassword"}]

Your light halt condition will be the state of your boolean light being “on”