Detect Mains Power and keep Domoticz synchronized

In this subforum you can show projects you have made, or you are busy with. Please create your own topic.

Moderator: leecollings

Post Reply
mjdb
Posts: 71
Joined: Thursday 12 January 2017 13:38
Target OS: Raspberry Pi / ODroid
Domoticz version: all beta
Location: NL - Pijnacker
Contact:

Detect Mains Power and keep Domoticz synchronized

Post by mjdb »

Have you ever wanted to detect if your Domoticz driven switch, light or power outlet was powered On or Off? Just to be sure? Or to know it at all?

Of course some equipment provides a return signal to indicate if a device is on, but 433 Mhz gear, like KaKu (Klik Aan Klik Uit) / CoCo (Click On Click Off) will not tell you at all and neither do classical power switches unless it is connected to a z-wave device with return channel.

Well, I have had that question many times, so I decided to make an easy 'non-invasive' helping aid to detect if a device was getting mains power or not.
WemosD1InHousing.jpg
WemosD1InHousing.jpg (17.64 KiB) Viewed 4554 times
It is based on the ESP8266 micro-controller. I choose the 'Wemos D1' version. It costs 6,50 Euro. As a power adapter, I took the 'Hi-Link PCB power-supply' (5 Euro). The trick is to power the ESP8266 on together with the device that must be monitored. Then the ESP8266 will send its boot-status, which is default 'On'.

In the following posts I will describe my monitor-method.
Last edited by mjdb on Monday 07 August 2017 11:56, edited 2 times in total.
- - - - - - - - - - -
2 x Domoticz on Raspberry Pi; 2 x RFXtrx433; Aeotec Z-Stick
KlikAanKlikUit ICS-2000 as Relay
Aeotec MultiSensors 6; Danfoss Z Thermostats 014G0013; Kaku Switches, Z-wave Switches
mjdb
Posts: 71
Joined: Thursday 12 January 2017 13:38
Target OS: Raspberry Pi / ODroid
Domoticz version: all beta
Location: NL - Pijnacker
Contact:

Detect Mains Power - Preparing the Wemos D1

Post by mjdb »

As 'operating system' I choose 'ESPEasy Version 2.10':
https://www.letscontrolit.com/wiki/index.php/Main_Page
Install the setup-program on your computer.

Then you start the tool FlashESP8266.exe and choose the version. I used 'ESPEasy_v2.0.0-dev10_normal_4096'. Of course, when you read this, there can be a more recent version available.
ESPEasyFlasher01.png
ESPEasyFlasher01.png (3.95 KiB) Viewed 4572 times
ESPEasyFlasher02.png
ESPEasyFlasher02.png (17.02 KiB) Viewed 4572 times
ESPEasyFlasher03.png
ESPEasyFlasher03.png (1.5 KiB) Viewed 4572 times
When the flashing is done, disconnect the Wemos D1 from your Computer and connect it to a seperate power adapter. I found that this makes it easier to perform the following steps.

ESPEasy will now start as a Wifi Access Point with network-name ESP_0.

Use (for example) your telephone to scan for the network 'ESP_0'. Connect to this access point with password: configesp. Then launch your internet browser (and perhaps press the home button) and it should find the active portal hosted by the ESP module.

From the screen 'Welcome to ESP Easy' choose your network and then type the access-password of that network. After clicking the connect button, ESP easy will connect itself to your network and get an IP address. Make a note of that IP-address.

Press the reset button on the Wemos D1.

If you don't see a screen with a new IP-address, try again, or search for the new device/IP-address in the connected devices list of your router.

Now, on your PC, start a browser and connect to the IP-address of the Wemos D1.
Last edited by mjdb on Monday 07 August 2017 11:40, edited 3 times in total.
- - - - - - - - - - -
2 x Domoticz on Raspberry Pi; 2 x RFXtrx433; Aeotec Z-Stick
KlikAanKlikUit ICS-2000 as Relay
Aeotec MultiSensors 6; Danfoss Z Thermostats 014G0013; Kaku Switches, Z-wave Switches
mjdb
Posts: 71
Joined: Thursday 12 January 2017 13:38
Target OS: Raspberry Pi / ODroid
Domoticz version: all beta
Location: NL - Pijnacker
Contact:

