logo elektroda
logo elektroda
X
logo elektroda

Useful scripts for Domoticz. Dzvent''''s, LUA, Node Red, etc.

xury 30735 39
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 17750955
    xury
    Automation specialist
    Posts: 7071
    Help: 876
    Rate: 1486
    I thought it would be useful to share useful scripts for Domoticz so I will be posting my scripts here. I also encourage others to share their scripts here.
    Here is my first script. It is a DzVents script that controls a lamp in my room.
    The lamp is connected to a Sonoff Basic and is controlled via the Integra's PIR sensor.
    The lamp only lights up if it detects movement at night and runs for up to two minutes after the last movement is detected. During the day, the lamp is not controlled by the PIR but manually via the domoticz switch panel.
    Of course, the names have to be adapted to suit your needs.
    Code: Lua
    Log in, to see the code
    .

    The next script sends information about the operating status of the gas boiler to the Influxdb database. The information is downloaded via the Satel Integra control panel, the input of which is connected to the output on the De Dietich MCR II boiler.
    Code: Lua
    Log in, to see the code
    .

    We have three PZEM004t and want to have all three current values on one virtual one and a graph of the current from the three phases:
    Code: Lua
    Log in, to see the code
    .

    Similarly as above from the same PZEM004t meters we want to sum up the total energy consumption from the three phases.
    Code: Lua
    Log in, to see the code
    .
  • ADVERTISEMENT
  • #2 17941615
    xury
    Automation specialist
    Posts: 7071
    Help: 876
    Rate: 1486
    With a call button and a camera pointed at the gate, it is possible to monitor who is calling at the gate on a smartphone.
    To do this we need the Telegram application and two scripts:
    One is a Linux bash script and the other is a dzvents script, which runs the first one when the bell button is pressed:
    We save the first script in e.g. /home/pi/domoticz/scripts/, of course giving execution rights:
    Code: Bash
    Log in, to see the code
    .
    Additionally, we need to have ffmpeg installed - i.e. we install:
    Code: Bash
    Log in, to see the code
    on a raspberry or other hardware where we have domoticz.
    The second script is already Domoticz's native dzvents script:
    Code: Lua
    Log in, to see the code
    .
    When we have executed everything correctly, a push notification to Telegram with a five-second video from the selected camera should arrive when the bell is pressed.
    I am assuming that the Telegram app and its ID and APIkey is configured correctly.

    The next script controls the lighting in the garage, using a PIR sensor and a reed switch mounted on the sectional door:

    Code: Lua
    Log in, to see the code
    .
    The operation is as follows:
    When the garage door starts to open, the reed switch is tampered with and if the time of day is within the set range, the light on the actuator ( in my case it's the relay on the ESP-01) comes on. If the PIR sensor does not detect movement then the light will go out after about 10 minutes. It will also go out when the gate closes and the reed switch is shorted.
  • #3 17946078
    igorrr
    Level 15  
    Posts: 481
    Help: 9
    Rate: 136
    Do you have to install the Telegram app on the RPI? I have Domoticz installed there.
  • #4 17946092
    xury
    Automation specialist
    Posts: 7071
    Help: 876
    Rate: 1486
    You install Telegram on your phone. In domoticz you need to enter your ID and API key to have notifications. In the script there is a link to the Domoticz Wiki on how to get your ID and API key
    And this is what the screen shot from Telegram looks like:
    Useful scripts for Domoticz. Dzvent''''s, LUA, Node Red, etc. .
  • ADVERTISEMENT
  • #5 18521592
    xury
    Automation specialist
    Posts: 7071
    Help: 876
    Rate: 1486
    It is now the turn of the Node-Red script. I give to Domoticz the current coronavirus statistics for Poland.

    Screenshot showing Node-Red script for collecting COVID-19 data for Poland and sending it to Domoticz. .
    Code: JSON
    Log in, to see the code
    .
    It is also possible to change the country.
    Domoticz you need to create 3 pieces of dummy custom sensor.
    Of course the IDXs have to be changed to your own.
    Domoticz sensor charts displaying Covid-19 statistics: cases, deaths, and recoveries in Poland. .
  • #6 18620730
    xury
    Automation specialist
    Posts: 7071
    Help: 876
    Rate: 1486
    Another cool script I wrote for controlling an Android TV.
    To be exact, it's a TCL 65EP640 model. However, I think it will work with almost any and various TV BOXes.
    To work, you need to enable programmer mode and USB debugging. Everyone must find for themselves how to do this on their TV.
    USB debugging must be enabled to connect via TCP using adb (android debug bridge). The adb server has to be installed.
    Connection via USB cable is not necessary. The flow given is an example of manual control and via node nora, which allows voice control.

    Useful scripts for Domoticz. Dzvent''''s, LUA, Node Red, etc. .
    Code: JSON
    Log in, to see the code
    .
  • #7 19193707
    xury
    Automation specialist
    Posts: 7071
    Help: 876
    Rate: 1486
    Displaying text in the image of a Hikvision camera or DVR .
    For a long time I have had a preview of the temperature of the external sensors displayed in the image of the Hikvision camera.
    It looks more or less like this:
    Hikvision camera image showing a winter yard with overlaid temperature data. .
    As you can see, the temperature from the ds18b20 sensors connected to the esp8266 is displayed in the bottom right corner.
    Then, using Node-Red, the temperature data is sent to the IP camera or DVR and attached to the main stream image.
    The following simple flow based on Hikvision ISAPI sends temperature data. This method can be used, for example, to send from a cash register or any other data source.
    Code: JSON
    Log in, to see the code
    .
    In the settings of the IP camera or DVR, the number of lines of overlaid text required should be selected in the OSD Image Settings menu.
    The position of the text can be adjusted in the function node - positionX and positionY tags.
    In my case, the temperature data are taken from the global variables twschod i tzachod and this requires customisation. They can be sent via payload, for example. All it takes is a moment to learn javascript.
    You can do much more with Hikvision ISAPI. If you are interested, I refer you to a web search for a six hundred page pdf with a description of the protocol.
    [Edited]
    Well, from now on the matter has become much simpler, because at my request my colleague
    Supergiovane has made a node that knows how to do this much more simply.
    It also has lots of other cool features:
    https://flows.nodered.org/node/node-red-contrib-hikvision-ultimate
  • #8 19586532
    ozogp
    Level 7  
    Posts: 15
    @xury very cool script "current coronavirus statistics for Poland".
    Everything works fine, I don't know why on the screen I give such a value as below, in devices also wrong but in node-red messages and Domoticz logs is the correct value
    screen
    Screenshot displaying incorrect COVID-19 case numbers in Poland.
    Domoticz log:
    2021-08-29 22:01:59.591 MQTT: Topic: domoticz/in, Message: {"command": "udevice", "idx":313, "nvalue":0, "svalue": "2888231"}
    Any ideas? Thank you in advance
  • #9 19648428
    Daro1003
    Level 34  
    Posts: 2711
    Help: 295
    Rate: 603
    Hey mate @xury I have a request for a dzVents script I have no idea about creating them and probably won't have because somehow I don't get into writing codes or I haven't yet come across a tutorial that explains it well from the beginning.

    Lighting controlled by Sonoff 4Ch
    Motion detection with pir detector from Integra 128 plus - integration done works.

    Operation of the script:
    1. the detector detects movement and lights up for a set time each subsequent violation resets the time and counts again after the time has elapsed if the detector does not detect movement the light goes out. So something like in your first script.
    2) I switch the lighting on with a local (bell) switch on the wall or from Domoticz and the lighting switches on for the set time but other than in point 1 every violation of the detector resets the time after which the light goes out.

    I am combining this on integra inputs and outputs and blockly in domoticz but it works out poorly.
  • #10 19648498
    ozogp
    Level 7  
    Posts: 15
    Hello @Daro1003 ,

    ad1. something like this should help you, as for pt. 2 I don't really understand the point of using it.
    Code: Lua
    Log in, to see the code
    .
  • #11 19648508
    Daro1003
    Level 34  
    Posts: 2711
    Help: 295
    Rate: 603
    ozogp wrote:
    what pt. 2 I don't really understand the point of it.


    Well I have rooms such as the garage, boiler room, pantry wardrobe where I need such a script at my place we are used to using switches from the old traditional installation where my wife turns on the switch and not off and the light is on such a script will turn it off after say 10 minutes.
  • #12 19648519
    ozogp
    Level 7  
    Posts: 15
    I understand, but that's what gets you through it.
    If you're caught by a detector it lights up for 5 minutes, if you turn on the physical switch and you're caught by a detector in a minute, it lights up for 5 minutes too, so that takes care of it for you.
  • ADVERTISEMENT
  • #13 19648528
    Daro1003
    Level 34  
    Posts: 2711
    Help: 295
    Rate: 603
    return {
    on = {
    devices = { 'Garage',
    'Pir detector'}
    },

    This is how I should substitute pir and link ?

    And it's not possible to embrace this at two different times ?
  • ADVERTISEMENT
  • #14 19648542
    ozogp
    Level 7  
    Posts: 15
    local PIR = domoticz.devices('PIR_M') - PIR_M - name of your motion detector
    local Lamp = domoticz.devices('S_M') - S_M - name of the switch which realizes the switching on of the desired light reception

    What software for Sonoff? Original, Tasmota, EspEasy?
  • #15 19648550
    Daro1003
    Level 34  
    Posts: 2711
    Help: 295
    Rate: 603
    On Sonof AFE Firmware Version T0-3.0.2
    But paired with Domoticz everything works.
    I would prefer two different times for switching on via detector shorter for via switch longer.
  • #16 19648856
    ozogp
    Level 7  
    Posts: 15
    With two sometimes I can't help you because I don't know how to do it.

    local PIR = domoticz.devices('PIR_M') - PIR_M - name of your motion detector
    local Lamp = domoticz.devices('S_M') - S_M - name of the switch from Domoticz that performs the switching on of the desired light reception
  • #17 19648859
    Daro1003
    Level 34  
    Posts: 2711
    Help: 295
    Rate: 603
    Ok thanked @ozogp for your willingness your script I will save because it is also ok and will probably come in handy.
  • #18 19648933
    ozogp
    Level 7  
    Posts: 15
    @Daro1003 no problem.
    The script is not mine, I only customized it for my needs but if it's useful I'm happy.
  • #19 19662345
    ozogp
    Level 7  
    Posts: 15
    I have a request for help, I am using the script below, everything works fine.
    The light comes on, after the detector is violated after dusk for 10 min.
    However, I need a modification so that the light turns on on movement of the sensor after dusk:
    - but from 00:00 to 6:00 it would turn off after 10 minutes,
    - from 00:00 to 6:00 it is switched off after 10 minutes, and from 00:00 to 23:59 it is not switched off automatically but on the button.

    I sincerely ask for help with the modification.
    Thank you in advance

    Code: Lua
    Log in, to see the code
  • #20 19662578
    xury
    Automation specialist
    Posts: 7071
    Help: 876
    Rate: 1486
    I have no way of testing now:
    Code: Lua
    Log in, to see the code
    .
  • #21 19669233
    ozogp
    Level 7  
    Posts: 15
    @xury thank you very much for writing back.
    Unfortunately this code doesn't work as I would expect, yes the light turns on, but it doesn't automatically turn off by itself after x minutes and at the specified times.
    The light is on non-stop.
    I have looked through the documentation related to time attributes, everything looks correct as you wrote @xury, but something is not working.
    I have no idea what is wrong.
  • #22 19669370
    xury
    Automation specialist
    Posts: 7071
    Help: 876
    Rate: 1486
    And I think I already know what the issue is. There's no trigger to set it off. I'll try to correct it soon.

    Code: Lua
    Log in, to see the code
    .
  • #23 19737772
    joy
    Level 21  
    Posts: 429
    Help: 30
    Rate: 45
    Can you help me, I need a script, twilight sensor integra, plus 2 outdoor Pir sensors will be a third. Six points of light. It's supposed to work like this, at dusk after the last violation, e.g. 2 minutes, all the lighting goes out except for the LED at the door controlled between dusk-23, but after that time it's supposed to light up with the others. Can the lighting on and off be done sequentially, lighting is certainly from the point closest to the PIR.
  • #24 19771781
    ozogp
    Level 7  
    Posts: 15
    Good morning,
    maybe someone can give a hint, I've been struggling with the topic for a few days now but no longer have any idea how to do it in Domoticz. I have a reading from an energy meter (L1) via RS485 and Modbus RTU / ASCII / TCP/IP plug-in - READ v2020.2F - it's from this Device and displays nicely, but I would like to insert a Dummy Device as an incremental meter e.g. energy let's call it L2.
    I am having trouble how to pass the current value of L1 to L2, of course L1 is variable.
    Do you perhaps have any idea how to implement this?
    Thank you in advance for your help.
  • #25 19773793
    xury
    Automation specialist
    Posts: 7071
    Help: 876
    Rate: 1486
    Exactly what value do you need to extract?
    Code: Lua
    Log in, to see the code
    .
    This pastes into the new dzvents minimal script deleting everything that appears there when you select it. The names must be the same as yours. Alternatively instead of names idx numbers without apostrophes.
  • #26 19776215
    ozogp
    Level 7  
    Posts: 15
    Good evening,
    @xury thank you very much for your reply. I need to read the current consumption and transfer to the new Dummy Switch device, which will show the current consumption value and add up the daily totals. This will be viewable in Reports - for specific months, year etc.
    Of course, I have a graphana but it would be nice to have everything in one place.

    In terms of your code - I executed as you wrote but it blew up when reading dzVents events in the logs Screenshot of error logs related to Domoticz program. .
    and this happened over and over again even though I kicked out of the events, zdysku. I had a copy of Container so somehow it worked. But something didn't snap in this code.
  • #27 19779452
    xury
    Automation specialist
    Posts: 7071
    Help: 876
    Rate: 1486
    ozogp wrote:
    transfer to new Dummy Switch device
    .
    I think you have confused the devices. Dummy Switch is a switch.
    Write exactly what type of virtual sensors you have.
    Sensors in my script
    'Energy use' - instant + counter
    'Current counter' - counter
    The attribute must match the sensor type.
  • #28 20403849
    xury
    Automation specialist
    Posts: 7071
    Help: 876
    Rate: 1486
    Flow, which allows data from an ordered PGE daily report to be loaded into the influxdb database. Flow reads the data on energy taken, given up and balanced from the CSV file. The report can be ordered on PGE's BOK website. Available range from 1.04.2022 to 31.12.2022, this gives over 19,000 records to add. Flow saves the data of hourly records with the correct timestamp, so that the data goes to the right place in the past. As there are quite a few requests, I've done a limit on sending to one value per second. It's best to leave it overnight and let it add up slowly. If you have a powerful machine on which you have influxdb, you can increase the number of requests. Alternatively, you can save to a txt file and upload according to the scheme described on the influxdb helpdesk.

    Comparing the data to that collected by MEW-01 shows that they agree almost perfectly.

    To work properly you need a CSV file ordered from PGE, match file paths and influxdb API link data.
    https://flows.nodered.org/flow/c6e3b42319c72c290a1c8c3f14bb38f2
    Useful scripts for Domoticz. Dzvent''''s, LUA, Node Red, etc. .
  • #29 20803902
    Maciek1008
    Level 11  
    Posts: 20
    Rate: 4
    Could someone help with writing a script that subtracts the values read from two water meters? I have two water meters in my house; one main meter for the whole house and garden, and a separate one just for the garden. I am concerned with calculating the difference between the readings of the main water meter and the one for the garden. I know that this is probably a trivial script for people familiar with dzVents, but somehow I am not familiar with dzVents. I tried to write something based on the script given in this thread, which sums up the energy consumption from three phases, but I failed.
  • #30 20804258
    xury
    Automation specialist
    Posts: 7071
    Help: 876
    Rate: 1486
    Write what kind of counters are in domoticz and their idx's.

