New to YAML so bear with me, all help appreciated.
I have multiple cameras that I want to use as triggers in an automation. Depending on witch camera that triggered the automation, sun, and light status different actions should happen.
I have started with what I thought would be simple automation to learn how to do this in YAML but after a number of different versions and as many failures I have to humbly ask her for guidance.
YAML
The first condition is supposed to be valid for any following if statement. The two if statements (I have tried elif but no success on the second) have to conditions that is meant to be âandâ i.e. {{ triggered by trigger id AND light is off }}
This as I mentioned above only a start if I can get it to work I will have 5 cameras as triggers with different action as result.
Hoping for some enlightenment / Lennart
Thx @templeton_nash I might have to look at that but since Iâm used to if statements it would be easier for me to understand and translate from the LUA scripts I have in Domoticz but perhaps Choose is a better option. The UI option I do understand that it will work but most likely will hamper me later on when Iâm a bit more in to the YAML, anyhow thanks for suggestion.
I donât use the UI personally, however I agree with @templeton_nash because the UI generates yaml in the background, which you can copy and modify. The option might be a great way to get you started on your yaml journey
Depending on witch camera that triggered the automation, sun, and light status different actions should happen.
This depends a bit on your actual automation wishes BUT my big recommendation would be to separate these out into numerous small automation! This will be easier to maintain and more flexible to extend in future.
So instead of a set of convoluted nested conditions, just go with âif trigger camera 1 detected motion â when condition camera 2 no motion detected and sun is up â do specific action Aâ.
Followed by automations for âspecific action Bâ etc.
Generally this will help you to concentrate on the expected effects (âspecific actionâ) rather than dealing with all possible combinations of triggers and conditions, which most probably will change and become more complex over time. Let me know how it goes!!
I understand your desire to write this in yaml, and there is nothing wrong with that and learning and understanding the yaml will undoubtedly assist you moving forward.
However as others have said, it may be worth you initially using the Ui to start the process here and then edit it or at least look at it in yaml once you have a solid starting point.
I am not sure if any of it or all of it is a cut and paste issue, but pretty much all off your indentation is wrong here:
trigger:
- type: motion
## SHOULD BE
trigger:
- type: motion
Also:
- if:
condition: # THIS DOES NOT BELONG
- condition: trigger
id: cam_ka
- condition: device
....
....
## SHOULD BE
- if:
- condition: trigger
id: cam_ka
- condition: device
....
....
Start with the Ui and let it help you learn the correct yaml structure and spacing etc and then work from there would be my best advice.
EDIT: Also its advisable to use states rather than devices where possible.
If I understand your automation correctly, it does this:
When a cameraâs motion sensor turns on, and the sun is below the horizon, a corresponding switch is turned on (but only if itâs currently off).
Is that exactly what you want to do for all five cameras (i.e. turn on an associated switch)? Or will some of the five cameras require a different action?
@123 In this automation it is more or less the same for all 5 cameras but I have a couple of others that will need different action depending on trigger.
You could do something like this for the ones that are exactly the same. You could expand the logic for the ones that are different as well.
I prefer not to use âdevicesâ unless I must. entities are easier to work with.
I didnât test the above. Just demonstrating the use of variables insteadâŠ
btw, you use ârestartâ in your definition but is that really what you want? that could impede another motionâs action if they both trigger at the same time.
You can streamline it a bit more by replacing the if with an equivalent condition. The advantage is that if the condition isnât fulfilled then the automation never executes its action (and its last_triggered remains unchanged).
@123 I have one or actually two camorras in a remote location (holiday home) where I have different automations depending on if the house is empty or if Iâm there.
I have a âbooleanâ that should be checked in the condition and If Iâm not at the house all lights in the house should turn on. I also had a plan to send a notification in this second case but not that important.
Just checking so I understood, - â{{ is_state(âsun.sunâ, âbelow_horizonâ) }}â is equal to that it is after sunset and before sunrise ânighttimeâ, right?
Ok, now i thought I understood some more but hit the wall immediately I tried to extend the function to also turn off the triggered light after 5 minutes I added this code.
The purpose was to first turn on the lite and after 5 minutes turn off. The delay part of the action should not happen if the first part didnât got triggered, i.e if the light was on to start with the automation should not turn it off , only if the cameras trigger had turned it on.
Hmm, when I format as you suggest I get âMessage malformed: expected a dictionary for dictionary value @ data[âactionâ][0][âtargetâ]â error. Canât get this to work⊠It look logical but still.
Thx, now I donât get the error but it the lights donât get triggered. I double checked the entity names and they are correct. The code now looks like this:
It really looks so neat as I hoped it would, but I have missed something. I checked the cameras and a get the motion, I even built the flow i Node-RED and it works but the cant seems to get this to work.
EDIT: Now I can get it to work when I excluded the
Will try again later when the sun is down with it back in as condition. I already realize tough that I need to turn on more then one light, is it possible to use scenes instead of the switch, I cant seem to find a state on the sceneâŠ
EDIT2: Donât think scenes will solve it anyhow, I will give the action: - choose: a go, seems to be closer to my former experience with sql and luaâŠ
You define a scene containing the lights you want to turn on (or off) and then simply activate the scene using scene.turn_on (see the first example in the documentation).