Detect Mains Power - Configure the Wemos D1

Post by mjdb »

When the connection to ESPEasy on you Wemos D1 was succesful, you will see a welcome screen. Click on 'Proceed to Main Config'.

Then configure several menu-tabs:
ESPEasyConfig.png
ESPEasyConfig.png (190.58 KiB) Viewed 4570 times
Menu --> Config

- Choose a unique Unit Name for your device and a Unit Number.
- If you have multiple ESP8266 devices in your network, they all should have a different number in the range 1 to 32.
- The Wifi settings are already set in the 'Flashing' fase. You don't have to change them.

Set the IP-settings, so the ESP8266 can connect to your lan.
- ESP IP: I suggest a fixed IP-address
- ESP GW: The gateway from your lan, usually the IP of your Router.
- ESP SUbnet: Usually 255.255.255.0
- ESP DNS: The Domain Name Server is also usually the Router.

Click Submit

Menu --> Controllers

Your controller will be the computer where you run your Domoticz instance.

- Controller IP: The IP address of your Domoticz computer
- Controller Port: Your Domoticz Port, usually 8080
- When this is on your local lan, you can leave the user and password empty
- Enable the controller

Click Submit

Menu --> Hardware

There will be no real sensor or any device connected to the Wemos D1. We will use one GPIO pin as a dummy device.

Pin Mode 12 (D6): Select 'Input'

Menu --> Devices

Click the Edit-button for Task number 1.

A sub-menu 'Task Settings' appears.

- Device: Select 'Switch input'.
- Name: This likely will be the name of the device you want to monitor.
- Enabled: Click the box.

- Internal Pull Up: Click the Box.
- Inverted Logic: leave empty.
- 1st GPIO: Select the dummy device you prepared in the hardware-menu: GPIO-12 (D6).
- Switch Type: Switch
- Switch Button Type: Normal Switch.
- Send Boot State: Click the box.

- Delay: Leave zero

Send to Controller: Here you enter the Domoticz-index-number of the Device you want to monitor. And you click the checkbox to enable it.

Click Submit.

Hardware --> Tools

When you click on 'Reboot', the Wemos D1 will indeed reboot.

You should now be able to re-connect with the fixed IP-address you entered before.
Last edited by mjdb on Monday 07 August 2017 11:44, edited 4 times in total.
- - - - - - - - - - -
2 x Domoticz on Raspberry Pi; 2 x RFXtrx433; Aeotec Z-Stick
KlikAanKlikUit ICS-2000 as Relay
Aeotec MultiSensors 6; Danfoss Z Thermostats 014G0013; Kaku Switches, Z-wave Switches
mjdb
Posts: 71
Joined: Thursday 12 January 2017 13:38
Target OS: Raspberry Pi / ODroid
Domoticz version: all beta
Location: NL - Pijnacker
Contact:

Detect Mains Power - Build the hardware

Post by mjdb »

Build the hardware

For the detection device I used:

- 'Wemos D1 Mini V2-ESP8266-12F - CH340
- 'Hi-Link PCB power-supply - 5VDC, 0.6A - HLK-PM01'
- Housing
- Line cord

You solder the line cord to the power-supply and the plus and minus to the Wemos D1.
WemosD1AndPowerSupply.jpg
WemosD1AndPowerSupply.jpg (22.26 KiB) Viewed 4569 times
Put it in the housing and use some double sided tape to keep it all in place.
WemosD1InHousing.jpg
WemosD1InHousing.jpg (17.64 KiB) Viewed 4569 times
Last edited by mjdb on Monday 07 August 2017 11:34, edited 1 time in total.
- - - - - - - - - - -
2 x Domoticz on Raspberry Pi; 2 x RFXtrx433; Aeotec Z-Stick
KlikAanKlikUit ICS-2000 as Relay
Aeotec MultiSensors 6; Danfoss Z Thermostats 014G0013; Kaku Switches, Z-wave Switches
mjdb
Posts: 71
Joined: Thursday 12 January 2017 13:38
Target OS: Raspberry Pi / ODroid
Domoticz version: all beta
Location: NL - Pijnacker
Contact:

Detect Mains Power - Build the hardware

Post by mjdb »

Moderator - Please delete this reply, it was double.
Last edited by mjdb on Monday 07 August 2017 11:46, edited 1 time in total.
- - - - - - - - - - -
2 x Domoticz on Raspberry Pi; 2 x RFXtrx433; Aeotec Z-Stick
KlikAanKlikUit ICS-2000 as Relay
Aeotec MultiSensors 6; Danfoss Z Thermostats 014G0013; Kaku Switches, Z-wave Switches
mjdb
Posts: 71
Joined: Thursday 12 January 2017 13:38
Target OS: Raspberry Pi / ODroid
Domoticz version: all beta
Location: NL - Pijnacker
Contact:

Detect Mains Power - Detect the 'On' State

Post by mjdb »

Detecting the 'On' state is easy as can be. You plug your Wemos D1 in the same power outlet as the device you want to monitor.

When this power outlet is switched on, the Wemos D1 will also power on and boot. It will then send the boot-state of the dummy-device to your controller. Since the dummy device is configured with the 'Internal Pullup' option, the boot-state will be 'On'.
The On state is send to the Domoticz-device you choose by the index-nummer you entered and so this device will be set to 'On'. The signal is send over Wifi and will be 100% certain to arrive.

This will happen when you power on your power outlet with a physical switch or from a over-the-air switch and your Domoticz controller 'misses' that switch signal. This is what you want.

Also, when the power outlet is switched from Domoticz itself, or when the over-the-air signal was already correctly received by Domoticz, the Wemos D1 will send an 'On' signal to Domoticz. This won't do any harm. It is just double.

Unfortunately, the 'Off' state is harder to detect in a similar way.

I tried many technical schemes to send an 'Off' signal when the Wemos D1 was powered down, just before it realy stops. I did not succeed.
It works when you have a second power-supply that is always on, but then the starting point to keep the detector as easy as possible would be abandoned.

The solution was to detect the 'Off' state with software.
- - - - - - - - - - -
2 x Domoticz on Raspberry Pi; 2 x RFXtrx433; Aeotec Z-Stick
KlikAanKlikUit ICS-2000 as Relay
Aeotec MultiSensors 6; Danfoss Z Thermostats 014G0013; Kaku Switches, Z-wave Switches
mjdb
Posts: 71
Joined: Thursday 12 January 2017 13:38
Target OS: Raspberry Pi / ODroid
Domoticz version: all beta
Location: NL - Pijnacker
Contact:

Detect Mains Power - Detecting the 'Off State

Post by mjdb »

When the power outlet is powered down, the Wemos D1 will also power down and stop running. This is something we can detect with software. When the Wemos D1 is off, the Device that is monitored is also off. That's the idea.

Create a LUA Event in Domoticz. Make it a Time-driven event. This will check every minute if the Wemos D1 is still on. This is done by sending a Ping request. If the Wemos D1 answers it is (still) On. If not, it is Off.

Change the DeviceToMonitorName and the ESP8266IP variables to your situation.

This is the LUA script:

Code: Select all

-- ESPMonitor

commandArray = {}

  -- The Name in Domoticz of the device from which we want to know
  -- if it is (still) On or Off.
  local DeviceToMonitorName = 'Werkk Bureaulamp (Pi2)'
  
  -- The IP address of the ESP8266 (Wemos D1) that is connected to
  -- the mains power of the Device we want to monitor.
  local ESP8266IP = '10.0.1.119'
  
  -- if the Ping gets an answer, PingResult will be true
  local PingResult = false

  -- It is only relevant to test the status of the ESP8266IP
  -- when the device that we are monitoring is (supposed to be) on.
  if (otherdevices[DeviceToMonitorName] == 'Off') then
    return
  end
  -- Note: Remember to make this a Time-driven event. 
  -- Otherwise it will flood your Domoticz-computer.
  
  -- We check if IP-device can be reached by sending a 'Ping'.
  -- If the Ping gets an answer, PingResult will be true.
  PingResult=os.execute('ping -c1 -w1 ' .. ESP8266IP)
  
  if PingResult then
    -- the ESP8266 (Wemos D1) gives an answer, so it is (still) On.
  else
    -- the ESP8266 (Wemos D1) gives no answer, so it is presumably Off.
    -- Consequently, we set the state of the DeviceToMonitor to Off.
    commandArray[DeviceToMonitorName] = 'Off'
  end -- no answer

