Latest from YouTube Channel

Random Vacation Lights Using Home Assistant

July 30th, 2022
YouTube video

You know the drill – you’re going away on holiday and you want to make your house look as lived-in as possible while you’re away, and being the good Home Automationist (Automationist, is that a word??) that you are you want to randomly turn your lights on and off in the evening. Well, I’ve come up with a Home Assistant blueprint called ‘Randomised Light at Night’ that’ll help you build that solution.

If you’re not familiar with blueprints, they’re essentially an easy way to create lots of automations that do the same thing but with different devices. When you create a new automation you choose to create one from a blueprint instead of starting from scratch, and the blueprint will present you with a bunch of fields that let you fill in specific parameters such as times, entities and that sort of thing.

The Blueprint

Start by copying and pasting my blueprint code below into a new text file called randomised_light_at_night.yaml, or import the blueprint using the following button:

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

(Update 2022-08-02: Tweaked a condition to fix a bug that kept the automation running after the lights-out time)

If you’d like to say thank you, please consider donating to help support the site and YouTube channel. All donations are really appreciated, but of course totally optional!

Buy Me A Coffee
blueprint:
  name: Randomised Light at Night
  description: Randomly toggles a light at night within set parameters
  domain: automation
  input:
    target_light:
      name: Light to toggle
      description: Which light are we automating?
      selector:
        entity:
          domain:
            - light
            - switch
    away_lighting_switch_entity:
      name: Away Lighting Switch
      description: This automation will only run if the Away Lighting Switch is on. You could control the status of the Away Lighting switch by another automation based on presence...
      selector:
        entity:
          domain:
            - input_boolean
            - switch
    trigger_minutes:
      name: Trigger Minutes
      description: How often should this automation run?
      default: 15
      selector:
        number:
          min: 1
          max: 120
          unit_of_measurement: minutes
    random_delay_max_minutes:
      name: Random Delay Maximum Minutes
      description: What is the maximum time limit for the random delay?
      default: 30
      selector:
        number:
          min: 1
          max: 120
          unit_of_measurement: minutes
    lights_out_time:
      name: Lights Out Time
      description: What time does this automation stop running? Must be before midnight 23:59:59.
      selector:
        time:

mode: single
max_exceeded: silent

variables:
  trigger_minutes: !input trigger_minutes
  random_delay_max_minutes: !input random_delay_max_minutes

trigger:
  - platform: time_pattern
    minutes: /1

condition:
  - condition: template
    value_template: '{{ now().minute % trigger_minutes == 0 }}'
  - condition: state
    entity_id: !input away_lighting_switch_entity
    state: 'on'
  - condition: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: '-2.0'
action:
  - choose:
      - conditions:
          - condition: time
            before: !input lights_out_time
          - condition: time
            after: '12:01:00'
        sequence:
          - delay:
              minutes: "{{ range(0,random_delay_max_minutes) | random }}"
          - choose:
              - conditions:
                  - condition: state
                    entity_id: !input away_lighting_switch_entity
                    state: 'on'
                sequence:
                  - service: homeassistant.toggle
                    target:
                      entity_id: !input target_light
            default: []
    default:
      - condition: state
        entity_id: !input target_light
        state: 'on'
      - delay:
          minutes: "{{ range(0,random_delay_max_minutes) | random }}"
      - choose:
          - conditions:
              - condition: state
                entity_id: !input away_lighting_switch_entity
                state: 'on'
            sequence:
              - service: homeassistant.turn_off
                target:
                  entity_id: !input target_light
        default: []

Once you’ve got that file ready, you’ll need to copy it up to your Home Assistant’s blueprint folder and the easiest way as usual is by using the File Editor add-on. In Home Assistant, load the File editor add-on’s interface, navigate to the config folder, the blueprints folder in there, and then the automation folder. Click on the Upload file icon, choose your file, then click on ok to upload.

You can then either restart Home Assistant or just reload the automations by going to Developer Tools and clicking on the ‘Automations’ link in the ‘YAML configuration reloading’ section. 

