switch_item

Rule languages supports Switches

    • Given Clean OpenHAB with latest Ruby Libraries
  • Switches respond to off/on and off?/on?

    • Given items:
      typenamelabelstate
      SwitchTestSwitchTest Switch<initial_state>
    • And code in a rules file
      # Invert all switches
      items.select { |item| item.is_a? Switch }
           .each   { |switch| if switch.off? then switch.on else switch.off end}
    • When I deploy the rules file
    • Then "TestSwitch" should be in state "<final_state>" within 5 seconds

    Examples:

    initial_state final_state
    ON OFF
    OFF ON
  • Switches accept boolean values

    • Given items:
      typenamelabelstate
      SwitchTestSwitchTest Switch<initial_state>
    • And code in a rules file
      TestSwitch << <bool>
    • When I deploy the rules file
    • Then "TestSwitch" should be in state "<final_state>" within 5 seconds

    Examples:

    initial_state bool final_state
    OFF true ON
    ON false OFF
  • Switches respond to toggle

    • Given items:
      typenamelabelstate
      SwitchTestSwitchTest Switch<initial_state>
    • And code in a rules file
      # Invert all switches
      items.select { |item| item.is_a? Switch }
           .each   { |switch| switch.toggle }
    • When I deploy the rules file
    • Then "TestSwitch" should be in state "<final_state>" within 5 seconds

    Examples:

    initial_state final_state
    ON OFF
    OFF ON
    UNDEF ON
    NULL ON
  • Switches support ! (not) operator to invert the state

    • Given items:
      typenamelabelstate
      SwitchTestSwitchTest Switch<initial_state>
    • And code in a rules file
      # Invert all switches
      items.select { |item| item.is_a? Switch }
           .each   { |switch| switch << !switch }
    • When I deploy the rules file
    • Then "TestSwitch" should be in state "<final_state>" within 5 seconds

    Examples:

    initial_state final_state
    ON OFF
    OFF ON
    NULL ON
    UNDEF ON
  • Switches respond to grep

    • Given items:
      typenamelabelstate
      SwitchTestSwitchTest SwitchON
    • And code in a rules file
      items.grep(Switch)
           .each { |switch| logger.info("Switch #{switch.id} found") }
    • When I deploy the rules file
    • Then It should log 'Switch Test Switch found' within 5 seconds
  • Switch states work in grep

    • Given items:
      typenamelabelstate
      SwitchTestSwitchTest SwitchON
      SwitchTestSwitchTwoTest Switch TwoOFF
    • And code in a rules file
      items.grep(Switch)
           .grep(ON)
           .each { |switch| logger.info("#{switch.id} ON") }
      
      items.grep(Switch)
           .grep(OFF)
           .each { |switch| logger.info("#{switch.id} OFF") }
    • When I deploy the rules file
    • Then It should log "Test Switch ON" within 5 seconds
    • Then It should log "Test Switch Two OFF" within 5 seconds
  • Switch states work in cases

    • Given items:
      typenamelabelstate
      SwitchTestSwitchTest SwitchON
      SwitchTestSwitchTwoTest Switch TwoOFF
    • And code in a rules file
      items.grep(Switch)
           .each do |switch|
              case switch
              when ON
                logger.info("#{switch.id} ON")
              when OFF
                logger.info("#{switch.id} OFF")
              end
           end
    • When I deploy the rules file
    • Then It should log "Test Switch ON" within 5 seconds
    • Then It should log "Test Switch Two OFF" within 5 seconds
  • on/off sends commands to a switch

    • Given group "Switches"
    • Given items:
      typenamelabelstategroup
      SwitchTestSwitchTest Switch<initial_state>Switches
      SwitchTestSwitchTwoTest Switch Two<initial_state>Switches
    • And code in a rules file
        Switches.each(&:<command>)
    • When I deploy the rules file
    • Then "TestSwitch" should be in state "<final_state>" within 5 seconds
    • And "TestSwitchTwo" should be in state "<final_state>" within 5 seconds

    Examples:

    initial_state command final_state
    OFF on ON
    ON off OFF
  • on?/off? checks state of switch

    • Given group "Switches"
    • Given items:
      typenamelabelstategroup
      SwitchTestSwitchTest Switch<initial_state>Switches
      SwitchTestSwitchTwoTest Switch Two<initial_state>Switches
    • And code in a rules file
        Switches.select(&:<check>).each(&:<command>)
    • When I deploy the rules file
    • Then "TestSwitch" should be in state "<final_state>" within 5 seconds
    • And "TestSwitchTwo" should be in state "<final_state>" within 5 seconds

    Examples:

    initial_state check command final_state
    OFF off? on ON
    ON on? off OFF