Elektroda.com
Elektroda.com
X

[Tutorial] Detached Mode for Openbeken wall switch

ferbulous 1149 8
  • This is a quick guide on setting up 'detached mode' with openbk flashed switches.
    This would continuously power your wifi/zigbee bulbs without turning it off when the switch is toggled.

    On your switch. you'll need to change the relay that's wired to your wifi bulbs to AlwaysHigh and assign a fake relay to any random unused pin.

    [Tutorial] Detached Mode for Openbeken wall switch

    P8 - Relay 1
    P14 - Button 1
    P9 - Fake relay

    [Tutorial] Detached Mode for Openbeken wall switch
    P8 AlwaysHigh 
    P9 Relay 1


    Add this script:

    
    //publish button actions to mqtt for button 1 (pin 14)
    addEventHandler OnClick 14  publish button click
    addEventHandler OnDblClick 14 publish button dblclick
    addEventHandler OnHoldStart 14 publish button holdstart
    addEventHandler OnHold 14 publish button hold
    addEventHandler OnRelease 14  publish button release
    //reassign pin 8 as relay when MQTT is offline
    addEventHandler MQTTState 1 backlog setPinRole 8 AlwaysHigh; setPinRole 9 Rel
    addEventHandler MQTTState 0 backlog setPinRole 8 Rel; setPinRole 9 AlwaysLow
    


    Now, you can toggle the switch and do some custom actions based on published mqtt button payload
    MQTT topic to subscribe

    <device name>/<channel number>/get
    <device name>/button/get

    Example for my device:

    living_room_switches/1/get
    living_room_switches/button/get


    Nodered flows for dimming & changing color temperature:
    [Tutorial] Detached Mode for Openbeken wall switch [
    Spoiler:
     
    [{"id":"18d64a1a715694fc","type":"mqtt in","z":"4cb2d8d4.f1ff18","name":"","topic":"living_room_switches/button/get","qos":"2","datatype":"auto-detect","broker":"da8d15474208fac9","nl":false,"rap":true,"rh":0,"inputs":0,"x":150,"y":780,"wires":[["4a5e5569a15691e5"]]},{"id":"4a5e5569a15691e5","type":"switch","z":"4cb2d8d4.f1ff18","name":"Switch","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"click","vt":"str"},{"t":"eq","v":"dblclick","vt":"str"},{"t":"eq","v":"hold","vt":"str"},{"t":"eq","v":"release","vt":"str"},{"t":"eq","v":"holdstart","vt":"str"}],"checkall":"true","repair":false,"outputs":5,"x":390,"y":780,"wires":[["9a0699ccd15bc40a"],["a7e3bedb345795e4"],["0b2252d48c11a5f6"],["5cfd4e38daddc201"],["bf794dd51f95e6d2"]]},{"id":"0bda89995a681890","type":"api-call-service","z":"4cb2d8d4.f1ff18","name":"toggle lights","server":"f4149317.d3794","version":5,"debugenabled":false,"domain":"light","service":"toggle","areaId":[],"deviceId":[],"entityId":["light.living_room_lights_prime"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1090,"y":620,"wires":[[]]},{"id":"bf794dd51f95e6d2","type":"api-call-service","z":"4cb2d8d4.f1ff18","name":"toggle_dimming","server":"10991ca8.1f1c03","version":5,"debugenabled":false,"domain":"input_boolean","service":"toggle","areaId":[],"deviceId":[],"entityId":["input_boolean.dim_switch"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","output_location":"","output_location_type":"none","x":540,"y":940,"wires":[[]]},{"id":"a7e3bedb345795e4","type":"api-current-state","z":"4cb2d8d4.f1ff18","name":"input cycle colors","server":"f4149317.d3794","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_select.cycle_colors","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":630,"y":720,"wires":[["44d99c8c08d5e1ee"]]},{"id":"44d99c8c08d5e1ee","type":"switch","z":"4cb2d8d4.f1ff18","name":"Switch","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Cool","vt":"str"},{"t":"eq","v":"Warm","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":810,"y":720,"wires":[["7316fa48add4a421"],["87c641df2d84020a"]]},{"id":"7316fa48add4a421","type":"api-call-service","z":"4cb2d8d4.f1ff18","name":"Cool","server":"f4149317.d3794","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.living_room_lights_prime"],"data":"{\"color_temp\":153}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":930,"y":700,"wires":[["03c552b5c13531ed"]]},{"id":"87c641df2d84020a","type":"api-call-service","z":"4cb2d8d4.f1ff18","name":"Warm","server":"f4149317.d3794","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.living_room_lights_prime"],"data":"{\"color_temp\":490}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":930,"y":760,"wires":[["03c552b5c13531ed"]]},{"id":"03c552b5c13531ed","type":"api-call-service","z":"4cb2d8d4.f1ff18","name":"","server":"f4149317.d3794","version":5,"debugenabled":false,"domain":"input_select","service":"select_next","areaId":[],"deviceId":[],"entityId":["input_select.cycle_colors"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1150,"y":720,"wires":[[]]},{"id":"0b2252d48c11a5f6","type":"api-current-state","z":"4cb2d8d4.f1ff18","name":"check dim","server":"f4149317.d3794","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_boolean.dim_switch","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":600,"y":800,"wires":[["5f94ea1ad5c9da8b"],["6034cf15d769023f"]]},{"id":"5cfd4e38daddc201","type":"change","z":"4cb2d8d4.f1ff18","name":"reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":880,"wires":[["5f94ea1ad5c9da8b","6034cf15d769023f"]]},{"id":"5f94ea1ad5c9da8b","type":"trigger","z":"4cb2d8d4.f1ff18","name":"","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"-250","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":860,"y":860,"wires":[["f4f5fd69f4ccefe2"]]},{"id":"6034cf15d769023f","type":"trigger","z":"4cb2d8d4.f1ff18","name":"","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"-250","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":860,"y":920,"wires":[["92cc63045a3ad078"]]},{"id":"92cc63045a3ad078","type":"api-current-state","z":"4cb2d8d4.f1ff18","name":"Check lights state","server":"22f5da40.d3e236","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.living_room_lights_prime","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","x":1090,"y":920,"wires":[["e07a0bcb27553333"],[]]},{"id":"f4f5fd69f4ccefe2","type":"api-current-state","z":"4cb2d8d4.f1ff18","name":"Check lights state","server":"22f5da40.d3e236","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.living_room_lights_prime","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","x":1090,"y":860,"wires":[["fbb46d6331d169ab"],[]]},{"id":"e07a0bcb27553333","type":"function","z":"4cb2d8d4.f1ff18","name":"Brightness -40","func":"var brightness = msg.data.attributes.brightness; //setup brightness and set to msg.payload\nbrightness = parseInt(brightness);\n\nif (brightness > 70) {\n    brightness = brightness - 40;\n} else {\n    brightness = 50;\n}\n\nmsg.payload = brightness;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1300,"y":920,"wires":[["8a533c20131b57b0"]]},{"id":"fbb46d6331d169ab","type":"function","z":"4cb2d8d4.f1ff18","name":"Brightness +30","func":"var brightness = msg.data.attributes.brightness;\nbrightness = parseInt(brightness);\n\nvar dimmSize = 30;  //ONLY CHANGE THIS VALUE IF YOU WANT A DIFFERENT DIMM SPEED\nvar maxBrightness = 255;\nif (brightness <= maxBrightness - dimmSize) {\n    brightness += dimmSize;\n} else {\n    brightness = maxBrightness;\n}\n\nmsg.payload = brightness;\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1300,"y":860,"wires":[["44b27c64043219f4"]]},{"id":"8a533c20131b57b0","type":"api-call-service","z":"4cb2d8d4.f1ff18","name":"Light On","server":"22f5da40.d3e236","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.living_room_lights_prime"],"data":"{    \"entity_id\": \"light.bedroom_1_light\",    \"brightness\": {{payload}}}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","output_location":"","output_location_type":"none","x":1500,"y":920,"wires":[[]]},{"id":"44b27c64043219f4","type":"api-call-service","z":"4cb2d8d4.f1ff18","name":"Light On","server":"22f5da40.d3e236","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.living_room_lights_prime"],"data":"{    \"entity_id\": \"light.bedroom_1_light\",    \"brightness\": {{payload}}}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","output_location":"","output_location_type":"none","x":1500,"y":860,"wires":[[]]},{"id":"9a0699ccd15bc40a","type":"api-current-state","z":"4cb2d8d4.f1ff18","name":"Check lights","server":"f4149317.d3794","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.living_room_lights_prime","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":550,"y":660,"wires":[["ca3d5ec50c904c8b"],["fa29de278dcf515f"]]},{"id":"fa29de278dcf515f","type":"api-call-service","z":"4cb2d8d4.f1ff18","name":"Lights off","server":"f4149317.d3794","version":5,"debugenabled":false,"domain":"light","service":"turn_off","areaId":[],"deviceId":[],"entityId":["light.living_room_lights_prime"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":780,"y":660,"wires":[[]]},{"id":"ca3d5ec50c904c8b","type":"api-call-service","z":"4cb2d8d4.f1ff18","name":"Lights on","server":"f4149317.d3794","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.living_room_lights_prime"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":780,"y":620,"wires":[[]]},{"id":"8a20507042a29c50","type":"mqtt in","z":"4cb2d8d4.f1ff18","name":"","topic":"living_room_switches/1/get","qos":"2","datatype":"auto-detect","broker":"da8d15474208fac9","nl":false,"rap":true,"rh":0,"inputs":0,"x":130,"y":700,"wires":[["46d961b14905441e"]]},{"id":"46d961b14905441e","type":"switch","z":"4cb2d8d4.f1ff18","name":"Switch","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":530,"y":600,"wires":[["ca3d5ec50c904c8b"],["fa29de278dcf515f"]]},{"id":"da8d15474208fac9","type":"mqtt-broker","name":"MQTT hass","broker":"192.168.1.40","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"f4149317.d3794","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true},{"id":"10991ca8.1f1c03","type":"server","name":"Home Assistant","addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":"","statusSeparator":"","enableGlobalContextStore":false},{"id":"22f5da40.d3e236","type":"server","name":"Home Assistant","addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"","statusSeparator":"","enableGlobalContextStore":false}]
    


    1) Create input boolean for dimming (input_boolean.dim_switch)
    2) Add a new input helper dropdown for 'cycle colors'
    [Tutorial] Detached Mode for Openbeken wall switch

    Brief summary of the flow
    a. single click to toggle lights on/off
    b. 'holdstart' payload would toggle 'input_boolean.dim_switch' that switches between dimming or increasing brightness
    c. hold action (with trigger node) would continuously increase/decrease brightness every 250ms
    d. release payload sends reset payload to stop the trigger node thus stopping the dimming action
    f. double click to change from warm or cold temperature
    e. toggle channel 1 relay to toggle on/off for the lights

    Demo:




    As you can see, the led indicator for switch 1 is always on, that's because for my switch, the led is tied to the relay.
    There are other switches in the market with its dedicated pin for led such as these:

    https://templates.blakadder.com/DS-102_3.html
    https://templates.blakadder.com/milfra_TB12.html

    Alternative way would be to use device group to perform all the actions above without relying on homeassistant, however device group isn't quite stable for my usage yet as I continue testing it.

    I'm sure there are other ways of setting 'detached' mode, anyway this is my approach.
    Your ideas are welcome

    Cool? Ranking DIY
    About Author
    ferbulous
    Level 16  
    Offline 
    ferbulous wrote 312 posts with rating 37, helped 5 times. Been with us since 2022 year.
  • Helpful post
    #2
    p.kaczmarek2
    Moderator Smart Home
    Very clever way to get around the device limitations, although with the latest OBK updates (that weren' t in place at the time of creating automation from the first post) it should be possible to do it fully in script....

    Double click to change temperature...

    Let me see if I can come up with something similar....

    Ok ok, it will not be the same, I will use a simple CW strip simulation as an example:
    
    
    // 
    // SetButtonTimes [ValLongPress] [ValShortPress] [ValRepeat]
    // Each value is times 100ms.
    // Defaults:
    //  SetButtonTimes 10 3 5
    // more responsive hold:
    SetButtonTimes 10 3 5
    
    // shortcut for command. Repetas time is 10 sec, repeats count is 1
    alias add_turnoff_event addRepeatingEvent 10 1 led_enableAll 0
    
    alias do_brightness_action add_dimmer 5 1
    alias do_temperature_action add_temperature 5 1
    alias do_hold_action if $CH10 then do_brightness_action else do_temperature_action
    
    // toggles light state - not needed, done automatically
    addEventHandler OnClick 7 led_enableAll toggle
    // channel 10 describes mode - changing temperature or brightness
    addEventHandler OnDblClick 7 toggleChannel 10
    // turn on and do add_dimmer 5 with bWrapInteadOfClamp enabled
    addEventHandler OnHold 7 backlog led_enableAll 1; do_hold_action 
    

    Code above does:
    - click turns on/off the strip
    - double click changes hold mode (adjust temperature or dimmer)
    - hold adjusts (as above) temperature or dimmer
    Simulation:



    It uses ScriptOnly button so it doesn't tries to toggle anything automatically, all is scripted:
    [Tutorial] Detached Mode for Openbeken wall switch

    If you look at my code, you can see how I handled determining whether we are in Dimmer adjust mode or a Temperature adjust mode:
    
    alias do_brightness_action add_dimmer 5 1
    alias do_temperature_action add_temperature 5 1
    alias do_hold_action if $CH10 then do_brightness_action else do_temperature_action
    


    I've been wondering if the same approach could be used for your case with checking MQTT state?

    
    alias do_hold_action if MQTTOn then do_some_action else do_other_action
    


    but I am just here showing some random ideas, in general, your solution is good.
  • #3
    ferbulous
    Level 16  
    Indeed, all those action are doable in script
    I did try implementing with device group earlier, but I was still getting 'dgr socket error', device group randomly not broadcasting/receiving or just the black led (light is on, but no light, need to select color temp/colors to get it to light up)
    So I went back to using nodered for now

    script for brightness & color change on openbk
    
    addEventHandler OnHoldStart 22 SetChannel 6 0
    // when Hold repeats, add a 10 value as a step
    // AddChannelSyntax: channelindex delta minValue maxValue
    // the 0 255 is needed so we clamp the value to 255 
    addEventHandler OnHold 22 backlog AddChannel 6 15 0 255; DGR_SendBrightness cornerlamp $CH6
    // send on/off command to cornerlamp device group
    addChangeHandler Channel4 == 0 DGR_SendPower cornerlamp 0 1
    addChangeHandler Channel4 == 1 DGR_SendPower cornerlamp 1 1
    // double click to change color temp
    setChannel 5 1
    addEventHandler OnDblClick 22 addChannel 5 1
    addChangeHandler Channel5 == 1 DGR_SendRGBCW cornerlamp 255, 255, 255
    addChangeHandler Channel5 == 2 DGR_SendRGBCW cornerlamp 255, 0, 0
    addChangeHandler Channel5 == 3 setChannel 5 1
  • #4
    p.kaczmarek2
    Moderator Smart Home
    I am using DGR to control LED strips and it seem reliable for me, but we will investigate more.

    But for now, have you considering replacing DGR_SendBrightness with SendGET?

    Maybe it could work....
  • #5
    ferbulous
    Level 16  
    @p.kaczmarek2
    SendGET didn’t work when i tried replacing DGR_SendBrightness
    I did put a request if channel variable can be passed to the url
  • #6
    p.kaczmarek2
    Moderator Smart Home
    @ferbulous it was already granted, from what I can tell. Almost immediately. The target URL is now expanding the $CH11 etc etc syntax.
  • #7
    ferbulous
    Level 16  
    p.kaczmarek2 wrote:
    it was already granted, from what I can tell. Almost immediately. The target URL is now expanding the $CH11 etc etc syntax.


    Thanks, I haven't checked that yet
    Example for the URL to utilize that?

    addEventHandler OnHoldStart 22 SetChannel 6 0
    // when Hold repeats, add a 10 value as a step
    // AddChannelSyntax: channelindex delta minValue maxValue
    // the 0 255 is needed so we clamp the value to 255 
    addEventHandler OnHold 22 backlog AddChannel 6 15 0 255; SendGet http://192.168.1.134/cm?cmnd=led_dimmer%20$CH6
  • #8
    p.kaczmarek2
    Moderator Smart Home
    The following example looks correct, altough I do not know if our software is currently capable of sending GET packet, every, how often does "hold event" repeat, 300ms? You might need to make repeats less frequent, but I am not sure.
    In general, just check in practise, I will also check as well.

    It's just that SENDGET is more "expensive" than UDP send
  • #9
    ferbulous
    Level 16  
    p.kaczmarek2 wrote:
    It's just that SENDGET is more "expensive" than UDP send

    Indeed, it's probably not suitable especially if you have multiple bulbs