Turn on outside light with alarm system

I am completely new to HA and do not yet fully understand how it works.I would like HA to turn on my outside light when the alarm system goes off. So far i have managed the following:

  • installed HA on Raspberry 3
  • installed Sonoff mini switch and integrated it with Sonoff LAN (i can see the switch on on the HA overview window and i can turn the light on and off)
  • connected the alarm system signal output (dry contact closes if alarm goes off) to pin 16 (GPIO 23) and pin 20 (Ground) on the Raspberry
    Now i am stuck. I can not figure out how to create the automation.
    I would appreciate your help how to do this.

Are you going to use the graphical user interface or yaml to write your automation?

In the GUI you select your trigger (alarm input) the state to goes to (on) and the action you want to perform (turn on the alarm switch). I don’t know a lot about the GUI as I don’t use it.

In yaml it would look like this:

automation:
- id: alarm_outside_lights_on
  alias: 'Alarm Outside Lights On'
  trigger:
    platform: state
    entity_id: binary_sensor.your_alarm_sensor_here  # change this to your alarm sensor
  action:
  - service: switch.turn_on 
    entity_id: switch.your_light_here # change this to your sonoff switch

Note I used the switch service not a light service. Your sonoff is a switch, connected to a light. If you would like to make your switch appear as a light you can use this:

1 Like

Thanks for replying :slightly_smiling_face:

I think it would be easier for a beginner to use the graphical user interface. If this is not possible i will try yaml with the file editor (i did not know that you can also do it directly in the yaml :slightly_smiling_face:)

  • where can i find the entity_id’s of my alarm sensor and sonoff switch?
  • how does HA know to which pin i have connected the sensor?

thanks again for helping me

configuration / entities menu.

Ah, sounds like you haven’t set that binary sensor up yet. Instructions here:

thanks @tom_l.

i set up the binary sensor and i can see it in the entities menu. When i click on it it gives me this message:

“This entity does not have a unique ID, therefore its settings cannot be managed from the UI”

is this a problem?

i also added your code to the yaml but the file editor is not happy. It gives me this message:

“duplicated mapping key at line 23, column -368:
automation:
^”
line 23 is the line the word “automation” is in

No this is not a problem. There are a lot of integrations that can not be set up from the UI yet.

The key,

automation:

Should only appear once in your configuration.yaml file. It already exists somewhere in that file so paste everything in my code except the first line under it.

you are right “automation” already appears in the file ( automation: !include automations.yaml)
I copied your code under it without the first line but now i get this error message:

“end of the stream or a document separator is expected at line 14, column 1:
- id: alarm_outside_lights_on
^”

The include! points to another file called automations.yaml. You have to paste it in there (again, minus the automation: line).

Sorry it has been a while since I set up home assistant and forgot that the initial config had that include pointer.

thanks. It worked :+1:

1 Like

i tested the system and the lights turn on when i close the contact but they do not turn off when i open the contact. Did i set up the binary sensor wrong?

You did not ask for them to turn off. :slight_smile:

The easiest to understand solution is simply to write another automation (and modify the initial one slightly):

- id: alarm_outside_lights_on
  alias: 'Alarm Outside Lights On'
  trigger:
    platform: state
    entity_id: binary_sensor.your_alarm_sensor_here  # change this to your alarm sensor
    to: 'on'
  action:
  - service: switch.turn_on 
    entity_id: switch.your_light_here # change this to your sonoff switch

- id: alarm_outside_lights_off
  alias: 'Alarm Outside Lights Off'
  trigger:
    platform: state
    entity_id: binary_sensor.your_alarm_sensor_here  # change this to your alarm sensor
    to: 'off'
  action:
  - service: switch.turn_off
    entity_id: switch.your_light_here # change this to your sonoff switch

There are more complex solutions that use templates to combine the two automations into one.

wow, learning so much. I changed the code and the lights turn now on and off. But it is quite erratic. Sometimes it works and sometimes it doesn’t . Sometimes the lights turn off when the contact closes and sometimes they turn on. What could be the reason for this behavior?

ok tested it again and observed the following behavior. When i close the contact for the first time nothing happens. Then when i open the contact the lights turn on. Thereafter closing and opening the contact turns the lights on and off ok but the lights come on with the contact open instead of closed.

Add the invert logic option to your GPIO binary sensor.

thanks for the information and the link to the Raspberry Pi GPIO integration. Could you please advice how to change the invert_logic of the binary sensor. I can see that it is listed under configuration variables but i could not figure out how to change it :cry:

Using the example configuration (as you did not post yours):

binary_sensor:
  - platform: rpi_gpio
    invert_logic: true
    ports:
      11: PIR Office
      12: PIR Bedroom

thanks again for your help and patients with me. I should have properly read the Raspberry GPIO link you provided and i could have found the answer :blush:.
I have changed the code and it is now working the right way. There is another problem i hope you can help me with. After restart of HA the first time i close the sensor contact the lights do not turn on. They only turn on after opening and then closing the contact again. After that everything works ok until the next time i restart HA. Why is this? Is there a way to tell HA how to start up after a reboot?

I have no idea why it is behaving that way.

Look in the developer tools states menu and check that your binary sensor does actually change state the first time you close the contacts.

A description of how you wired the contact might help too. Did you use pull-up resistors?

i checked. HA starts up with the sensor off and It does not change state when i first close the contact. After opening and then closing the contact it changes state to on

Ground (pin 20) to contact in - contact out to GPIO 23 (pin 16)
i also put a 1k resistor into the wire to prevent damage to the Pi (in case i screw up the pin in and out programming :grinning:)

binary_sensor:

  • platform: rpi_gpio
    invert_logic: true
    ports:
    23: alarm
    pull_mode: UP

no.I used the pull up resistor in the Pi using the configuration of the binary sensor (see above copy of the code)