Configure a push-button in Domoticz and script it with dVents (LUA)

This post describe how to install a pushbutton with a Raspberry Pi. After the installation we add it in Domoticz and with the Domoticz event system we use the switch to toggle the relay we installed in the post ‘Raspberry Pi Relay module‘ .

Supplies needed:

  • A push button (or some jumper wires)
  • A resistor (1k Ohm)
  • Female – Female jumper wires
  • A raspberry Pi offcourse
  • Installed with Raspbian and domoticz

Step 1: Connect the button to the Raspberry Pi

I use a breadboard to connect the push botton with the Raspberry Pi.
The 1k Ohm resistor is to protect the raspberry Pi.

Raspberry
PIN1 – 3.3V
PIN36 (GPIO16)

Step 2: Configure the Raspberry Pi GPIO

We need to export GPIO16 (PIN36) so it can detect by Domoticz. In the post relay and GPIO in Domoticz we can see how to export pins in Domoticz. We created a file ‘setGpio.sh’ in the user folder. We edit this file and add the following lines.

#Push button, pump
/usr/bin/gpio export 16 in
/usr/bin/gpio edge 16 both

We export the pin as in (input). The edge is to enable edge-triggered interrupts via the /sys/class/gpio interface. Restart the Pi to enable the exports (or execute the file once)

Step 3: Add the push button to Domoticz

If you have the General GPIO enabled in the hardware you should see the Generic GPIO (16) in the device list. Enable this device.

Step 4: Add a Domoticz event to toggle another device

In domoticz goto Setup -> More Options -> Events.
Create a new event of the type ‘dzVents’.

Here you see the code to toggle the switch. The higher purpose of this project is to automate a swimmingpool. For the pool we have a pump and a desinfection unit (salt electrolyse). The pump is controlled by Domoticz, but for maintenance i want a push button to toggle the state off the pump. However, if the desinfection is running, the pump cannot turned off.

return {
   on = {
      devices = {
         'Manual toggle pump'
      }
   },
   data = {
      ManualPompSwitchCanSwitch = { initial = 1 }
   },
   execute = function(domoticz, switch)
       domoticz.log('Manual toggle pump triggert, state: ' .. switch.state)
       domoticz.log('current can switch: ' .. domoticz.data.ManualPompSwitchCanSwitch)
       
        if (domoticz.data.ManualPompSwitchCanSwitch == 1) then
           domoticz.log('can-switch is on. toggle pump state')

           domoticz.data.ManualPompSwitchCanSwitch = 0
           
           if (domoticz.devices('Pump').state == 'On') then
                domoticz.log('Pump is going off')
                if (domoticz.devices('Desinfection').state == 'On') then
                   domoticz.log('Pomp cant go off, desinfection is on')
                else
                    domoticz.devices('Pump').switchOff()
                end    
           else
               domoticz.log('Turn pump on')
               domoticz.devices('Pump').switchOn()
           end
           
        elseif (switch.state == 'Closed') then
            domoticz.data.ManualPompSwitchCanSwitch = 1
            domoticz.log('can-switch on again')
        end 

   end
}

About the author

Comments

  1. Hey There. I found your blog using msn. This is an extremely well written article.
    I will be sure to bookmark it and return to read more of your useful information. Thanks for
    the post. I will certainly comeback.

  2. Wow, this paragraph is good, my younger sister is analyzing such things,
    therefore I am going to convey her.

Comments are closed.