FAQ

TL;DR: Domoticz writes new sensor values to its internal SQLite database every 5 minutes [Elektroda, xury, post #21444762] “Writing faster than 5 minutes needs source recompilation” [Elektroda, xury, post #21444762]

Why it matters: knowing native limits avoids unnecessary SD-card wear and lost datapoints.

Quick Facts

• Default Domoticz database commit interval: 300 s (fixed) [Elektroda, xury, post #21444762] • dzVents ‘every minute’ timer fires 1 440 times per day [dzVents Wiki] • PZEM-004T measures 0–20 A RMS, ±1% typical accuracy (PZEM-004T v3 datasheet) • Sonoff Basic relay handles 10 A/2.2 kW, price ≈ US $6 [ITEAD, 2024] • Telegram Bot API needs Chat ID + Token; no RPi app required [Elektroda, xury, post #17946092]

How do I make Domoticz log data faster than every 5 minutes?

You can’t change the 5-minute commit interval from the GUI. The scheduler is hard-coded; faster logging needs you to modify main/Domoticz.cpp and re-compile, or bypass Domoticz by pushing values directly to InfluxDB or MQTT [Elektroda, xury, post #21444762]

Where should I install Telegram for push notifications?

Install Telegram only on your phone. In Domoticz, create a Bot, note the Chat ID and API Token, and enter them under Setup → Notifications. The RPi just calls the HTTPS endpoint; no local Telegram app is necessary [Elektroda, xury, post #17946092]

I get “bad argument #1 to 'pairs' (table expected, got nil)’ in a Lua script. Why?

The devicechanged or uservariables table is nil in All-Events context. Wrap the loop in if devicechanged then … end, or switch the script type to ‘Device’ so the engine populates those tables [Elektroda, sznickers, post #21447539]

How can I subtract two water-meter readings in dzVents?

Create a Dummy ‘Water’ counter for the result, then add a script:

return {
on = { timer = { 'every minute' } },
execute = function(dz)
local main = dz.devices(871).counter
local lawn = dz.devices(1146).counter
dz.devices(1164).updateCounter(main - lawn)
end
}
Replace the IDX numbers if they differ [Elektroda, Maciek1008, post #20804762]

How do I merge three PZEM-004T phase currents into one graph?

Use device.updateCurrent(L1,L2,L3) in dzVents. Example shown by xury consolidates IDX 5-7 into IDX 16 and updates every new value [Elektroda, xury, post #17750955]

Can I overlay live sensor text on a Hikvision camera?

Yes. Send an HTTP PUT with XML payload to /ISAPI/System/Video/inputs/channels/1/overlays using digest auth. Node-RED flow in the thread updates two temperature strings every minute [Elektroda, xury, post #19193707]

What’s the quickest way to capture a doorbell video and push it to Telegram?

  1. Install ffmpeg on the RPi. 2. Save the provided doorbell.sh script with execution rights. 3. Add the dzVents script that runs os.execute('/home/pi/domoticz/scripts/doorbell.sh') when device ‘Dzwonek’ turns ON. The script records 5 s RTSP clip and posts it via Telegram Bot API [Elektroda, xury, post #17941615]

How can I power-on an Android TV from Node-RED?

Enable USB debugging on the TV, install adb on the server, then use an Exec node: etherwake -i <iface> <MAC> && adb connect <IP>:5555 && adb shell input keyevent KEYCODE_POWER. Flow by xury also maps D-Pad and volume keys to Nora scenes for voice control [Elektroda, xury, post #18620730]

Is there a way to stop Victron PV inverters during negative price hours?

Yes. Monitor day-ahead price API, and when price < 0, publish ESS_OVERRIDE=1 and STATE=2 via MQTT to Victron’s Venus OS. The shared Node-RED flow blocks export and resumes when prices normalize [Elektroda, xury, post #21467173]

Edge case: What happens if Domoticz misses a 1-minute PIR event?

Because Domoticz logs every 5 minutes, a PIR that switches ON and OFF within that window may not appear in the history. “If the pump runs only 1–2 minutes it can be missed” [Elektroda, sznickers, post #21441441] Use InfluxDB or a dzVents HTTP call for higher-resolution logging.
Generated by the language model.
ADVERTISEMENT