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!
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.
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).
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.
@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 …
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).
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.
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:
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…
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.
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).
Now the only thing I have to do is get my installation back on the road .
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.