Switch and Dimmers Rule [3.4.4] [4.0.4]

This script allows you to unify the control of light through switches without making changes to the script every time you need to change the assignment of switches and target lamps.

“ECMAScript 262 Edition 11 (application/javascript; version=ECMAScript-2021)”

**Dependencies: **

  • Javascript Automation Add-in (ECMA2011)
  • Latest openhab-js installed
  • Buttons configured as Switches or Contacts (see “switchState” variable below) with custom metadata (read further)


It is necessary to place all switches in one group.

The script supports switches and dimmers. The detection occurs automatically, according to the type of lighting element (SwitchItem or DimmerItem).

Then you have to enter custom metadata for you switch Item, called linkedItem
For example:

value: testLight
  p2: testLightDimmer
  p3: testLightDimmer2
  hold: testSwitch4

Where properties in config section

  • value - main linked Item.
  • p2 - item to action with while 2 presses detected.
  • p3 - item to action with while 3 presses detected.

Property value is mandatory, while other configuration is optional.

If main linked Item, specified in value field, is DimmerType Item, then item specified in hold property will be ignored, because hold action used to control dimmerType.

How It works

One/Two/Three short press:

  • switching on/off conventional lamps (Switch).
  • turning on / off dimmers (Dimmer) with the restoration of the previous dimming level.

Long press:

If main linked Item (in value) is Switch

  • switching on/off conventional lamps (Switch).

If hold config specified - action performed:

  • switching on/off conventional lamps (Switch).
  • turning on / off dimmers (Dimmer) with the restoration of the previous dimming level.

If main linked Item (in value) is Dimmer

  • dimmers increase to 100% / decrease to ‘minDim’ values. If you release and press the button again, the dimming direction changes.

In this case hold config will be ignored.

Additionally, you can register any other actions for double / triple, etc. clicks by modifying the “shortPressAction” function.



Version 0.1

  • initial release

Version 0.2

  • code refactored a bit
  • support for additional configuration, possibility to add several items for 2, 3 presses, and for hold actions for switch items.

If you need to separately display information about all switches and what light sources are linked to them in the metadata, you can create a separate widget that will show the necessary information in a list.

uid: widget_96f79b9179
tags: []
  parameters: []
  parameterGroups: []
timestamp: May 23, 2023, 10:53:24 AM
component: f7-card
config: {}
    - component: oh-list
        mediaList: true
          - component: oh-repeater
              fragment: true
              for: item
              sourceType: itemsInGroup
              groupItem: gAllSwitches
              fetchMetadata: linkedItem
                - component: oh-list-item
                    accordionList: true
                    icon: ='oh:' + loop.item.category
                    title: =loop.item.label
                    subtitle: >
                      ="item Name: " + loop.item.name
                    footer: >
                      ="linked Item: " + loop.item.metadata.linkedItem.value
                    after: =loop.item.state

Notes: Based on Felix Krämer script, Icon Author


uid: lazygatto:switch_and_dimmers
label: Switch and Dimmers Rule
description: Allows you simply control of switch and dimmer lights through button switches.
  - name: switchesGroup
    type: TEXT
    context: item
      - name: type
        value: Group
    label: Switches Group
    required: true
    description: Group Item that holds all the Switches
  - name: delayMulti
    label: Short Press Delay
    description: Delay needed to register short presses. The delay is in milliseconds.
    type: INTEGER
    defaultValue: 350
    required: true
  - name: delayDimStep
    label: Dimmer Steps Delay
    description: Pause between dimmer steps will be performed. The delay is in milliseconds.
    type: INTEGER
    defaultValue: 100
    required: true
  - name: dimStep
    label: Dimmer Step Size
    description: Step to increase/decrease dimming level for one iteration.
    type: INTEGER
    defaultValue: 2
    required: true
  - name: minDim
    label: Dimmer Step Size
    description: Minimum level for dimming while pressing button.
    type: INTEGER
    defaultValue: 5
    required: true
  - name: switchStateON
    label: Switch State ON
    description: Set here 'ON' if use Switch Type Items and 'CLOSED' if you're using Contact Type Items.
    type: TEXT
    defaultValue: CLOSED
    required: true
  - name: switchStateOFF
    label: Switch State OFF
    description: Set here 'OFF' if use Switch Type Items and 'OPEN' if you're using Contact Type Items.
    type: TEXT
    defaultValue: OPEN
    required: true
  - name: namespace
    type: TEXT
    label: Linked Item Namespace
    required: true
    defaultValue: linkedItem
    description: The Item metadata namespace for enter linked Item name (e.g. "linkedItem").
  - id: "1"
      groupName: "{{switchesGroup}}"
      state: "{{switchStateON}}"
      previousState: "{{switchStateOFF}}"
    type: core.GroupStateChangeTrigger