Now a lot of vacation lighting automations tend to just randomly turn lights on and off between sunset and sunrise, but this automation is a bit different. It treats each light individually because quite often you’ll have a pattern of maybe watching TV in the living room in the evening until say 10pm-ish and then you might go up to the bedroom and need the light on up there. You might walk through the hall and the light turns on there temporarily as you’re passing through to use the bathroom several times, again a light turning on just while you’re in there. So your randomised lighting needs to be slightly less random than you think in order to look realistic.

To configure my solution, for each light that you want to include in your vacation lighting you must create a separate automation based on the blueprint. It’s not as difficult or onerous as you might be thinking but there are a few pre-requisites that I suggest you put in place.

Pre-requisites

Firstly you’ll need to create an input_boolean (or use an existing switch) called ‘Away Lighting’. This is the switch that you’ll toggle when you want to enable your vacation lighting. You can create an automation to turn this on and off automatically when it detects that you are away, or you can just toggle it manually if you like.

In Home Assistant, navigate to Settings > Devices & Services, then select the Helpers tab at the top. Click on the ‘Create Helper’ button, scroll down to find and select ‘Toggle’, give the helper a name of ‘Away Lighting’ and click on ‘Create’ to finish. This will create an entity named input_boolean.away_lighting which you can add to your dashboard. The automation will only run when you turn this switch on (to tell Home Assistant that you are away from home). You can now use this entity in the blueprint.

Creating the Automations

Once you have those in place you are ready to create your first automation with the blueprint. Navigate to Settings > Automations and Scenes, then click on the blue Create Automation button in the bottom-right.

Choose Randomised Light at Night from the blueprint drop-down box and a new automation will be created.

First things first, give it a name. Choose a light for the automation to work with, this can be a light or a switch entity. Then choose the away lighting switch entity that you created previously.

Now it’s time for a few decisions. This automation is set to only run when the sun has dropped at least 2 degrees below the horizon, which is better in my opinion than using sunset because it can quite often still be light at sunset especially in the summer.

You need to think about how often you want this automation to run. For example, we walk through our hallway quite a lot, so in normal use the hallway light is turning itself on and off quite regularly. Setting the trigger minutes value to 5 minutes means that the light is likely to turn on and off more regularly. However in a bedroom, you could be in there for a lot longer so setting it to run every 15 minutes may be more appropriate.

Then there’s the maximum time for the random delay. Basically the automation will choose a random number between 1 and this value you’re choosing here, and that’s how long the light will stay on or off for. Again, in a hallway you don’t want this value to be too high because you want it to regularly turn on and off – I’ve seamy hallway random maximum to 15 minutes, but in a bedroom I have it set to 120 minutes because in normal use we’re likely to be in there for a lot longer.

Finally, you need to set the lights-out time, this is the time that you would usually turn the lights out in that room and go to bed. Once that time is reached, if the light is on, it’ll wait for the random delay and turn it off, but of course it may already be off at that time in which case the automation will no longer run that night. Save that automation and it’ll appear in your automations list.

Once you toggle your Away Lighting switch on, those automations will start running and turning your lights on and off according to the parameters you selected.

Less-random Lights

I’ve found for some lights, such as my living room, it was far better not to use this blueprint. Our living room light doesn’t normally turn on and off throughout the evening when we’re at home, we just turn it on when it gets dark and it stays on until we go to bed.

I just created a standalone automation to turn on the light at about sunset, and turn it off randomly in a window of about 30 minutes around our usual bed time. To use this, create a new automation, switch to YAML mode and paste in the following code:

alias: 'Away Light: Living Room'
description: ''
trigger:
  - platform: numeric_state
    entity_id: sun.sun
    below: '-2.0'
    attribute: elevation
  - platform: time
    at: '22:00:00'
condition:
  - condition: state
    entity_id: switch.away_lighting
    state: 'on'
action:
  - choose:
      - conditions:
          - condition: time
            before: '22:00:00'
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.living_room_wall_lights
            data: {}
    default:
      - delay:
          minutes: '{{ range(0,30) | random }}'
      - service: light.turn_off
        target:
          entity_id: light.living_room_wall_lights
        data: {}
mode: single
  • As an Amazon Associate I earn from qualifying purchases.