Time-dependend, Motion-activated lights. Barely working

Hi guys!

I run Home Assistant on a Raspberry Pi in combination with a Zigbee stick and loads of Ikea lights and motion sensors, and some aquara door sensors thrown in. I’ve been running the setup for quite some time, but without much added smarts. I’m changing this by automating my home step by step…

I’ve been really struggeling to get Home Assistant do what I want it to do: I failed to modify and get the automations I found online, or the automations just don’t do exactly what I want them to do…

Now, I’m no newbie to coding, but then I’m talking C-type languages If then else and a whole lot more… But the Hass language has really got me confused…

Long story short, my first big automation is this:
I want 2 lights to turn on, when motion is detected. But between 7:00 and 19:00 I want it to be brighter then between 1900 and 700. After 3 minutes the lights should go off.
The code below works (as of now). But I’m very much aware that this is not the way to go…
It relies on 3 scenes where the light intensity of the 2 bulbs in question is either set to 1%, 100% or off…

- id: '1661194722124'
  alias: DIY Motion activated lights gang Avond
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.beneden_gang_motion_on_off
    - binary_sensor.boven_gang_motion_on_off
    from: 'off'
    to: 'on'
    for:
      hours: 0
      minutes: 0
      seconds: 0
  condition:
  - condition: time
    before: 07:00:00
    after: '19:00:00'
    weekday:
    - sun
    - sat
    - thu
    - fri
    - wed
    - tue
    - mon
  action:
  - service: scene.turn_on
    target:
      entity_id: scene.gang_s_avonds
    metadata: {}
  mode: single
- id: '1661197191194'
  alias: DIY Motion activated lights gang Ochtend
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.beneden_gang_motion_on_off
    - binary_sensor.boven_gang_motion_on_off
    from: 'off'
    to: 'on'
    for:
      hours: 0
      minutes: 0
      seconds: 0
  condition:
  - condition: time
    before: '19:00:00'
    after: 07:00:00
    weekday:
    - sun
    - sat
    - thu
    - fri
    - wed
    - tue
    - mon
  action:
  - service: scene.turn_on
    target:
      entity_id: scene.gang_s_ochtends
    metadata: {}
  mode: single
- id: '1661197292370'
  alias: Lampen gang uit na 3 minuten
  description: ''
  trigger:
  - platform: device
    type: turned_on
    device_id: f5bd06d461a75408f4399c6da340ca78
    entity_id: light.beneden_gang_lamp_light
    domain: light
    for:
      hours: 0
      minutes: 3
      seconds: 0
  condition: []
  action:
  - service: scene.turn_on
    target:
      entity_id: scene.new_scene
    metadata: {}
  mode: single

I’m not quite done with this automation either;
I want to add the fact that one light should be turned up to 100% for 5 minutes when a door opens (magnetic on_off sensor).
But that can wait for now: I want to wrap my head around the basics before going even deeper and maybe confusing myself even more…

Thank you for your consideration, help in the matter or general advice!

Couple of quick comments:

As you have the same triggers for the first 2 automations then you could create this as one automation and then under action section use the choose function and put the first condition and action under the first choose and the 2nd automations conditions and action under the 2nd choose.

Also if you are only setting 2 lights why not just call the light turn on service and set the via the automation, no need for a scene in this case.

Similarly it looks like you are using a scene to turn them off? Again you can simply call the light turn off action directly in the automation.

Edit: Also I notice the 3rd automation turns the lights off (I assume) following the light being on for 3 mins, but what happens if you are still moving ? At the moment the light will simply turn off after 3 mins regardless, is this what you wanted? Maybe consider changing this to no motion for 3 mins = turn lights off.

1 Like

Ok, now i’m pretty sure this has to do with indentation…

[edit:]

I’m not -ever- going to get out of this… The indentation rules are so ridiculously dificult and unclear. If you take another script, a person may claim it work for them but then you see that they have a hyphen in fron of some lines, and if I do that, that’s opening the gate to a whole other dimention of mess…

However, this is how far I got so far:

Logger: homeassistant.config
Source: config.py:455
First occurred: 3:00:13 PM (5 occurrences)
Last logged: 3:03:43 PM

Invalid config for [automation]: required key not provided @ data[‘action’]. Got None. (See /config/configuration.yaml, line 9).

- id: '1661194722124'
  alias: DIY Motion activated lights gang Avond
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.beneden_gang_motion_on_off
    - binary_sensor.boven_gang_motion_on_off
    to: 'on'
    for:
      hours: 0
      minutes: 3
      seconds: 0
      milliseconds: 0
    action:
    - choose:
      #EVENING
      - conditions:
        - condition: time
          after: '19:00:00'
          before: '07:00:00'
        squence:
        - service: scene.turn_on
          target:
          entity_id: scene.gang_s_avonds
      #EVENING
      - conditions:
        - condition: time
          after: '07:00:00'
          before: '19:00:00'
        squence:
        - service: scene.turn_on
          target:
          entity_id: scene.gang_s_ochtends
    mode: single

but that does -not- work… At all…

Replace squence: with sequence:

Indent entity_id: another two spaces but only for the scene.turn_on service calls.


