Help With First Automation

Normally I would try and figure this all out and then ask questions to fill in the blanks, but I just got off a 12hr shift and I am tired and the attempts I have made so far have failed. So I will ask for help this time with the intention of getting a full blown example I can follow. The ones in the documentation have not made a lot of sense and don’t seem to be geared towards stepping you from a beginner simple automation to more complex ones.

Here is what I have:

First, my sole door sensor:

group.doors_sensors

{
“entity_id”: [
“binary_sensor.aeotec_zw112_door_window_sensor_6_sensor_3”
],
“friendly_name”: “Doors Sensors”,
“order”: 1
}

Second, my sole smart switch:

group.switches

{
“entity_id”: [
“switch.aeotec_zw096_smart_switch_6_switch_4”,
“sensor.aeotec_zw096_smart_switch_6_voltage_4”,
“sensor.aeotec_zw096_smart_switch_6_power_4”,
“sensor.aeotec_zw096_smart_switch_6_energy_4”,
“sensor.aeotec_zw096_smart_switch_6_current_4”,
“sensor.aeotec_zw096_smart_switch_6_previous_reading_4”
],
“friendly_name”: “Switches”,
“order”: 6
}

All I am trying to do, in order to learn how to do this, is take the door sensor and when the door is opened, turn on the smart switch, which has a single lamp plugged into it. When the door is closed, the sensor registers off, open, on. I can manually turn on and off the lamp inside HA, so I know that part works. I just cannot get it to trigger from the door sensor.

I hate to be so lazy as part of the fun for me is learning this (though it is frustrating at times), but the documentation is just not that helpful.

So can anyone give me an idea of how this should be coded? Again, I apologize for seeming lazy, but after a 12hr shift I am going to avoid beating my head on my desk and instead lay it on a pillow, LOL.

automation:
  trigger:
    platform: state
    entity_id: binary_sensor.aeotec_zw112_door_window_sensor_6_sensor_3
    state: 'on'
 action:
    service: switch.turn_on
    entity_id: switch.aeotec_zw096_smart_switch_6_switch_4

I think this would work. You could even add a condition for the sun being down or close to it by adding this:

automation:
  trigger:
    platform: state
    entity_id: binary_sensor.aeotec_zw112_door_window_sensor_6_sensor_3
    state: 'on'
 condition:
    condition: sun
    after: sunset
    after_offset: "-00:45:00" 
 action:
    service: switch.turn_on
    entity_id: switch.aeotec_zw096_smart_switch_6_switch_4

That would make it only turn on 45 minutes before official sundown.

Get some rest bud. Hope this helps.

Welcome aboard and heres to many more Automations… cheers!

FYI, for future reference, when pasting andy code in the forums, always use the “Preformatted text” option on the top of the editor bar. Just highlight the code and click the button that looks like this:

</>

Otherwise the code is hard to read. You pasted JSON, so the spacing is not as important, yet still very hard to read. But when you start writing your automations in YAML, spacing is very specific, so the problems you may have in future automations will often be due to not having the correct spacing, but if you were to paste it here in the forum without selecting the “Preformated text” option, we can’t tell what your spacing is like.

To see the difference, look at the code you pasted and then look at rpitera’s response.

Althought rpitera’s code would fail if you copied and pasted it because his spacing is off. You will notice that “trigger” does not lineup with “condition” or “action” in both of his examples. This would throw an error as YAML is very specific with spacing.

@rpitera, thank you for that. That worked perfectly at turning on the light. It gives me something to work with now and I will try to figure out how to turn the light back off when the door sensor changes state to off. It should be a slow day at work so I will have some time to browse around and get educated on that process, LOL.

Thank you so much.

I think I was close in my initial attempts, but when I looked at the log it was telling me that duplicate entity id’s were not allowed or something to that effect. Not sure now as I was pretty wiped out then. Nonetheless, my goal now is to use what you have provided to build on and turn the light back off. My understanding is that it will be in some sort of conditional statement, I will just have to figure out how to write it.

