Lighting script

Hello all

haven’t been using HA for long, moved from Dom to HA.

I’m also NOT a programmer. But I have a task that I’m sure requires a script of some sort.

I use HA to automate my grow room. Nothing else.

The other day we had a power cut and the automation I had for the lights didn’t run as power was restored after the timer event. Obviously have 2 events for the lights, one for on and one for off.

What I would like is a single script that has 2 variables LightsOn and LightsOff and I would like this to be the only place I set the timers. The timers need adjusting during the grow cycle and I don’t want to adjust in multiple locations.

The script also needs to do some sanity checking. for example if we have a power cut and when it comes back on it checks the time to see if the lights should be on or off and sets the state of the light switch appropriately.

Information on my install

  • Installation method Home Assistant OS
  • Core 2025.12.4
  • Supervisor 2025.12.3
  • Operating System 16.3
  • Frontend 20251203.3

The light switch is a tasmota device called RelayBoard and the switch is called Lights.

As I have said above, I’m NOT a programmer, I would rather not attempt any programming. Simply put, I do what I do and I do it well, for everything else I get someone who IS good at what I want done and I ask them if they would like to do it for me. So please don’t flame an old retiree.

Many thanks in advance for your offerings.

Regards

Fred

What have you tried after reading the documentation?
Post your code/yaml.

The general answer to your issue is to add more triggers to your automation and make sure all triggers are appropriately mirrored as conditions.

There are a few options, you can use one or more of the following:

  1. If your Home Assistant server also turns off when the power is cut, you could add a Home Assistant trigger for the start event.
  2. Include a Time Pattern trigger . Habitually using time pattern triggers is usually something to avoid, but it sounds like the demand on this Home Assistant instance will be pretty low, so it shouldn’t be an issue.
  3. Trigger off the state of the switch changing from ‘unavailable’. Many entities will have a state of “unavailable” if their device disconnects from the network, at startup, or when their integration is reloaded. That makes it a pretty good indicator that you may want to check the time for the lights.

The HA analog to Domoticz’ variables are Helpers. “Date and/or Time” Helpers come in three types, make sure to select Time when you set up the Helpers, since that is what will suit your purposes.

You should end up with two entities input_dateime.lightson and input_dateime.lightsoffthat you can use in your Time triggers and conditions, and whose values can be modified through dashboard cards or scripts/automations.

There are multiple ways all of this could be assembled into an automation…

Here's one way... just make sure to swap in your actual entity IDs if they don't already match
description: "Turn lights on and off based on the values from Time Helpers with redundant triggers in case of power failure etc."
mode: single
triggers:
  - trigger: time
    at: input_datetime.lightson
    id: turn_on
  - trigger: time
    at: input_datetime.lightsoff
    id: turn_off
  - trigger: homeassistant
    event: start
  - trigger: time_pattern
    hours: /1
conditions: []
actions:
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: trigger
                id: turn_on
              - condition: time
                after: input_datetime.lightson
                before: input_datetime.lightsoff
        sequence:
          - action: switch.turn_on
            metadata: {}
            target:
              entity_id:
                - switch.lights
            data: {}
      - conditions:
          - condition: or
            conditions:
              - condition: trigger
                id: turn_off
              - condition: not
                conditions:
                  - condition: time
                    after: input_datetime.lightson
                    before: input_datetime.lightsoff
        sequence:
          - action: switch.turn_off
            metadata: {}
            target:
              entity_id: 
                - switch.lights
            data: {}
1 Like

Here is both codes.

alias: Grow Room Lights OFF
description: ""
triggers:
  - trigger: time
    at: "18:00:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
conditions: []
actions:
  - type: turn_off
    device_id: 014400ee2d450232ecc8c41997b0375e
    entity_id: 19ca0cf71477f7863ef2623d225393ce
    domain: switch
mode: single
alias: Grow Room Lights ON
description: ""
triggers:
  - trigger: time
    at: "06:00:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
conditions: []
actions:
  - type: turn_on
    device_id: 014400ee2d450232ecc8c41997b0375e
    entity_id: 19ca0cf71477f7863ef2623d225393ce
    domain: switch
mode: single

These codes were simply created in the GUI under Automations because I can’t code.

Ideally it would be good to have a single piece of code for the entire thing

To answer [Didgeridrew] question, yes the HA server goes down when we loose power.

Regards

Fred

AI can easily write the automation you want

OK. so it seems I may have solved this issue. After floundering around I discovered blueprints.

I installed the Smart Light blueprint, and after a bit of faffing about I managed to configure it and got it working. Now I just have to test it under a power failure enviroment during the switch on/off period.

Regards

Fred

Not sure you read the above. I’m not a programmer. And I clearly don’t know what I’m doing, and AI isn’t something a person of my age has any knowledge of.

But nice of you to make the effort to comment.

Regards

Fred

What you are trying to do it really pretty basic even for a non-programmer like myself.

Millions of non-programmers using AI to write code. AI codes 99% of my complex automation and blueprints. And what amazing, is you can actually learn from the code the AI builds for you :wink:

Your choices are…

  1. take an hour or two to figure out AI to write this pretty simple automation you want
  2. learn from the help Dig gave you (which is more than he gives most that have not tried to figure it out themselves)
  3. hire someone
  4. hope that someone will write it for you for free (which is likely what you are doing now)

Oh, and I dont know what a ‘person of my age’ means… but its not relevant, you are on this forum so you have enough tech know-how to use AI.

I was around far before the Internet existed, when Pong was modern tech

I’ve taken the dive and used chatgpt to create some code. Took me 3 days to get it right, but I now have a working solution.

Regards

Fred

glad you did it. next time it will take you 1 day, then the time after that 3 hours. :wink: It does get far easier/quicker. I do complex blueprints code that is 1800 to 2800 lines and it takes me 3 days but ~8 total hours.