Are you sure you want the State Trigger to use the for option set at 3 minutes? It means the automation won’t trigger until it detects movement for a minimum of 3 consecutive minutes. A person will need to move around for three minutes before the scene is turned on.

1 Like

@Taras: thank you for the hints. Sequence was a dead giveaway. But the spaces realy bug me. It still doesn’t work and nearly makes me run to the store to buy myself a smartthings hub :sweat_smile:
I Just don’t get it. The learning curve is so steep, and I cannot find a decent starting guide. Everything is explained on itself and goes too deep too fast imho.

I’m still stuck on:

Logger: homeassistant.config
Source: config.py:455
First occurred: 3:47:22 PM (1 occurrences)
Last logged: 3:47:22 PM

Invalid config for [automation]: required key not provided @ data[‘action’]. Got None. (See /config/configuration.yaml, line 9).

With this code:

- id: '1661194722124'
  alias: DIY Motion activated lights gang Avond
  description: ''
  trigger:
  - platform: state
    entity_id:
      - binary_sensor.beneden_gang_motion_on_off
      - binary_sensor.boven_gang_motion_on_off
    to: 'on'
    action:
      - choose:
        #EVENING
        - conditions:
            - condition: time
              after: '19:00:00'
              before: 07:00:00
          sequence:
            - service: scene.turn_on
              target:
                entity_id: scene.gang_s_avonds
        #EVENING
        - conditions:
            - condition: time
              after: 07:00:00
              before: '19:00:00'
          sequence:
            - service: scene.turn_on
              target:
                entity_id: scene.gang_s_ochtends
    mode: single
