Turn on GPIO if switch Port turned to on. Config yaml rpi_gpio

Hello Guys,
i want to turn on a light if a switch (GPIO on my Raspberry) turned to on.

Here is my Code:

switch pi:
  platform: rpi_gpio
  ports:
   16: Bewegungsmelder
   18: Alarm

alias: 'Turn alarm LED on'
  trigger:
   - platform: state
     entity_id: pi.Bewegungsmelder
     from: 'off'
     to: 'on'
  condition:
   - condition: state
     entity_id: pi.Bewegungsmelder
     state: 'on'
  action:
  <TURN ON Alarm (Port 18)>

First of all:
Is my syntax right and can i do it in my configuration.yaml (is that working)?
and if yesā€¦ how can i write my Action? (This Part: <TURN ON Alarm (Port 18)>)

Second:
I get Errors with my command ā€œhass --script check_configā€ if i put my code in the yaml file. Line 82 is the Column ā€œtā€ in ā€œtrigger:ā€.

2017-07-09 12:48:32 ERROR (Thread-1) [homeassistant.util.yaml] while parsing a block mapping
  in "/home/homeassistant/.homeassistant/configuration.yaml", line 1, column 1
expected <block end>, but found '<block mapping start>'
  in "/home/homeassistant/.homeassistant/configuration.yaml", line 82, column 3
2017-07-09 12:48:32 ERROR (MainThread) [homeassistant.bootstrap] Error loading /home/homeassistant/.homeassistant/configuration.yaml: while parsing a block mapping
  in "/home/homeassistant/.homeassistant/configuration.yaml", line 1, column 1
expected <block end>, but found '<block mapping start>'
  in "/home/homeassistant/.homeassistant/configuration.yaml", line 82, column 3

Thank you for help!!

@Hmma

You dont need the hyphen for the platform unless there are multiple. This may not be the exact issue though.
I see some spacing issues too. Also when you added the pi ports to you config it should have created entities which you can check in the dev tools. You can put your automation in the config but there should be the automation header.

Your switch should look like this with all switch platforms under it.

switch:

  - platform: rpi_gpio
    ports:
      23: Garage Relay

Your automation could look like this with all automation below it.

automation:

  - alias: 'Turn alarm LED on'
    trigger:
      platform: state
      entity_id: pi.Bewegungsmelder
      from: 'off'
      to: 'on'
    condition:
      - condition: state
        entity_id: pi.Bewegungsmelder
        state: 'on'
    action:
      service: switch.turn_on
      entity_id: switch.alarm_switch  # Use the actual name of your switch here

Hope this helps!

@PtP Thank you for the help, the code is now correct.

If i now turn on the ā€œBewegungsmelderā€ switch, the ā€œAlarmā€ Switch does not turn on. Why?
Does this code not trigger it? Manual it turns on if i use the switchbutton of ā€œAlarmā€.

What does your yaml look like now?

Does the entity id you are using match the entity id in the dev tools?

Is the state of the switch ā€˜ONā€™, ā€˜onā€™, or ā€˜Onā€™?

I suspect if you get rid of the redundant condition it will probably work :slight_smile:

1 Like
# CONF RPI GPIO PINs
switch pi:
  platform: rpi_gpio
  ports:
   16: Bewegungsmelder
   18: Alert

# CONF Alarm test
  automation:
    - alias: 'Turn alarm LED on'
      trigger:
        platform: state
        entity_id: pi.Bewegungsmelder
        from: 'off'
        to: 'on'
      condition:
        - condition: state
          entity_id: pi.Bewegungsmelder
          state: 'on'
      action:
        service: switch.turn_on
        entity_id: pi.Alert #Port 18

This is my Code. Without Problems i can start Hass. But the Switch is not turning to on. @keithh666 @PtP
So decided for the Redundant solution because i want to build in the Future more with sensors and so on. A little bit practice is good. (Like if light on then send SMS or Push message)
No Event Triggered?

Change switch pi: to switch:

What do your switches look like in the dev tools (<>) ?

The Configuration.yaml looks now (This Preformated Text is terrible):

switch:
  platform: rpi_gpio
  ports:
    16: Bewegungsmelder
    18: Alert
  automation:
    - alias: "AlarmLED"
      trigger:
        platform: state
        entity_id: switch.Bewegungsmelder
        from: "off"
        to: "on"
      condition:
        - condition: state
          entity_id: switch.Bewegungsmelder
          state: "on"
      action:
        - service: switch.turn_on
          entity_id: switch.Alert #Port 18

The dev tools:

If the ā€œBewegungsmelderā€ onā€¦ so:

So Guys nice help!!
Iā€™m doneā€¦ :stuck_out_tongue_winking_eye: I got it!!!

So what iā€™ve done is that:

Copy the Automation thing in the automation.yaml :wink:
Like that:

configuration.yaml:

switch:
  platform: rpi_gpio
  ports:
    16: Bewegungsmelder
    18: Alert

automations.yaml:

- alias: "AlarmLED"
  trigger:
    platform: state
    entity_id: switch.Bewegungsmelder
    from: "off"
    to: "on"
  condition:
    - condition: state
      entity_id: switch.Bewegungsmelder
      state: "on"
  action:
    - service: switch.turn_on
      entity_id: switch.Alert #Port 18

Then this appears in the GUI of Home Assistant:

If i now turn on the ā€œBewegungsmelderā€-Switch the ā€œAlertā€-Switch is turning on too:

That was what i want!
Thank you!

Congrats! Have fun!

1 Like