return commandArray
If the ESP8266 (Wemos D1) gives no answer it is presumably Off.
Consequently, we set the state of the DeviceToMonitor to Off.
Last edited by mjdb on Monday 07 August 2017 11:48, edited 3 times in total.
- - - - - - - - - - -
2 x Domoticz on Raspberry Pi; 2 x RFXtrx433; Aeotec Z-Stick
KlikAanKlikUit ICS-2000 as Relay
Aeotec MultiSensors 6; Danfoss Z Thermostats 014G0013; Kaku Switches, Z-wave Switches
mjdb
Posts: 71
Joined: Thursday 12 January 2017 13:38
Target OS: Raspberry Pi / ODroid
Domoticz version: all beta
Location: NL - Pijnacker
Contact:

Detect Mains Power - Alternatives

Post by mjdb »

If you want a quick(er) response that the device is On or Off, you should leave the Wemos D1 powered on all the time and use a sensor to see if the Device were are monitoring is On or Off.

As sensor a light-sensitive resistor will do. You can build your own Opto-coupler by using a light that is put On or Off together with the Device to monitor. I used a so called neon light.

The light-sensitive resistors are fragile. Use some shrink-tube to strengthen it.
This is how you make your 220 volt opto-coupler:
Optocoupler.png
Optocoupler.png (105.67 KiB) Viewed 4568 times
Connect one of the wires of the light-sensitive resister to your Wemos D1. Best is one of the pins D5, D6, D7 or D8. Connect the other wire to ground.

This is the task-setting for the switch:
ESPEasyConfigureLightSensitiveResistor.png
ESPEasyConfigureLightSensitiveResistor.png (28.29 KiB) Viewed 4568 times
When the light-sensitive resistor gets no light, the resistence is high and there is no current flowing (to ground). The Internal PullUp resistor in the Wemos D1 makes that the GPIO pin is High. So the device seems to be On. But... because of the 'Inversed Logic' it is set Off.

When the light-sensitive resistor does get light, the resistance is low and it is connecting the GPIO-pin to ground. So the pin will be Low and the device seems to be Off. But again, because of the 'Inversed Logic' it is set to On.

This state will be immediately send to your Domoticz computer and the device with the corresponding Idx will be set accordingly.

When the device you are monitoring has a light-source of itself like a power-light, or perhaps because it is a light iself, you can place the light-sensitive-resistor on top of that light-source. Just be sure that it is realy dark when the device is off.
- - - - - - - - - - -
2 x Domoticz on Raspberry Pi; 2 x RFXtrx433; Aeotec Z-Stick
KlikAanKlikUit ICS-2000 as Relay
Aeotec MultiSensors 6; Danfoss Z Thermostats 014G0013; Kaku Switches, Z-wave Switches
niceandeasy
Posts: 102
Joined: Thursday 28 January 2016 22:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: NL
Contact:

Re: Detect Mains Power and keep Domoticz synchronized

Post by niceandeasy »

Interesting designs. I might build some of these, because I'm still a lot of euro away from replacing all of my 433MHz stuff for ZWave.

