Complex motion - light automation

Trying to convert my tuya to HA.

at night:

  • If motion, turn on light
  • Set brightness based on time - but only if turned on by automation
  • No motion for 1min, turn off - but only if turned on by automation

It sounds simple, but I have this quite a bit mess so far:

alias: Motion1
trigger:
  - type: motion
    platform: device
    entity_id: binary_sensor.upstairs_sensor
    domain: binary_sensor
    id: start
  - type: no_motion
    platform: device
    entity_id: binary_sensor.upstairs_sensor
    domain: binary_sensor
    for:
      hours: 0
      minutes: 1
      seconds: 30
    id: stop
condition:
  - condition: or
    conditions:
      - condition: sun
        before: sunrise
      - condition: sun
        after: sunset
        enabled: true
    enabled: true
action:
  - if:
      - condition: trigger
        id: start
      - condition: device
        type: is_off
        entity_id: light.library_module_light
        domain: light
    then:
      - if:
          - condition: time
            before: "22:00:00"
            after: "00:00:00"
        then:
          - type: turn_on
            entity_id: light.library_module_light
            domain: light
            brightness_pct: 35
        else:
          - type: turn_on
            entity_id: light.library_module_light
            domain: light
            brightness_pct: 15
      - delay:
          hours: 0
          minutes: 1
          seconds: 0
          milliseconds: 0
      - type: turn_off
        entity_id: light.library_module_light
        domain: light
  - if:
      - condition: trigger
        id: stop
    then:
      - if:
          - condition: numeric_state
            entity_id: light.library_module_light
            attribute: brightness
            below: "128"
        then:
          - type: turn_off
            entity_id: light.library_module_light
            domain: light
          - type: turn_off
            entity_id: light.socket_1
            domain: light
        enabled: true
    enabled: true
mode: restart

It works sometimes, other times the light is left on forever. Itā€™s got a crap hack to guess if itā€™s on due to automation or me.
I thought Iā€™d ask for help before making it any longer.

Iā€™d suggest setting up a simple timer (any length of time you like). Then make a couple simple automations (this is the way I do it):

If the timer is started, turn the light on.

If the timer is finished, turn the light off.

Then create an automation that when there is motion sensed, then just start the timer. (Even if the light is already on, the net effect of motion being sensed again will restart the timer!) This also resolves the issue of the lights going off while people are sitting still in the room (unless they sit motionless for sayb15 minutes if that is the length of your timer), but also guarantees they will go off.

Then I made mine a little fancier, by:

Add an ā€œinput_selectā€ entity to the dashboard that just creates a drop-down, with two values: ā€œEnabledā€ and ā€œDisabledā€. In my automations I ignore the motion if I have the automation ā€œDisabledā€.

Create an ā€œinput_numberā€ entity for the dashboard that works like a slider that you would then use to determine the timer length in seconds. Use that in the motion sensor automation when it starts the timer, to start it with the input_number as the duration.

The entity which represents the light switch in your dashboard, create automations that will simply start the timer when the switch is turned on, and cancel the timer when the switch is turned off.

After doing this for several rooms, to simplify my code I created one script which when it runs, accepts three entities as parameters: the input_select, the input_number, and the timer. It took me forever to figure out how to do this but once you have it done itā€™s bulletproof.

Then in all of my room lighting automations, I removed all of the redundant code and each automation just simply calls that one script. (Note the script is set as ā€œparallelā€ and ā€œ100ā€ (overkill) so the script can essentially be called by any number of automations at the same time.

It was a lot of trial and error but I can supply the code for how the automation calls the script with the three entity parameters, as well as all of the code for the script (which is just something like 10 lines!).

Thoughts?

1 Like

What I see is that if I turn the light on, then this triggers, the light gets turned off afterwards. I donā€™t want the automation to turn off a light I turned on.

Also, our motion sensor is quite dumb, if Iā€™m sitting on the floor reading, it doesnā€™t sense me, thus again turning my light off. And takes a lot of hand waving and doesnā€™t sense me still.
Thatā€™s the problem right now.

Please reread my entire first answer above - I edited it alot and added a lot to it! My motion sensors are pretty crappy as well. The elegance of my solution for example, is if you donā€™t move for 15 minutes, but the timer is set to 30 minutes, then itā€™s no issue because as long as it senses you moving within those 30 minutes, itā€™ll restart the timer with the lights still on! Some of my lights are actually smart outlets being used by table lamps, so in my case when I ā€œDisableā€ that automation, not only does it cancel the timer but also turns that smart outlet ON so people wonā€™t complain that they cannot turn on the lamp!

Also, I was starting to say that since my motion sensors are pretty crappy, although there is an integration for them, they also support IFTTT. So I have IFTTT webhook calls that when the motion is sensed, Even if the automation is not called by the integration, It is called by IFTTT. In that case I have the automation being called more than once to turn the light on, but that really doesnā€™t matter because all itā€™s doing is restarting the timerā€¦ How is HA connecting to your motion sensors? To improve the reliability of the motion sensed at least turning on the light, you can attack it from two angles like I didā€¦ lol

Lastly I have noticed that the motion sensors seem to work better if you are closer to them. Another idea is to just change to a better motion sensor - or put more of them in the same room and have them all call the same automations?

Are your motion sensors wireless? Is it possible the batteries are low or the WiFi connection for it is unstable?

FYI I had a weird issue early on where when motion was sensed, it would actually turn on a light in a DIFFERENT ROOM! And the issue was randomly changing from one motion sensor and one light to another! Someone on the HAvforum told me that the automation is relying not on the device Mac address but instead on itā€™s IP address! All of my motion sensors and light switches had automatically assigned IP addresses which were changing when my router was rebooted! I finally resolved that issue by assigning static (unchanging) ip addresses to each device. That wasnā€™t hard, just go into the router settings and look at every device and whatever IP address it has, just assign that IP address to that device so when your router reboots the same devices, including motion sensors, Will have the same IP addresses permanently. Maybe you have devices that are changing IP addresses!

Also in addition to my other answer, Please note that I did not realize you didnā€™t want the light to ever turn off on its own If you had manually turned it on. I think that oneā€™s pretty simpleā€¦ What you could do is have a hidden input variable that you set when the motion sensor starts the timer and have it set to a different value when you press the button to turn on the light and for your logic that is called when the timer ends you could look at that variable and depending upon its value then decide whether or not to turn the light offā€¦ essentially itā€™s just a variable that keeps track of what turned the light on. One other thing I have noticed is itā€™s much easier to automate things if your code is very short and itā€™s just triggered by very strategic things like the start or stop of a timer etcā€¦ I felt I should do it that way otherwise Iā€™d be reinventing the wheel with a million lines of logic and driving myself nuts lol I think with his home automation weā€™ve already all driven are each other nuts anywayā€¦

Thoughts on the variable that keeps track of what turned the light on, to use to determine whether or not to turn the light off on the timer ends?

It sounds more and more complicated. Iā€™m still very new to this. I struggle with syntax.

  • sounds like a few scripts that work together - how do you organise them, since you canā€™t group them in the gui. Iā€™m finding it more of a mess now.
  • where would this variable live?
  • my timer is 1min, itā€™s like a go to toilet light at night, so itā€™s annoying when it goes wrong

Donā€™t worry, everyone struggle at first! So letā€™s start simple! 1. Letā€™s create a hidden place to set text representing whether or not something was rturned on by an automation. Go into settings->Helper->Create Helper->Text and save that call it whatever you want. Then go into settings->entities and find that helper, double click on it to edit it - and in there you should be able to save it as ā€˜hiddenā€™. 2. Lets figure out how to set itā€™s value. Creat a couple of simple automations that will change the value of that variable. What would they be - once when you turn on the light and also when the motion sensor wouold turnj it on?

Thanks I appreciate this time youā€™re giving me. I created the helper.

Btw I heard of node-red some like, I havenā€™t touched it yet, would it be easier in there?

I use node red, but what is the definition of ā€˜easierā€™ ?
It looks simple; just connecting building blocks, but some blocks use java ā€¦

[{"id":"480e4fbb.d7002","type":"tab","label":"Motion","disabled":false,"info":""},{"id":"657089a.ec2ba78","type":"function","z":"480e4fbb.d7002","name":"Turn on 1%","func":"msg.payload = {\n    domain: \"light\",\n    service: \"turn_on\",\n    data: {\n        entity_id: \"light.light_hallway_upstairs\",\n        \"brightness_pct\": 1\n    }\n};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":670,"y":140,"wires":[["d67976c3.140b78","4e9c980c.3cdee8"]]},{"id":"4e9c980c.3cdee8","type":"api-call-service","z":"480e4fbb.d7002","name":"payload entity","server":"37d68302.2aabcc","version":5,"debugenabled":false,"domain":"homeassistant","service":"","areaId":[],"deviceId":[],"entityId":[],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1160,"y":580,"wires":[["c380196.54abde8"]]},{"id":"fe199ecf.000f4","type":"function","z":"480e4fbb.d7002","name":"Turn off","func":"msg.payload = {\n    domain: \"light\",\n    service: \"turn_off\",\n    data: {\n        entity_id: msg.data.entity_id,\n    }\n};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1160,"y":360,"wires":[["4e9c980c.3cdee8"]]},{"id":"d67976c3.140b78","type":"function","z":"480e4fbb.d7002","name":"Reset  3m Delay","func":"msg.delay = 180000;\nreturn [msg];","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":900,"y":120,"wires":[["ddef815f2eac0f31","90d337bb4f3ae369"]]},{"id":"20b47ca3.89f634","type":"server-state-changed","z":"480e4fbb.d7002","name":"","server":"37d68302.2aabcc","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.light_hallway_upstairs","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"","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":160,"y":80,"wires":[["d67976c3.140b78","b4fefb11.a9ff78"],["875f4aaf.ff7cf8"]]},{"id":"64a7da4a.74b264","type":"function","z":"480e4fbb.d7002","name":"Turn on 100%","func":"msg.payload = {\n    domain: \"light\",\n    service: \"turn_on\",\n    data: {\n        entity_id: \"light.light_hallway_upstairs\",\n        \"brightness_pct\": 100\n    }\n};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":180,"wires":[["d67976c3.140b78","4e9c980c.3cdee8"]]},{"id":"9bb684bd.eda288","type":"function","z":"480e4fbb.d7002","name":"Turn on 15%","func":"msg.payload = {\n    domain: \"light\",\n    service: \"turn_on\",\n    data: {\n        entity_id: \"light.light_hallway_downstairs\",\n        \"brightness_pct\": 15\n    }\n};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":670,"y":660,"wires":[["5ab4df3.29eaa2","4e9c980c.3cdee8"]]},{"id":"5ab4df3.29eaa2","type":"function","z":"480e4fbb.d7002","name":"Reset 3m Delay","func":"msg.delay = 180000;\nreturn [msg];","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":900,"y":640,"wires":[["b686e240db36962c"]]},{"id":"7026ec5c.25afe4","type":"time-range-switch","z":"480e4fbb.d7002","name":"","lat":"51.83125","lon":"4.33552","startTime":"sunset","endTime":"sunriseEnd","startOffset":"","endOffset":0,"x":440,"y":700,"wires":[["9bb684bd.eda288"],["94e27b0a64833bf7"]]},{"id":"a3fbdd0d.41e8e","type":"server-state-changed","z":"480e4fbb.d7002","name":"","server":"37d68302.2aabcc","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.light_hallway_downstairs","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"","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":170,"y":600,"wires":[["5ab4df3.29eaa2","b4fefb11.a9ff78"],["6dbb846c.9e82cc"]]},{"id":"b4fefb11.a9ff78","type":"switch","z":"480e4fbb.d7002","name":"Get brightness","property":"data.new_state.attributes.brightness","propertyType":"msg","rules":[{"t":"gt","v":"25","vt":"num"}],"checkall":"true","repair":true,"outputs":1,"x":1040,"y":300,"wires":[["b1c34edd.9809d"]]},{"id":"b1c34edd.9809d","type":"function","z":"480e4fbb.d7002","name":"Set brightness level","func":"msg.payload = {\n    domain: \"light\",\n    service: \"turn_on\",\n    data: {\n        entity_id: msg.data.entity_id,\n        \"brightness\":msg.data.new_state.attributes.brightness\n    }\n};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1230,"y":300,"wires":[["4e9c980c.3cdee8"]]},{"id":"875f4aaf.ff7cf8","type":"function","z":"480e4fbb.d7002","name":"Cancel","func":"msg.payload = \"stop\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":60,"wires":[["ddef815f2eac0f31"]]},{"id":"6dbb846c.9e82cc","type":"function","z":"480e4fbb.d7002","name":"Cancel","func":"msg.payload = \"stop\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":580,"wires":[["b686e240db36962c"]]},{"id":"c380196.54abde8","type":"debug","z":"480e4fbb.d7002","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1310,"y":580,"wires":[]},{"id":"58064783.3d6cd8","type":"server-state-changed","z":"480e4fbb.d7002","name":"PIR Hallw.Upstrs","server":"37d68302.2aabcc","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.motion_hallway_upstairs_occupancy","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":false,"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":100,"y":220,"wires":[[],[]]},{"id":"d1374b7.1eda3b8","type":"server-state-changed","z":"480e4fbb.d7002","name":"PIR Mst Bedroom","server":"37d68302.2aabcc","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.motion_master_bedroom_occupancy","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":false,"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":100,"y":160,"wires":[["daff13bb.15281"],[]]},{"id":"cc5ee022.06a45","type":"api-current-state","z":"480e4fbb.d7002","name":"Already On?","server":"37d68302.2aabcc","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.light_hallway_downstairs","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","x":290,"y":640,"wires":[["5ab4df3.29eaa2"],["7026ec5c.25afe4"]]},{"id":"daff13bb.15281","type":"api-current-state","z":"480e4fbb.d7002","name":"Already On?","server":"37d68302.2aabcc","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.light_hallway_upstairs","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","x":290,"y":120,"wires":[["d67976c3.140b78"],["d138811.3e1c58"]]},{"id":"94e27b0a64833bf7","type":"function","z":"480e4fbb.d7002","name":"Turn on 100%","func":"msg.payload = {\n    domain: \"light\",\n    service: \"turn_on\",\n    data: {\n        entity_id: \"light.light_hallway_downstairs\",\n        \"brightness_pct\": 100\n    }\n};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":700,"wires":[["4e9c980c.3cdee8","5ab4df3.29eaa2"]]},{"id":"2c9256b691bfbd68","type":"api-current-state","z":"480e4fbb.d7002","name":"Already On?","server":"37d68302.2aabcc","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"switch.bathroom_shower_light","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","x":290,"y":860,"wires":[["c11b2c44abfbda34"],["e7f26db07129dd40"]]},{"id":"134ff7500a1c6357","type":"function","z":"480e4fbb.d7002","name":"Switch On Daytime","func":"msg.payload = {\n    domain: \"switch\",\n    service: \"turn_on\",\n    data: {\n        entity_id: \"switch.bathroom_shower_light\"\n    }\n};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":690,"y":900,"wires":[["c11b2c44abfbda34","4e9c980c.3cdee8"]]},{"id":"bf4af0d6af5e98cf","type":"server-state-changed","z":"480e4fbb.d7002","name":"","server":"37d68302.2aabcc","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"switch.bathroom_shower_light","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"","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":170,"y":820,"wires":[["c11b2c44abfbda34"],["ce512a0168fba370"]]},{"id":"ce512a0168fba370","type":"function","z":"480e4fbb.d7002","name":"Cancel","func":"msg.payload = \"stop\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":800,"wires":[["58a3bbe4d5dcce3b"]]},{"id":"c11b2c44abfbda34","type":"function","z":"480e4fbb.d7002","name":"Reset 15m Delay","func":"msg.delay = 1500000;\nreturn [msg];","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":910,"y":840,"wires":[["58a3bbe4d5dcce3b"]]},{"id":"ddb053fdf17b2e0a","type":"function","z":"480e4fbb.d7002","name":"Turn off","func":"msg.payload = {\n    domain: \"switch\",\n    service: \"turn_off\",\n    data: {\n        entity_id: \"switch.bathroom_shower_light\"\n    }\n};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1180,"y":660,"wires":[["4e9c980c.3cdee8"]]},{"id":"58a3bbe4d5dcce3b","type":"stoptimer","z":"480e4fbb.d7002","duration":"15","units":"Minute","payloadtype":"num","payloadval":"0","name":"","x":900,"y":780,"wires":[["ddb053fdf17b2e0a"],[]]},{"id":"b686e240db36962c","type":"stoptimer","z":"480e4fbb.d7002","duration":"3","units":"Minute","payloadtype":"num","payloadval":"0","name":"","x":900,"y":580,"wires":[["fe199ecf.000f4"],[]]},{"id":"ddef815f2eac0f31","type":"stoptimer","z":"480e4fbb.d7002","duration":"3","units":"Minute","payloadtype":"num","payloadval":"0","name":"","x":900,"y":60,"wires":[["fe199ecf.000f4"],[]]},{"id":"e7f26db07129dd40","type":"time-range-switch","z":"480e4fbb.d7002","name":"","lat":"51.83125","lon":"4.33552","startTime":"22:00","endTime":"7:00","startOffset":"0","endOffset":"0","x":410,"y":920,"wires":[["f460b58c9d8decf4"],["134ff7500a1c6357"]]},{"id":"10ae7b3029837675","type":"link in","z":"480e4fbb.d7002","name":"Motion Office","links":["5e68004f5a41255a"],"x":165,"y":400,"wires":[["64c34644.a21288","284b1dfb5747d26b"]]},{"id":"940d3e9a31e7dffc","type":"link in","z":"480e4fbb.d7002","name":"Motion Hallway Downstairs","links":["247ff713ae34324e"],"x":165,"y":640,"wires":[["cc5ee022.06a45"]]},{"id":"c3123b22a9e8b99b","type":"link in","z":"480e4fbb.d7002","name":"Motion/Door Toilet Upstairs","links":["ea96cdb2de7642d8"],"x":165,"y":860,"wires":[["2c9256b691bfbd68"]]},{"id":"64c34644.a21288","type":"api-current-state","z":"480e4fbb.d7002","name":"Already On?","server":"37d68302.2aabcc","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.lights_office","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","x":290,"y":400,"wires":[[],["edc7966c.447588"]]},{"id":"edc7966c.447588","type":"function","z":"480e4fbb.d7002","name":"Turn on 100%","func":"msg.payload = {\n    domain: \"light\",\n    service: \"turn_on\",\n    data: {\n        entity_id: \"light.lights_office\",\n        \"brightness_pct\": 100\n    }\n};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":400,"wires":[["4e9c980c.3cdee8"]]},{"id":"5e7a9f12d6c7d909","type":"comment","z":"480e4fbb.d7002","name":"Light Office","info":"","x":90,"y":320,"wires":[]},{"id":"284b1dfb5747d26b","type":"api-call-service","z":"480e4fbb.d7002","name":"OfficeMotion","server":"37d68302.2aabcc","version":5,"debugenabled":false,"domain":"input_boolean","service":"turn_on","areaId":[],"deviceId":[],"entityId":["input_boolean.officemotion"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":130,"y":360,"wires":[["67fa80b7c2a5e655"]]},{"id":"079cee6cb70e5d4f","type":"api-call-service","z":"480e4fbb.d7002","name":"OfficeNoMotion","server":"37d68302.2aabcc","version":5,"debugenabled":false,"domain":"input_boolean","service":"turn_off","areaId":[],"deviceId":[],"entityId":["input_boolean.officemotion"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":700,"y":360,"wires":[[]]},{"id":"67fa80b7c2a5e655","type":"function","z":"480e4fbb.d7002","name":"Reset 120s Delay","func":"msg.delay =120000;\nreturn [msg];","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":360,"wires":[["e1f2af4ae546190b"]]},{"id":"e1f2af4ae546190b","type":"stoptimer","z":"480e4fbb.d7002","duration":"120","units":"Second","payloadtype":"num","payloadval":"0","name":"","x":510,"y":360,"wires":[["079cee6cb70e5d4f"],[]]},{"id":"f460b58c9d8decf4","type":"function","z":"480e4fbb.d7002","name":"Switch On NightTime","func":"msg.payload = {\n    domain: \"switch\",\n    service: \"turn_on\",\n    data: {\n        entity_id: \"switch.bathroom_secondary_light\"\n    }\n};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":940,"wires":[[]]},{"id":"d138811.3e1c58","type":"time-range-switch","z":"480e4fbb.d7002","name":"","lat":"51.83125","lon":"4.33552","startTime":"19:30","endTime":"sunriseEnd","startOffset":"0","endOffset":"1","x":430,"y":180,"wires":[["657089a.ec2ba78"],["64a7da4a.74b264"]]},{"id":"cf18ed8d0f6650d7","type":"link in","z":"480e4fbb.d7002","name":"Motion Hallway Upstairs","links":["2511e9a4a4f60833"],"x":165,"y":120,"wires":[["daff13bb.15281"]]},{"id":"ce613e68006fe462","type":"comment","z":"480e4fbb.d7002","name":"Light Hallway Upstairs","info":"","x":120,"y":40,"wires":[]},{"id":"eb2f558ba160e703","type":"comment","z":"480e4fbb.d7002","name":"Light Hallway Downstairs","info":"","x":130,"y":560,"wires":[]},{"id":"f50782160983d9d5","type":"comment","z":"480e4fbb.d7002","name":"Light Bathroom Upstairs","info":"","x":100,"y":780,"wires":[]},{"id":"90d337bb4f3ae369","type":"debug","z":"480e4fbb.d7002","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1130,"y":120,"wires":[]},{"id":"37d68302.2aabcc","type":"server","name":"Home Assistant","version":4,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m"}]

(this is the automation for 4 lights)

Is having java itself already easier to do some jobs? Iā€™m not fully understanding yet, but seems yaml variables is rather restricted vs normal programming variables.

When i started using HA, yaml was still very limited, hence I decided to use NR, as i would be able to use java to program (i seriously found the lack of programming an issue)

In the mean while, yaml majored a bit, so I guess it can be now, but i never bothered to rewrite the NR; it works for me and i know how to adept it when needed.

When i started using HA, yaml was still very limited, hence I decided to use NR, as i would be able to use java to program (i seriously found the lack of programming an issue)

In the mean while, yaml majored a bit, so I guess it can be done nowadays, but i never bothered to rewrite the NR; it works for me and i know how to adept it when needed.

I hear many say use yaml for simple, and NR for complex. But at what point does it get complex? What canā€™t you do easily in yaml? I donā€™t know. Both look a little messy I guess.

Iā€™d love to see the code for this please. I really like the idea of 1 automation for many similar things of same typeā€¦like an object model ā€¦ But that doesnā€™t appear to be the HASS way, so very interested in what you have done.

First let me say - donā€™t let the learning curve get you down, I am new to this too and thinking back on it the stuff I had to do to get to where I am with it took me maybe 150+ hours. I trashed my entire setup maybe 10 times while learning so before I got very deep into it I learned how to back up and restore everything. That may sound stupid as itā€™s usually easy to do, but for speed purposes I run my rpi on a 1tb SSD instead of an SD card. The Raspbian os automatically takes up the entire storage automatically when you use it, so even getting it to boot off of a spare SD card and then telling the sd card copier software to copy it to another device would constantly fail and I had to buy another 1tb ssd to use for the sd card copy and then bigger storage for the PC as the next step is to is the old win32 disk imager to burn the copy onto a drive on the PC, and that image is a 1gb file. Then, that file can be drastically shrunk in size, but only using an odd shrink utility on Ubuntu which I had to install as a vm on my windows PC copying a YouTube video, then that .img file which shrinks from 1tb to about 26mb can then be used by the windows application ā€œraspberry pi imagerā€ to recreate my setup quickly on any sd card or ssd. That took me a long time to learn and was amajor headache!

I also thought early on that I wanted the lights to just stay off and not be automated when I physically turned them off, but looking into it when I thought about it I said to myself then I want the lights to stay off next time I walk into the room? Why? I donā€™t need to turn them off anyway, the timer takes care of it!

Anyway, ok, so here is some of my code:

alias: Den Motion Detected (Lamp By Closet Automation)
description: ā€œā€
trigger:

  • type: motion
    platform: device
    device_id: 5ef8f43f23c1d09910584c8c6a3cb29c
    entity_id: binary_sensor.den_motion_sensor_motion
    domain: binary_sensor
    condition: []
    action:
  • service: script.motion_detected
    data:
    enablement_input_selector_entity: input_select.den_automation_lamp_by_closet_is
    timer_duration_entity: input_number.den_adjust_couch_lamp_by_closet_timer
    timer_entity: timer.den_couch_lamp_by_closet_timer
    mode: parallel

alias: Motion Detected
sequence:

  • condition: template
    value_template: ā€œ{{ is_state(enablement_input_selector_entity,ā€˜Enabledā€™) }}ā€
  • service: timer.start
    data:
    duration: ā€œ{{ (states(timer_duration_entity)|int*60) }}ā€
    target:
    entity_id: ā€œ{{ timer_entity }}ā€
    mode: parallel
    max: 100

I set up the text. And this

alias: Library light
description: ""
trigger:
  - platform: device
    type: turned_on
    device_id: .....
    entity_id: light.library_module_light
    domain: light
condition: []
action:
  - service: input_text.set_value
    data:
      value: switch
    target:
      entity_id: input_text.upstairs_light_triggered_by
mode: restart

then in the motion sensor

service: input_text.set_value
data:
  value: automation
target:
  entity_id: input_text.upstairs_light_triggered_by

Hello,

For the complex automations I also prefer to use NodeRed.

We have a row of ā€˜Billyā€™ wardrobes in the bedroom with spotlights on them.
I control the lighting with a fibaro dimmer and magnetic switches on the 6 doors connected to a single Visonic alarm contact. A second Visonic motionsensor in the corner of the room wil be used to detect motion. The motion sensor position is set that it will not see motion in our bed itself.

  • If one or more cabinets are opened during the day: lighting at 100%.

  • In the evening at 70%.

  • If movement is detected in the evening then lighting is at 30%.

  • At night at 12%.

Switching off:

  • During the day, the lights go to 0% when the cabinet doors are closed.

  • In the evening the lights first go from 70% to 30% when we leave the room and after 1 minute to 0%.

  • At night the lighting goes from 70% to 12% and after after 1 minute t to 0%.

Iā€™m still learning about NodeRed. Maybe my code is not optimal but it works.
Some things have been named in Dutch for my own clarity.

[{"id":"2c37d8.61780828","type":"comment","z":"34426c31.6087e4","name":"Kastverlichting","info":"","x":100,"y":40,"wires":[]},{"id":"57fc45ee.e9f1fc","type":"server-state-changed","z":"34426c31.6087e4","name":"Kastdeuren open","server":"2875e661.1a0c9a","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.visonic_z14","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":"","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":120,"y":100,"wires":[["48b30d6.4f953f4"]]},{"id":"3e173d20.488e82","type":"time-range-switch","z":"34426c31.6087e4","name":"Overdag","lat":"51.80424","lon":"5.80776","startTime":"07:10","endTime":"22:00","startOffset":"","endOffset":"","x":700,"y":300,"wires":[["4119959a.4430dc"],[]]},{"id":"3a3674f7.abe87c","type":"time-range-switch","z":"34426c31.6087e4","name":"Avond","lat":"51.80424","lon":"5.80776","startTime":"sunset","endTime":"22:00","startOffset":"","endOffset":0,"x":690,"y":420,"wires":[["d4565b9f.bd1538"],[]]},{"id":"979ca527.e440f8","type":"time-range-switch","z":"34426c31.6087e4","name":"Nacht","lat":"51.80424","lon":"5.80776","startTime":"22:00","endTime":"07:10","startOffset":0,"endOffset":0,"x":690,"y":480,"wires":[["eb3ade47.8c0c"],[]]},{"id":"961ae561.5c4a78","type":"change","z":"34426c31.6087e4","name":"Brightness 100%","rules":[{"t":"set","p":"payload","pt":"msg","to":"100","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1030,"y":300,"wires":[["3bb85947.2e7586"]]},{"id":"d4565b9f.bd1538","type":"change","z":"34426c31.6087e4","name":"Brightness 30%","rules":[{"t":"set","p":"payload","pt":"msg","to":"30","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":900,"y":420,"wires":[["3bb85947.2e7586"]]},{"id":"eb3ade47.8c0c","type":"change","z":"34426c31.6087e4","name":"Brightness 12%","rules":[{"t":"set","p":"payload","pt":"msg","to":"4","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":900,"y":480,"wires":[["3bb85947.2e7586"]]},{"id":"69873f4d.557ea","type":"server-state-changed","z":"34426c31.6087e4","name":"Beweging","server":"2875e661.1a0c9a","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.visonic_z13","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":"","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":100,"y":400,"wires":[["c55a2a3a.f22418"]]},{"id":"c55a2a3a.f22418","type":"time-range-switch","z":"34426c31.6087e4","name":"","lat":"51.80424","lon":"5.80776","startTime":"sunset","endTime":"sunrise","startOffset":"-60","endOffset":0,"x":300,"y":400,"wires":[["c6c308f0.97be08"],[]]},{"id":"c6c308f0.97be08","type":"switch","z":"34426c31.6087e4","name":"keuzeschakelaar ","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":510,"y":400,"wires":[["3e173d20.488e82","3a3674f7.abe87c","979ca527.e440f8","b0a1fbb0.b7d608"],["19394caa5a7c28a8"]]},{"id":"48b30d6.4f953f4","type":"switch","z":"34426c31.6087e4","name":"keuzeschakelaar","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":100,"wires":[["cb9c391a.8a5b78","2ced225f.e4286e","3e173d20.488e82"],["82220cf3.55a89","df26738c.1732c"]]},{"id":"8eef7eca.4ba0d","type":"inject","z":"34426c31.6087e4","name":"on","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":90,"y":180,"wires":[["48b30d6.4f953f4","346e15f.2b39bea"]]},{"id":"d7dd4ec2.726f6","type":"inject","z":"34426c31.6087e4","name":"off","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":90,"y":220,"wires":[["48b30d6.4f953f4"]]},{"id":"b2a442f2.2640a","type":"inject","z":"34426c31.6087e4","name":"on","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":90,"y":480,"wires":[["c55a2a3a.f22418"]]},{"id":"784d4f32.5e87d","type":"inject","z":"34426c31.6087e4","name":"off","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":90,"y":520,"wires":[["c55a2a3a.f22418"]]},{"id":"2ced225f.e4286e","type":"change","z":"34426c31.6087e4","name":"Stop","rules":[{"t":"set","p":"payload","pt":"msg","to":"stop","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":620,"wires":[["19394caa5a7c28a8"]]},{"id":"91fa230f.be15f","type":"change","z":"34426c31.6087e4","name":"Brightness 0%","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":620,"wires":[["3bb85947.2e7586"]]},{"id":"cb9c391a.8a5b78","type":"time-range-switch","z":"34426c31.6087e4","name":"Nacht","lat":"51.80424","lon":"5.80776","startTime":"22:00","endTime":"07:10","startOffset":0,"endOffset":0,"x":690,"y":360,"wires":[["4cf16a13.c1a9b4"],[]]},{"id":"4cf16a13.c1a9b4","type":"change","z":"34426c31.6087e4","name":"Brightness 70%","rules":[{"t":"set","p":"payload","pt":"msg","to":"70","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":900,"y":360,"wires":[["3bb85947.2e7586"]]},{"id":"82220cf3.55a89","type":"time-range-switch","z":"34426c31.6087e4","name":"","lat":"51.80424","lon":"5.80776","startTime":"sunrise","endTime":"sunset","startOffset":0,"endOffset":0,"x":300,"y":200,"wires":[["c6c308f0.97be08"],[]]},{"id":"df26738c.1732c","type":"change","z":"34426c31.6087e4","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"off","fromt":"str","to":"on","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":100,"wires":[["63162fa9e22f07c5"]]},{"id":"346e15f.2b39bea","type":"change","z":"34426c31.6087e4","name":"Stop","rules":[{"t":"set","p":"payload","pt":"msg","to":"stop","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":160,"wires":[["63162fa9e22f07c5"]]},{"id":"3bb85947.2e7586","type":"api-call-service","z":"34426c31.6087e4","name":"Lampen uit","server":"2875e661.1a0c9a","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.kledingkast_slaapkamer"],"data":"{\"brightness_pct\":\"{{payload}}\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1370,"y":400,"wires":[[]]},{"id":"4119959a.4430dc","type":"api-current-state","z":"34426c31.6087e4","name":"Kast open?","server":"2875e661.1a0c9a","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"binary_sensor.visonic_z14","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","x":850,"y":280,"wires":[["961ae561.5c4a78"],[]]},{"id":"b0a1fbb0.b7d608","type":"api-current-state","z":"34426c31.6087e4","name":"Kast open?","server":"2875e661.1a0c9a","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"binary_sensor.visonic_z14","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","x":130,"y":620,"wires":[["2ced225f.e4286e"],[]]},{"id":"63162fa9e22f07c5","type":"stoptimer-varidelay","z":"34426c31.6087e4","duration":"5","durationType":"num","units":"Second","payloadtype":"num","payloadval":"0","name":"","reporting":"last_minute_seconds","persist":false,"ignoretimerpass":false,"x":880,"y":100,"wires":[["c6c308f0.97be08"],[],[]]},{"id":"19394caa5a7c28a8","type":"stoptimer-varidelay","z":"34426c31.6087e4","duration":"60","durationType":"num","units":"Second","payloadtype":"num","payloadval":"0","name":"","reporting":"last_minute_seconds","persist":false,"ignoretimerpass":false,"x":510,"y":620,"wires":[["91fa230f.be15f"],[],[]]},{"id":"2875e661.1a0c9a","type":"server","name":"Home Assistant","version":4,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m"}]

I updated my previous reply FYI

Actually I have a problem. This always fires, I canā€™t tell a button press vs automation turning it on. Is there another way?

trigger:
  - platform: device
    type: turned_on
    device_id: .....
    entity_id: light.library_module_light
    domain: light

What button is used?