How to activate GPIO pin before sunset?

Hi all,

I’m trying to activate a GPIO pin 1 hour before sunset. I’m banging my head without results, and there’s nothing in the docs that illustrates this :frowning:

I have a relay connected to GPIO 4 on the raspberry where Hass is running. Here’s my configuration file:

# Configure the GPIO pins
switch:
  - platform: rpi_gpio
    ports:
      04: Livingroom_lamp

After reading some posts I came up with the automation file below but then Hass reboots in safe mode and throws all kind of errors saying that it doesn’t understand the file.


- alias: Living room lamp on
  trigger:
    platform: sun
    event: sunset
    offset: '-01:00:00'
  action:
    - service: switch.turn_on
      entity_id: switch.Livingroom_lamp

- alias: Living room off at 11PM
  trigger:
    platform: time
    at: 23:00
   
  action:
    - service: switch.turn_off
      entity_id: switch.Livingroom_lamp

Can anybody help me with an automation file to turn GPIO 04 on 1 hour before sunset and off at 11PM?

Thanks beforehand!

Without digging too deep into this, I can see at least one syntax error. The entity id Livingroom_lamp has a capitalized L. Try replacing Livingroom_lamp with livingroom_lamp and try it again. Then, if you want to see “Living-room Lamp” in your dashboard, just add the friendly_name: field to your switch config.

Capital letters (and whitespace) in names is generally a no-no when it comes to yaml. The exception being fields where HA parses the variable name (like friendly_name: and alias:). So “Living room off at 11PM” works, because HA will automagically convert the alias: field, and internally refer to it as living_room_off_at_11pm. However, HA does not parse the entity_id: field. So any capitals there will result in invalid yaml.

Also, I recommend always using the configuration checker button before rebooting. It can save you from major headaches.

you want debugging yet you get errors which you don’t show.

Hi, thanks for helping out.

I changed the yaml files omitting all caps, and everything works fine now!