# Considering openHAB - Startup and migration (from Home Assistant) questions

**URL:** <https://community.openhab.org/t/considering-openhab-startup-and-migration-from-home-assistant-questions/137808>\
**Category:** Beginners\
**Created:** [August 2, 2022, 6:55am UTC](https://community.openhab.org/t/considering-openhab-startup-and-migration-from-home-assistant-questions/137808 "2022-08-02T06:55:18Z")\
**Posts on this page:** 1\
**Showing post:** 26

<div class="post-metadata">

**Author:** ![jimtng](https://community.openhab.org/user_avatar/community.openhab.org/jimtng/32/98543_2.png) [@jimtng](https://community.openhab.org/u/jimtng)\
**Post date:** [August 5, 2022, 9:58am UTC](https://community.openhab.org/t/considering-openhab-startup-and-migration-from-home-assistant-questions/137808/26 "2022-08-05T09:58:59Z")

</div>

> [@ImaginaryTango](#):
>
> Interesting way to do it. so if the elevation is 0, how do I distinguish between whether it’s sunrise or sunset?

In my rules, I don’t really care whether it’s sunset or sunrise because they are simply using the elevation to decide whether to perform something or not. For example:

> “if someone turned off the living room light, and it stays off for 10 minutes, and the sun is not visible (elevation \< 0), then turn off the wall mounted display screen”.

Here’s my actual rule written in [jruby](https://community.openhab.org/t/jruby-openhab-rules-system/110598):

```ruby
rule 'Turn off photo frame' do
  changed gLivingRoomLights, to: OFF, for: 10.minutes
  only_if { Sun_Elevation.negative? }
  run { PhotoFrame_Screen.ensure.off }
end

```

If on the other hand you actually need to run specific tasks on sunset e.g turn lights on 10 minutes before sunset, and turn them off 5 minutes before sunrise, the [Astro binding has that covered](https://www.openhab.org/addons/bindings/astro/#channel-config) using `offset`.

For a task to be performed **after** the event (sunset/sunrise) you can have your rule fire on the event (so you don’t have to configure the “offset” as per the above link), and then inside your rule, create a timer that will execute the task X minutes later.

Others can probably give you more ideas. If you can describe the exact scenario, we’ll be happy to go into it further.

Most importantly do not hesitate to ask here if you are stuck. We’ve all been there. I am constantly learning new things about openhab.

> [@ImaginaryTango](#):
>
> One reason I was thinking about a flag like Night\_Flag was because I had something a little more complex than my original idea in mind. I was thinking that during the day, the switch on the hall lights would go from off to 100%. But then, starting 15 minutes before Sunset and after Sunrise, it’d work differently. It’d follow a pattern: off-\>30%-\>100%-\>off. So during that time, it wouldn’t be just off/on, but would have 3 steps, like a ceiling fan that goes from one speed to the next each time you pull the string.

I would write something like this (in jruby):

```ruby
# cycles the light from OFF -> 30% -> 100% -> OFF
def cycle_light_level(light)
  light << case light
           when OFF then 30
           when 100 then OFF
           else 100
           end
end

# Returns a range from sunset to sunrise, minus/plus the given offset
# can be given as argument for TimeOfDay#between?
def sunset_to_sunrise(offset = 0.seconds)
  astro = things['astro:sun:home']
  pre_sunset = astro.getEventTime('SUN_SET', nil, nil).minus(offset)
  post_sunrise = astro.getEventTime('SUN_RISE', nil, nil).plus(offset)

  # convert to DateTimeType range
  DateTimeType.new(pre_sunset)..DateTimeType.new(post_sunrise)
end

rule 'Hall light switch' do
  updated Hall_Switch, to: "click" # assuming this switch sends "click" when it's tapped / clicked
  run do
    if TimeOfDay.now.between? sunset_to_sunrise(15.minutes)
      cycle_light_levels(Hall_Light)
    else
      Hall_Light << 100
    end
  end
end

```

Or you can have two other rules that control Night\_Flag and then use Night\_Flag in the “Hall light switch” rule as others have suggested above. One thing to consider when using a “Night\_Flag” type system: you’ll also need to set this flag when openhab starts up, so you’ll still need to actually obtain the sunset/sunrise time and not just rely on the astro `trigger event`. This is to cover the time when openhab gets restarted after the set time for night flag, otherwise your rule will run incorrectly until the following day.

---

_[View the full topic](https://community.openhab.org/t/considering-openhab-startup-and-migration-from-home-assistant-questions/137808)._