```<sup>Text</sup>

Everything starting with the action line is indented too much. Shift it all to the left by two spaces.

If you’re unfamiliar with an automation’s YAML syntax, I recommend you use the visual Automation Editor and not attempt to compose it manually. You can toggle the Automation Editor between visual and YAML modes to see the code it generates.

Alternately, use a text editor with a YAML checker that flags basic syntax errors.

1 Like

I dove right back in the editor because I was about to say “there is no choose, …”. But after careful reconsideration, there is…
I made the above automation with the visual editor, and it finaly got accepted:

- id: '1662819309956'
  alias: Lights hallway
  description: ''
  trigger:
  - platform: state
    entity_id:
    - group.motion_sensors_gang
    to: 'on'
  condition: []
  action:
  - choose:
    - conditions:
      - condition: time
        before: '19:00:00'
        after: 07:00:00
      sequence:
      - service: scene.turn_on
        target:
          entity_id: scene.gang_s_ochtends
        metadata: {}
    - conditions:
      - condition: time
        before: 07:00:00
        after: '19:00:00'
      sequence:
      - service: scene.turn_on
        target:
          entity_id: scene.gang_s_avonds
        metadata: {}
    default: []
  mode: restart

Thank you for all the help @123 ! I’m way further now. This will be tested from now. Hope that I turn of the lights te right way now.

- id: '1662819309956'
  alias: Lights hallway
  description: ''
  trigger:
  - platform: state
    entity_id:
    - group.motion_sensors_gang
    to: 'on'
  condition: []
  action:
  - choose:
    - conditions:
      - condition: time
        before: '19:00:00'
        after: 07:00:00
      sequence:
      - service: scene.turn_on
        target:
          entity_id: scene.gang_s_ochtends
        metadata: {}
    - conditions:
      - condition: time
        before: 07:00:00
        after: '19:00:00'
      sequence:
      - service: scene.turn_on
        target:
          entity_id: scene.gang_s_avonds
        metadata: {}
    default: []
  - wait_for_trigger:
    - platform: state
      entity_id:
      - group.motion_sensors_gang
      to: 'off'
  - type: turn_off
    device_id: f5bd06d461a75408f4399c6da340ca78
    entity_id: light.beneden_gang_lamp_light
    domain: light
  - type: turn_off
    device_id: 1499aae6648ce7034a60db1d081815f5
    entity_id: light.boven_gang_lamp_level_on_off
    domain: light
  mode: restart

[EDIT]
I just found out about trigger ID’s, I’m going to check this out and use this to turn the lights off…

1 Like

Not entirely there yet…
The visual editor made it easier, but the code seems not to do what I want:

- id: '1662819309956'
  alias: Lights hallway
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.hallway_motion_all
    id: Hallway-on
    to: 'On'
  - platform: state
    entity_id:
    - binary_sensor.hallway_motion_all
    id: Hallway-off
    to: 'Off'
    for:
      hours: 0
      minutes: 3
      seconds: 0
  condition: []
  action:
  - choose:
    - conditions:
      - condition: trigger
        id: Hallway-on
      - condition: time
        before: 07:00:00
        after: '19:00:00'
      sequence:
      - service: scene.turn_on
        target:
          entity_id: scene.gang_s_avonds
        metadata: {}
    - conditions:
      - condition: trigger
        id: Hallway-on
      - condition: time
        before: '19:00:00'
        after: 07:00:00
      sequence:
      - service: scene.turn_on
        target:
          entity_id: scene.gang_s_avonds
        metadata: {}
    - conditions:
      - condition: trigger
        id: Hallway-off
      sequence:
      - type: turn_off
        device_id: f5bd06d461a75408f4399c6da340ca78
        entity_id: light.hallway_light_downstairs_light
        domain: light
      - type: turn_off
        device_id: 1499aae6648ce7034a60db1d081815f5
        entity_id: light.hallway_light_upstairs_level_on_off
        domain: light
    default: []
  mode: single

I’d expect this code to turn on the lights when motion is detected, and based on the time the details of those lights are selected. And after 3 minutes without motion, the lights should go off…

The diagram shown in the debug view seems pretty much spot-on. But the lights aren’t turned on or off…

So… Apparently you should use on and off in stead of On and Off

Jeezzz. Hass is -really- picky!

You said you used the Visual Automation Editor to create the automation and it inserted On and Off in the YAML it generated?

That surprises me because although the selection list, in visual mode , shows On and Off in title-case, the actual YAML produced contains the chosen value in lower-case.

Visual mode (To field contains On):

YAML mode (to field contains on not On):
Screenshot_20220910-180513~2

Come on now, you said you are not a newbie to programming. NO programming language is case insensitive.

Well, I could rephrase that with inconsistent, if you please. When you look in history for example, the states are available, but there they are written in upper case.
Furthermore, I’m accustomed to C languages, there you’re not tossed to square one if you put one space out of place…
I stand with what I said. It’s immensely powerfull, but very finicky, and not easy to dive into because of that.

I don’t disagree. That’s why both the developer tools have always shown you the true state, and the auto complete shows the actual state. Arguably while the new automagical auto listing all known states feature of GUI is very handy, it does further push this issue because it too uses Initial Case.

Because C, unlike YAML and python, isn’t position-sensitive. However, C has its own set of syntax rules that, if unheeded, will produce errors (like a missing matching brace which is the functional equivalent of incorrect indentation in YAML or python). Anyway, comparing YAML to C is comparing apples to oranges; one’s a data-serialization language the other is a general programming language.

Home Assistant uses YAML like a form to organize information. After the “form” is confirmed to be properly structured by the YAML processor, it’s fed to the Jinja2 interpreter if any of the form’s fields contain Jinja2 templates. Ultimately, it’s processed by the python interpreter.

All this to say that what you’re seeing is just a means of sparing a user from composing scripts/automations directly in python (which, like YAML, is position-sensitive) or seeing their UI-based creations translated directly into python.

For many users, they don’t need to dabble with YAML and simply use the visual Automation and Script Editors. The editors store what you compose in the UI as YAML which can be easily shared with others via copy-paste (as opposed to sharing screenshots).

Thank you for all the help!

I’ve now got (had) a nicely finetuned version working for me:

alias: Lights - Hallway V2
description: |-
  Licht bij bewegingsdetectie (uurafhankelijk)
  Licht bij openen voordeur
trigger:
  - platform: state
    entity_id:
      - binary_sensor.hallway_motion_all
    to: "on"
    id: Motion_detected_true
  - platform: state
    entity_id:
      - binary_sensor.hallway_motion_all
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 3
    id: Motion_detected_false
  - platform: state
    entity_id:
      - binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_4
    to: "on"
    id: Frontdoor_opening
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Motion_detected_true
        sequence:
          - choose:
              - conditions:
                  - condition: time
                    before: "19:00:00"
                    after: "07:00:00"
                sequence:
                  - service: light.turn_on
                    data:
                      transition: 3
                      brightness: 255
                      kelvin: 2700
                    target:
                      entity_id: light.hallway_light_all
              - conditions:
                  - condition: time
                    before: "21:45:00"
                    after: "19:00:00"
                sequence:
                  - service: light.turn_on
                    data:
                      transition: 1
                      brightness_pct: 3
                      kelvin: 2700
                    target:
                      entity_id: light.hallway_light_all
              - conditions:
                  - condition: time
                    before: "07:00:00"
                    after: "21:45:00"
                sequence:
                  - service: light.turn_on
                    data:
                      transition: 3
                      color_temp: 500
                      brightness_pct: 1
                    target:
                      entity_id: light.hallway_light_downstairs_light
      - conditions:
          - condition: trigger
            id: Frontdoor_opening
        sequence:
          - service: light.turn_on
            data:
              transition: 3
              brightness_pct: 100
              kelvin: 2700
            target:
              entity_id: light.hallway_light_downstairs_light
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id: Motion_detected_false
              - condition: state
                entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_4
                state: "off"
        sequence:
          - service: light.turn_off
            data:
              transition: 5
            target:
              entity_id: light.hallway_light_all
    default: []
mode: single

Now the only thing I have to do is get my installation back on the road :roll_eyes:.

edit:

I have to mention that the 3 seconds delay I’ve put in for the motion sensors is taking into account the 3 minutes cooldown period my sensors have. Essentially putting the delay on 3 minutes and 3 seconds…

For future reference, it’s customary to assign the Solution tag to the first post that identifies the cause of the original problem and explains how to correct it. It’s meant to be a learning guide for other users who may have a similar question.

For more information refer to guideline 21 in the FAQ