Rule - How to: Sun_Elevation changed from < x to >= y

I am trying to control a light based on the sun elevation.
So not only using the sun rise and set triggers, but if elevation changes from < 10 ° to >= 10 °, then get brighter.

Is there a way to accomplish this in the webinterface - Using the ‘next gen’ rules?
I’m using openhab 3.2 in docker and this type of rules at least gets presented as the way it should be done.
However i put the ‘from state’ and ‘to state’ (With “| °” or without and some variations) it does not seem to work

Might look something like this:

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: LocalSun_Position_Elevation
      previousState: < 10.0 °
      state: ">= 10.0 °"
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - id: "2"
    configuration:
      itemName: IkeaLEDDriver_ZLL_DIMMABLE_LIGHTLevelControl
      command: "40"
    type: core.ItemCommandAction

Any advice on how to accomplish this?

Your elevation Item probably has units - degrees, radians. Comparing with 10 whatever’s is not computable.

I don’t think the trigger handles units? So you would trigger on any change and compare in script section.

Can i move that part to the conditions section?
Like this:

conditions:
  - inputs: {}
    id: "3"
    configuration:
      itemName: LocalSun_Position_Elevation
      state: 10 °
      operator: ">="
    type: core.ItemStateCondition

if yes, how would i go about transforming the Number:Angle to an Int that i can compare?
After that’s done for the current state, how can i access the previousState in the conditions block?

I wouldn’t expect that to work any better than triggers.

But you can script a condition, and scripts can compare quantities with units.
How-to access previousState depends on the scripting language.

A rule triggers on events, not conditions. You can not use comparisons in a rule trigger.

You need to add conditions to compare the state with 10.0 °. If you want to compare the previous state as well, you need to use a script condition.

As of 3.2, UI rule conditions are units aware now. It will work without units but complain in the logs with a warning that you should add the units.

I am using the elevation of the sun to control my blinds, perhaps you find some useful input in the attached script.

Note: I have converted the rules script from openHAB 2.5

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: MagenwilSonnendaten_Azimut
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: >-
        var int Sonnenstand = (MagenwilSonnendaten_Azimut.state as
        Number).intValue

        var Winkel	        = (MagenwilSonnendaten_Position_Elevation.state as Number).floatValue

        if ( Winkel >= 3 && Sonnenstand >= 60 && Sonnenstand <= 155 ) {
          if ( Sonnenschutz_Ost.state != ON ) Sonnenschutz_Ost.postUpdate(ON)
        } else {
          Sonnenschutz_Ost.postUpdate(OFF)
        }

        if ( Winkel >= 3 && Sonnenstand >= 80 && Sonnenstand <= 195 ) {
          if ( Sonnenschutz_Bibliothek.state   != ON ) Sonnenschutz_Bibliothek.postUpdate(ON)
        } else {
          Sonnenschutz_Bibliothek.postUpdate(OFF)
        }

        if ( Winkel >= 3 && Sonnenstand >= 80 && Sonnenstand <= 245 ) {
          if ( Sonnenschutz_Sued.state != ON ) Sonnenschutz_Sued.postUpdate(ON)
        } else {
          Sonnenschutz_Sued.postUpdate(OFF)
        }

        if ( Winkel >= 3 && Sonnenstand >= 80 && Sonnenstand <= 245 ) {
          if ( Sonnenschutz_Schlafzimmer.state   != ON ) Sonnenschutz_Schlafzimmer.postUpdate(ON)
        } else {
          Sonnenschutz_Schlafzimmer.postUpdate(OFF)
        }

        if ( Winkel >= 3 && Sonnenstand >= 260 && Sonnenstand <= 305 ) {
          if ( Sonnenschutz_Kueche.state   != ON ) Sonnenschutz_Kueche.postUpdate(ON)
        } else {
          Sonnenschutz_Kueche.postUpdate(OFF)
        }

        logInfo("Rule Engine", "Jahreszeit  :                {}", MagenwilSonnendaten_Jahreszeit.state)

        logInfo("Rule Engine", "Sonnenwinkel:                {}", Winkel)

        logInfo("Rule Engine", "Azimut      :                {}", Sonnenstand)

        logInfo("Rule Engine", "Sonnenschutz Wohnzimmer Ost: {}", Sonnenschutz_Ost.state.toString)

        logInfo("Rule Engine", "Sonnenschutz Wohnzimmer Süd: {}", Sonnenschutz_Sued.state.toString)

        logInfo("Rule Engine", "Sonnenschutz Bibliothek    : {}", Sonnenschutz_Bibliothek.state.toString)

        logInfo("Rule Engine", "Sonnenschutz Schlafzimmer  : {}", Sonnenschutz_Schlafzimmer.state.toString)

        logInfo("Rule Engine", "Sonnenschutz Küche         : {}", Sonnenschutz_Kueche.state.toString)
    type: script.ScriptAction
2 Likes