Front Porch Light Automation

I’ve been working on an automation for my front porch lights that works as follows:
When the sun is 1 degree below the horizon, activate a random Hue scene from a script. When motion is activated, turn the lights on full bright. After 1 minute of no motion, run the random scene script again. I am having some issues getting it to work correctly so I am looking for feedback on my yaml where I might be able to make improvements. I haven’t been able to set it up to just turn off when the sun it 1 degree above the horizon. I prefer to work on degrees of horizon rather than time to account for the changing times for sunset and rise since elevation is absolute. I’ve considered stripping it into 2 separate automatons but I’d like to make it work in one, if possible. Anyhow, here is what I have thus far:

alias: Front Porch Full_Random Scenes
description: ""
trigger:
  - platform: numeric_state
    entity_id: sun.sun
    id: Sunset
    attribute: elevation
    below: -1
  - type: motion
    platform: device
    device_id: b4fff2a7dc95f2b971e24908010e0e73
    entity_id: binary_sensor.hue_motion_sensor_1_motion
    domain: binary_sensor
    id: Porch Motion
  - type: no_motion
    platform: device
    device_id: b4fff2a7dc95f2b971e24908010e0e73
    entity_id: binary_sensor.hue_motion_sensor_1_motion
    domain: binary_sensor
    id: No Porch Motion
    for:
      hours: 0
      minutes: 1
      seconds: 0
condition:
  - condition: numeric_state
    entity_id: sun.sun
    attribute: elevation
    enabled: true
    below: -1
action:
  - if:
      - condition: trigger
        id: Sunset
    then:
      - service: script.new_script
        data: {}
    else:
      - service: light.turn_off
        data: {}
        target:
          entity_id: light.front_porch_lights_new
        enabled: true
  - if:
      - condition: trigger
        id: Porch Motion
    then:
      - service: scene.turn_on
        target:
          entity_id: scene.front_porch_bright
        metadata: {}
    else: []
  - if:
      - condition: trigger
        id: No Porch Motion
    then:
      - service: script.new_script
        data: {}
mode: single

In case you want to see, here is the random script:

alias: FP_Random_Scene
sequence:
  - data_template:
      entity_id: >
        {{ ["scene.front_porch_savanna_sunset", "scene.front_porch_meriete",
        "scene.front_porch_moonlight", "scene.front_porch_starlight",
        "scene.front_porch_forest_adventure", "scene.front_porch_magneto",
        "scene.front_porch_arctic_aurora", "scene.front_porch_memento",
        "scene.front_porch_motown"]
          | random}}
    service: scene.turn_on
mode: single

I’m not sure why this is not working as I cannot see anything obviously wrong other than the fact that the condition will prevent the automation from running in the situation you describe where the sun is 1 degree above the horizon.

I have tended to use the choose command rather than if-then-else in these applications and the following is an example that has been working well for a few years.

alias: Hall Light Control
description: Controls hall light based on motion sensor
trigger:
  - entity_id:
      - binary_sensor.neo3
    platform: state
    to: "on"
    from: "off"
    id: motion
  - platform: state
    entity_id:
      - binary_sensor.neo3
    to: "off"
    for:
      hours: 0
      minutes: 5
      seconds: 0
    id: no_motion
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: motion
          - condition: or
            conditions:
              - after: sunset
                after_offset: "-00:30:00"
                condition: sun
              - before: sunrise
                before_offset: "00:30:00"
                condition: sun
              - type: is_illuminance
                condition: device
                device_id: 607cb4197754ca295c3646d022602574
                entity_id: sensor.neo3_luminance
                domain: sensor
                below: 25
        sequence:
          - type: turn_on
            device_id: 5ba6f5722dd40e85dc7dcf5826616248
            entity_id: light.hall_light
            domain: light
      - conditions:
          - condition: trigger
            id: no_motion
          - condition: device
            type: is_on
            device_id: 5ba6f5722dd40e85dc7dcf5826616248
            entity_id: light.hall_light
            domain: light
        sequence:
          - type: turn_off
            device_id: 5ba6f5722dd40e85dc7dcf5826616248
            entity_id: light.hall_light
            domain: light
    default: []
initial_state: "on"
mode: restart

I have used states in the triggers whereas you have used devices but I don’t think that should matter.

Hope this helps a bit.

1 Like

Thanks for the reply. I was just looking at the choose command this afternoon. I’ve never used it and actually started.looking at documentation to learn about it today. I’ll look over your example when I’m not on mobile and will see how I might incorporate it. Thanks again!

For your consideration (the script’s functionality is incorporated into the automation but can be easily moved back to a script if that’s your preference).

alias: Front Porch Full_Random Scenes
description: ''
trigger:
  - platform: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: -1
  - platform: state
    entity_id: binary_sensor.hue_motion_sensor_1_motion
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: binary_sensor.hue_motion_sensor_1_motion
    from: 'on'
    to: 'off'
    for:
      minutes: 1
condition:
  - condition: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: -1
action:
  - service: scene.turn_on
    target:
      entity_id: >
        {% if trigger.platform == 'numeric_state' or trigger.to_state.state == 'off' %}
          {% set scenes = ['savanna_sunset', 'meriete', 'moonlight', 'starlight',
            'forest_adventure', 'magneto', 'arctic_aurora', 'memento', 'motown'] %}
          scene.front_porch_{{ scenes | random }}
        {% else %}
          scene.front_porch_bright
        {% endif %}
mode: single
  • A random scene is activated when the sun sets or when the motion sensor turns off.
  • The front porch light is turned on when the motion sensor turns on.

EDIT

Simplified the action’s logic. Only one scene.turn_on service call is needed.

1 Like

@123 That is an excellent simplification. I appreciate the feedback. I am going to try and implement this and see how it works. Again, many thanks!

edit: looking over this again I am again reminded how much more robust this platform is when templates are used. Another reminder that I need to really learn how to use those in my automations. Thanks again!

FWIW, looking back at the template today, I realized it can also be written like this:

action:
  - service: scene.turn_on
    target:
      entity_id: >
        scene.front_porch_{{ 'bright' if trigger.to_state.state == 'on' else 
          ['savanna_sunset', 'meriete', 'moonlight', 'starlight',
            'forest_adventure', 'magneto', 'arctic_aurora', 'memento', 'motown'] | random }}
1 Like

@123 Ok cool, thanks for the reply! I am setting it up and will see how it runs tonight, I’ll let you know.

Edit: Wouldn’t you know… I went to do some testing and discovered my Hue sensor failed last night and I think it is finally dead because I can’t get it to power on at all. Time for a new sensor and then I’ll be able to report back how it all functions.

@123 Just a quick reply, automation worked flawlessly last night. I fiddled with my motion sensor last night and got it to sort of work and got great results. Thanks again for the help!

1 Like