1 Like

@jbardi, point taken. However, my intent was not to share my configuration, I was trying to share the information about the devices I was trying to automate. Since I was asking for such detailed help I was trying to provide detailed information about the devices. That and just being utterly exhausted I just cut-n-pasted instead of formatting.

Hmm, strange as I cut and pasted with preformatting and this was one of the first automations I did and it runs beautifully.

Glad I could help. You sounded beat up! Hope you’re feeling better today!

I am, I was pretty exhausted and had just a short time to poke around HA before I had to get to bed. Today, I only have an 11 hr shift. Wooo Hoo, and extra hour to poke around HA today, LOL.

I appreciate your patience with me as well. I try to at least show I have made an effort when asking questions, last night I was just too tired to focus long. I believe I cut and pasted your example too this morning testing it out. I was just happy to see an automation work. I am going to buy a couple more door sensors and switches before I convert the rest of my home over from SmartThings to HA but am excited to learn so I can do just that.

Interesting side note, a friend of mine who knows I have automated much of my home, whom works in oil and gas, has inquired of me for assistance in using such automation in his work. He seemed very interested in all HA can do, so I am going to set up a demo for him.

1 Like

Believe me, people here have been (and continue to be) patient with me! @robbiet480 spent a marathon session with me in Gitter getting HTML5 push notifications to work after I screwed things up. And he had more than one chat going on but was so helpful and patient with me.

It’s part of the culture here.

…and another HASS ambassador is born! :wink:

1 Like

Not that it was as difficult as I thought it would be, but I managed to make the automation so that the light turned on and off based on the sensor state.

Now, it occurs to me, that seems like a lot to do for a simple action based on a binary state. I was initially thinking along the lines of an if/then type of statement, but how to do so did not readily leap out at me.

automation 1:
  trigger:
    platform: state
    entity_id: binary_sensor.aeotec_zw112_door_window_sensor_6_sensor_3
    to: 'on'
  action:
    service: switch.turn_on
    entity_id: switch.aeotec_zw096_smart_switch_6_switch_4

automation 2:
  trigger:
    platform: state
    entity_id: binary_sensor.aeotec_zw112_door_window_sensor_6_sensor_3
    to: 'off'
  action:
    service: switch.turn_off
    entity_id: switch.aeotec_zw096_smart_switch_6_switch_4

I looked at the documentation and found the following:

condition: or
conditions:
  - condition: state
    entity_id: 'device_tracker.paulus'
    state: 'home'
  - condition: numeric_state
    entity_id: 'sensor.temperature'
    below: '20'

As been my experience thus far with the documentation, it seems to want to thrust you right into intermediate to advanced type examples. As such, I am not sure how to convert this for my simple demo. So I made an a guess, I want to say educated one, but I think that would come back and bite me at this stage, and came up with the following:

automation 3:
  condition: or
  conditions:
    - condition: state
      entity_id: binary_sensor.aeotec_zw112_door_window_sensor_6_sensor_3
      state: 'on'
  action:
    service: switch.turn_on
    entity_id: switch.aeotec_zw096_smart_switch_6_switch_4	
    - condition: state
      entity_id: binary_sensor.aeotec_zw112_door_window_sensor_6_sensor_3
      state: 'off'
  action:
    service: switch.turn_off
    entity_id: switch.aeotec_zw096_smart_switch_6_switch_4

And it more or less told me to get bent. So it would be safe to say my ‘educated’ guess was as I suspected and not so much. So where am I going wrong here?

I don’t think you can nest a condition in an action for one. Also, your using and or statement but only have one condition to test for.

And you can’t have two “action:” statements. You can have one action: and then actions listed under them with hyphens for each actio. like this:

action:
   - service: blah blah blah
     entity_id: your_entity
   - service: other service
     entity: other_entity