conditions: []
  - inputs: {}
    id: "2"
      type: application/javascript;version=ECMAScript-2021
      script: >-
        Switch and Dimmers Rule

        Ver. 0.2

        The MIT License (MIT)
        Copyright © 2023 LazyGatto, 
        based on Felix Krämer script
        Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

        - Javascript Automation addon (ECMA2011)
        - Latest openhab-js installed
        - A button items configured as switches or contacts (see below 'switchState' variable) with custom metadata (read further)

        You have to place all your switches/contacts into one group, which one will be a trigger.
        Also you have to add custom metadata for all your switches/contacts 'linkedItem', with item name of light/dimmer it will control.

        Script supports switches and dimmers.
        For one short press:
        - turn ON/OFF switches 
        - turn ON/OFF dimmers with restoration of previous level of dimming.
        For long press:
        - switches will turn ON/OFF
        - dimmers will increase to 100 / decrease to minDim values, stop, press and hold again button will revert dimming direction.

        You can provide any custom actions for double/triple/etc presses by customizing 'shortPressAction' function and 'delayMulti'
        variable to detect multiple buttom presses.


        // ==== Configuration ====

        // Delays:

        var delayMulti = {{delayMulti}};   // registering multiple pushes

        var delayDimStep = {{delayDimStep}}; // pause between in-/decrease steps

        var dimStep = {{dimStep}}; // step to increase/decrease dimming level for one iteration

        var minDim = {{minDim}}; // minimum level for dimming

        var switchState = {
          ON: '{{switchStateON}}', // CLOSED for Contact Type
          OFF: '{{switchStateOFF}}' // OPEN for Contact Type

        // ==== END Configuration ====

        var switchTriggered = items.getItem(triggeringItem.name);

        var isObjectEmpty = (objectName) => {
          return JSON.stringify(objectName) === "{}";

        // Change the behaviour for different button presses:
        // stor.pressCount - button pressed times
        function shortPressAction(stor) {

          switch (stor.pressCount) {

            // 1 press to toggle light on/off
            case 1:

            // 2 presses. For now Item Toggle function is here. You can write your own.
            case 2:
              if ('p2' in stor.linkedItem) {
              } else {
                console.log('There is no action for 2 presses for ' + stor.switchTriggered.name)

            // 3 presses. For now Item Toggle function is here. You can write your own.
            case 3:
              if ('p3' in stor.linkedItem) {
              } else {
                console.log('There is no action for 3 presses for ' + stor.switchTriggered.name)

              console.log('Default case');

        function resetStorage(stor) {
          stor.btnTimer = null;
          stor.pressCount = 0;
          stor.dimTimer = null;
          cache.private.put(stor.cacheId, stor);

        function toggleItem(itemObj) {

          var stateToSet = null

          if (itemObj.state == 'OFF') {
            stateToSet = 'ON'
          } else if (itemObj.state == 0) {
            stateToSet = ("previousState" in itemObj) ? itemObj.previousState : 100;
          } else {
            itemObj.previousState = itemObj.state
            stateToSet = 'OFF'

        function dimItem(stor) {

          var dimToSet = null;

          //early exit
          if (stor.switchTriggered.state.toString() == switchState.OFF) {

          if (stor.dimUp == true) {
            dimToSet = Number(stor.linkedItem.state) + dimStep;
            if (dimToSet > 100) {
              dimToSet = 100

          } else {
            dimToSet = Number(stor.linkedItem.state) - dimStep;
            if (dimToSet < minDim) {
              dimToSet = minDim


          if (stor.dimTimer) {

        function getPressCountTimer(stor) {

          if (stor.switchTriggered.state.toString() == switchState.OFF) { // Short press cases


          } else { // Hold cases


            switch (stor.linkedItem.type) {

              case 'SwitchItem':
                if ('hold' in stor.linkedItem) {
                } else {

              case 'DimmerItem':
                // Set dim direction
                if (stor.linkedItem.state > 90) {
                  stor.dimUp = false;
                } else if (stor.linkedItem.state < 10) {
                  stor.dimUp = true;
                } else {
                  stor.dimUp = !stor.dimUp

                if (stor.dimTimer === null) {
                  stor.dimTimer = actions.ScriptExecution.createTimer(stor.dimTimerId, time.toZDT(), () => { dimItem(stor) });
                cache.private.put(stor.cacheId, stor);


        if (switchTriggered.state.toString() == switchState.ON) {
          var funcGenerator = function (switchTriggered) {
            var cacheId = switchTriggered.name + '_Id';

            switchTriggered.meta = items.metadata.getMetadata(switchTriggered, 'linkedItem');

            if (switchTriggered.meta == null) {
              console.error(switchTriggered.name + ' has no linkedItem namespace specified! Exit...')

            var linkedItem = items.getItem(switchTriggered.meta.value);

            if (!isObjectEmpty(switchTriggered.meta.configuration)) {
              if ("p2" in switchTriggered.meta.configuration) {
                linkedItem.p2 = items.getItem(switchTriggered.meta.configuration.p2)
              if ("p3" in switchTriggered.meta.configuration) {
                linkedItem.p3 = items.getItem(switchTriggered.meta.configuration.p3)
              if ("hold" in switchTriggered.meta.configuration) {
                linkedItem.hold = items.getItem(switchTriggered.meta.configuration.hold);

            var stor = cache.private.get(cacheId, () => ({
              'cacheId': cacheId,
              'switchTriggered': switchTriggered,
              'linkedItem': linkedItem,
              'btnTimer': null,
              'pressCount': 0,
              'dimTimer': null,
              'dimTimerId': linkedItem.name + '_dimTimerId',
              'dimUp': false,
            if (stor.btnTimer !== null) {
            stor.btnTimer = setTimeout(() => getPressCountTimer(stor), delayMulti);
    type: script.ScriptAction


Nice. This seems pretty awesome to manage rooms with multiple spot light groups that you’d want to control individually.
Unfortunately i don’t have a use case for this, but this makes life much easier for anyone who does.

Strange behaviour - 1st post updated, but in Marketplace I cannot see changes from some moment.
And firstly tried to add link to GitHub raw file.
In this case was unable to add template from Marketplace. Button ADD just pressed and template actually was not added.
BTW, is there possibility to see what’s wrong in some logs?