Noob question about yaml, this seems clumsy

I just installed HASS and am getting things set up. I’ve set up an automation that works fine, but it seems really cumbersome. Is this really the most efficient way to set this up? For example you’ll see the time 22:00:00 several times. I originally had midnight, but decided to change it … in all three places.

The goal is to have the outside lights turn on at a default brightness of 70 at sunset, and then off at 10pm. Also, any motion detected between dusk and dawn causes the lights to go to full brightness (and then return to the default value once motion ends).

Is there a way to streamline this? Can I at least declare constants, something like:
#define NIGHT_BRIGHTNESS 70
#define LIGHTS_OFF 22:00:00
So that I have one place to edit these values and keep things working consistently?

Thanks for any help, and apologies in advance if this is obvious and I simply missed reading the relevant documentation.

-Greg

automation:
# Turns outside lights on at sunset
  - alias: 'Outside lights on in the evening'
    trigger:
      - platform: sun
        event: sunset
    action:
      service: homeassistant.turn_on
      entity_id: light.frontdoorlight_level_4_0
      data:
        brightness: 70

# Turns outside lights off at 10pm
  - alias: 'Outside lights off at 10pm'
    trigger:
      - platform: time
        after: '22:00:00'
    action:
      service: homeassistant.turn_off
      entity_id: light.frontdoorlight_level_4_0
            
# Set Front Door Light to full brightness upon motion
  - alias: 'Front door motion'
    trigger:
      - platform: state
        entity_id: sensor.vision_zp3102_pir_motion_sensor_burglar_3_10 
        to: '8'
    condition:
      condition: sun
      after: sunset  
    action:
      service: homeassistant.turn_on
      entity_id: light.frontdoorlight_level_4_0
      data:
        brightness: 255

# Set Front Door Light back to normal brightness upon no motion
  - alias: 'Front door no motion - dim'
    trigger:
      - platform: state
        entity_id: sensor.vision_zp3102_pir_motion_sensor_burglar_3_10
        to: '0'
    condition:
      condition: and
      conditions:
        - condition: time
          after: '16:00:00' 
          before: '22:00:00'
        - condition: sun
          after: sunset
    action:
      service: homeassistant.turn_on
      entity_id: light.frontdoorlight_level_4_0
      data:
        brightness: 70

# Set Front Door Light back to off upon no motion
  - alias: 'Front door no motion - off'
    trigger:
      - platform: state
        entity_id: sensor.vision_zp3102_pir_motion_sensor_burglar_3_10
        to: '0'
    condition:
      condition: or
      conditions:
        - condition: time
          after: '22:00:00' 
          before: '10:00:00'
        - condition: sun
          before: sunset
    action:
      service: homeassistant.turn_off
      entity_id: light.frontdoorlight_level_4_0

To start, and I am not saying this is the correct way.

I would create an input Boolean called for example motion lights.

I would have your automation that turns on the lights at night also turn on this input Boolean.

Then on your motion detector automations I would set the condition to the input Boolean being on.

Then your automation that turns off the lights should turn off be input Boolean as well.

Or

You could have your first automation turn on your motion automations. And then have your turn off automation turn off the motion automations. Then you would not need any conditions on the motion automations.

If you are used to other programming languages, yaml is very clumsy. An alternative is appdaemon, which allows you to program automations in python.

1 Like

Yes you can! :slight_smile:
For the brightness you can take an input_slider
For the time you can use an input_select

Thanks all for these excellent suggestions. For now I will take the simple route of using an input_slider and input_select, but will look into restructuring it or switching to python (which might be easier for me in the end).

Thanks again.
Greg

Ok, I spoke too soon. I implemented two input_sliders, one as a UI element to set the value (default 70), and one as a hidden entity that gets set either to 0 or to the selected brightness value, based on sunset and time triggers. That all seems good, and it allowed me to simplify my automations.

I also implemented an input_select with some reasonable options for the time trigger.

What I haven’t sorted out is how to do a time trigger off of the value of my input_select.

Here is the input_select:

input_select:
  outside_lights_off:
    name: Turn off outside lights at
    options:
      - "21:00:00"
      - "21:30:00"
      - "22:00:00"
      - "22:30:00"
      - "23:00:00"
      - "23:30:00"
      - "00:00:00"
    initial: "22:00:00"
    icon: mdi:clock

How do I link that value as the ‘after’ in my automation script?

# Turns outside lights off at 10pm
  - alias: 'Outside lights off at 10pm'
    trigger:
      - platform: time
        after: '22:00:00'
    action:
      - service: homeassistant.turn_off
        entity_id: light.frontdoorlight_level_4_0
      - service: input_slider.select_value
        data:
          entity_id:  input_slider.outside_light_current_brightness
          value: 0

Here’s a nice, but very long thread about this.
I prefer a template sensor for triggering the template trigger.

sensor:  
  - platform: template
    sensors:
      current_time:
        value_template: '{{ now().strftime("%H:%M") }}'

input_select:
  outside_lights_off:
    name: Turn off outside lights at
    options:
      - "21:00"
      - "21:30"
      - "22:00"
      - "22:30"
      - "23:00"
    initial: "22:00"
    icon: mdi:clock

automation:
  - alias: Time template trigger
    trigger:
      platform: template
      value_template: '{{ states.sensor.current_time.state == states.input_select.outside_lights_off.state }}'
    action:
      service: persistent_notification.create
      data:
        message: "Turn off outside lights"
        title: "Time trigger"
1 Like

That worked perfectly. Thanks for the help!