To solve it by classic electronic hobby solution: Use a KAKU keyfob, take it apart, create a 'capacitive dropper' power supply for it with quite a big storage capacitor and another with a small storage capacitor, for 'Off' event sensing. With some resistors/capacitors/transistors, you can simulate a keypress on the On button of the keyfob when the big capacitor reaches 3V. That will be just after power-up of this device. Some more resistors/capacitors/transistors can simulate a keypress on the Off button of the keyfob when the voltage over the small storage capacitor drops away. The 433MHz signal will get sent by the energy remaining in the big capacitor. Voltage on the big capacitor should be limited by a zener. Voltage over the small capacitor can be limited by a zener or just dissipate it.
This whole thing would be at live 230V voltage, so it should be mounted in a suitable case. For example, the case of a broken power adapter or a broken power plug switch device.
It is just a concept. No design available, no prototype ever built. But... no programming/scripting needed. Just a Domoticz master/slave relationship should do.
mjdb
Posts: 71
Joined: Thursday 12 January 2017 13:38
Target OS: Raspberry Pi / ODroid
Domoticz version: all beta
Location: NL - Pijnacker
Contact:

Re: Detect Mains Power and keep Domoticz synchronized

Post by mjdb »

Use a KAKU keyfob
I tried to avoid solutions that are based on sending more 433 Mhz Signals. This would polute the 433 Mhz-ether even more and so disturbing other 433 Mhz signals.
with quite a big storage capacitor
I had a prototype where a capacitor (2 x 4700 micro Fahrad) would keep the Wemos D1 'alive' for three extra seconds after the power-supply went down.
I used an Optocoupler on that power-supply with the switching-side on the Wemos D1. This was supposed to trigger the 'Off' State which should be transmitted in those remaining three seconds. I could measure the sequence of events as expected, however, the Off state was never send.
I assume the power of the capacitor(s) still was to little.
create a 'capacitive dropper' power supply
You would need the red capacitors as on the picture (Polypropyleen 220 n; 600 volt AC !), diodes, resistors and normal Capacitors to flatten the voltage. The price for the needed parts together would be the same, if not higher, as the price for the Hi-Link PCB power-supply (5 Euro).
So you could even use two Hi-Link power-supplies: One for powering the Wemos D1 and one to be switched On and Off with the Device To Monitor and so acting as an On/Off sensor.
Capacitors.jpg
Capacitors.jpg (52.98 KiB) Viewed 4461 times
- - - - - - - - - - -
2 x Domoticz on Raspberry Pi; 2 x RFXtrx433; Aeotec Z-Stick
KlikAanKlikUit ICS-2000 as Relay
Aeotec MultiSensors 6; Danfoss Z Thermostats 014G0013; Kaku Switches, Z-wave Switches
niceandeasy
Posts: 102
Joined: Thursday 28 January 2016 22:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: NL
Contact:

Re: Detect Mains Power and keep Domoticz synchronized

Post by niceandeasy »

You're right about 433MHz. I just had an issue with my rain meter. I replaced it for zwave and after that, I discovered that another device, somewhere in the neighborhood was sending erroneous info on the same 433MHz protocol/dev-id as my old rain meter was using. With 5 T/H-sensors, UV-sensor, door sensors, door bell, a load of KAKU-stuff, and a neighbor with 433MHz-devices, weird things can happen from time to time.

I must say, my classic elec hobby solution can easily be replaced by a ready-made KAKU AMWT-230. So, yes, it is kinda pointless, especially when you would need to buy all the parts. I'd probably salvage them from old electronics. And I didn't know about the Hi-Link PCB power-supply.
kimot
Posts: 105
Joined: Saturday 25 November 2017 17:18
Target OS: Raspberry Pi / ODroid
Domoticz version: v3.8153
Location: Czech Rep.
Contact:

Re: Detect Mains Power and keep Domoticz synchronized

Post by kimot »

And what about sending wemos status to Domoticz every minute - through Rules and Timers in ESPeasy side.
In domoticz LUA script check last_seen attribute and if nothing seen for 2 or 3 minutes, then we assumed, that power is off.
RPi2 Domoticz v 4.10717
10 x Sonoff Basic - ESPeasy
1 x Wemos D1 - ESPeasy
1 x Shelly Plus Plug S
1 x Sonoff S26 - ESPeasy
1 x Shelly 1
1 x MySensors HC-SR04
1 x MySenosrs wifi gateway
1 x RFLink
4x Cam IPC-T240H
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest