Run shell script after Domoticz startup

Use this forum to discuss possible implementation of a new feature before opening a ticket.
A developer shall edit the topic title with "[xxx]" where xxx is the id of the accompanying tracker id.
Duplicate posts about the same id. +1 posts are not allowed.

Moderators: leecollings, remb0

Post Reply
alainvm
Posts: 3
Joined: Monday 09 February 2015 23:12
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Paris - France
Contact:

Run shell script after Domoticz startup

Post by alainvm »

After a power outage, reboot or startup, GPIO outputs are all set to "0" state so some do not correspond to the previous state saved in the data base.

I am needing to set known defined initial states for some GPIO pins after Domoticz startup, for example a relay must automatically be switched "On" for the boiler control (only off on raspberry power outage).

This can be done easily by running a shell script on startup using JSON to control switches and get status each time Domoticz goes running (not just on reboot).

I have done this in my code by adding a "-script" option to domoticz in the domoticz.cpp source code.

If -script is present with a script file path (i.e. /domoticz/scripts/bash/startup.sh) the script is run with a non blocking posix_spawn() call. I have put the code just before domoticz goes into it's infinite loop.

let me know what the pros and cons of this can be, so far it works fine for me.

I have attached my files for review.
Cheers,
alainvm
Attachments
domoticz.cpp
(25.87 KiB) Downloaded 149 times
startup.zip
(523 Bytes) Downloaded 162 times
paulvha
Posts: 40
Joined: Friday 29 January 2016 16:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Re: Run shell script after Domoticz startup

Post by paulvha »

Interesting solution. I could see 2 concerns:
1. It will require the owners of Domoticz to add the '--script' option to the standard source, otherwise this will have to be included every time manually when a new version has been created and one can not use the beta-versions (at least have not seen where to download that source code)
2. I assume you switch off the heating on certain hours. This script would switch on the heating, after a failure/reboot, even at hours you do not want it switched on. This however might not be a problem if it happens only occasionally.

Alternative for point 1 could be to switch your heating on / off with a script that is triggered at system start-up AND by cron/crontab, and then sent the command to Domoticz with the instructions you have have created.

Regards,
Paul
alainvm
Posts: 3
Joined: Monday 09 February 2015 23:12
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Paris - France
Contact:

Re: Run shell script after Domoticz startup

Post by alainvm »

Good points.

For point 1, it would be nice to have this feature included in the standard release if it does not give any side effect problems. Perhaps my code needs tuning as I am not a pro programmer. In any case, I rebuild my Domoticz from source as I have made a few changes for 1wire device ID's

For point 2, I made it simple in the post, but the setup is a bit more complicated than a simple relay. The Viessmann Duomatik boiler controller (1997 model) reads the value of a resistor to set hot water temperature. So I have built an I2C digital Potentiometer I control with a Domoticz Slider switch (Consigne Chaudiere - Off, 0-100%) and a LUA script sending commands over the bus. I can also set timers to control night/day heating or turn it down low when not at home.

Only big problem is if I loose power on the system 5V and 12V, the DigiPot goes to infinite value sending boiler to full blow !! That's the reason of the relay to provide a fixed value resistor (connected to NO) to the boiler controller to keep in a low heat mode.

For this to work, I need to activate the relay and have a 1 (Chauffage marche - Switch On) on GPIO port while Domoticz is powered on.

Putting the -script option in the startup of Domoticz makes it run on every (re)start of Domoticz, not just on reboot (like with a cron script)

Reason for making my changes !

Cheers,
alainvm
Attachments
Chaudiere.jpg
Chaudiere.jpg (14.28 KiB) Viewed 4943 times
DigiPot.jpg
DigiPot.jpg (52.12 KiB) Viewed 4945 times
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Re: Run shell script after Domoticz startup

Post by Number8 »

There is definitely the need to have this capability included in the regular code. Any serious installation needs to recover to a known state after a power outage or a reboot. It would be great to have a kind of save context running automatically for specific outputs in order to be able to load it if needed.
Debian buster on NUC and three RPi with buster.
stlaha2007
Posts: 370
Joined: Monday 05 October 2015 10:16
Target OS: -
Domoticz version:
Contact:

Re: Run shell script after Domoticz startup

Post by stlaha2007 »

I understand the request, and i believe there is some state backup already implemented.

However not everyone is using gpio to control stuff. I'm in the middle of setting up temperture controlled fans (RPi heating up in non-cooled room ;-) ) so ran against the GPIO=0 state also. But hacking the code, nicely done btw., this way i think is overkill.
paulvha wrote:Interesting solution. I could see 2 concerns:
1. It will require the owners of Domoticz to add the '--script' option to the standard source, otherwise this will have to be included every time manually when a new version has been created and one can not use the beta-versions (at least have not seen where to download that source code)

Alternative for point 1 could be to switch your heating on / off with a script that is triggered at system start-up AND by cron/crontab, and then sent the command to Domoticz with the instructions you have have created.
Depending on the needs, i would solve it with the following method: (Can also be rewritten for the few Windows users)
For instance using crontab with a line like:

Code: Select all

@reboot /home/user/scripts/startup_script.sh &&
you force it to run after (re)boot.
Build with some checks like:
- sleep 120 seconds (normal boottime approx less then 2 minutes)
- check domoticz.pid exists
- check gpio status
- check domoticz gpio status
- switch gpio(s) if different then domoticz
And keep it running in the background with a loop.

This way it can be used by the tweakers among use, and left alone for those that won't. And survive upgrades, as it doesn't need a modification to code or domoticz-launchscript.

My main thought is, keep it simple, and seperate your specific wishes so its more easy to implement for the novice user too.
DomoUHT
Posts: 15
Joined: Friday 13 March 2015 22:08
Target OS: OS X
Domoticz version: 2.2293
Location: Alkmaar, The Netherlands
Contact:

Re: Run shell script after Domoticz startup

Post by DomoUHT »

Hi,

I installed the rPI watchdog (Wiki : https://www.domoticz.com/wiki/Setting_u ... i_watchdog ) where a line is added to /etc/rc.local to send an e-mail at every reboot.

I added this line :
curl -s "http://[DomoticZ-IP]:[DomoticZ-Port]/json.htm?type=command&param=switchlight&idx=329&switchcmd=On" &
just before the last line which is ''exit 0'

This way (SSH into the rPI) :
sudo nano /etc/rc.local
(Add 'curl --' above the exit 0 line)
(Save the file)
Make sure the rc.local can be executed:
sudo chmod 0755 /etc/rc.local


At every reboot the virtual switch index=329 (Mine is called FreshBoot) is switched on.

I then have a blocky (named FreshBoot) :
If Freshboot=On
Set Freshboot=Off
<switch on what you need after a power outage, e.g boiler on, or main router on>


I guess you don't need to install the watchdog, just modify the /etc/rc.local file.
When DomoticZ is shutdown the service is stopped and there will be no reboot.

In my Freshboot blocky I can start a VirtualSwitch cyclic delay-off timer :
Set DelayOff=Off
Set DelayOff=On after 1 sec

After 1sec DelayOff switches to On, and this event is used to start the cyclic timer in another blocky (Delay-Off) :
If DelayOff=On
Set DelayOff=Off
Set DelayOff=On after 600sec (10mins)
ElseIf DelayOff=On AND Lamp=Off
Set Lamp=Off after 2sec

The Lamp is switched off every 10 mins when its state is Off (to prevent against missed Off commands).


As you might have noticed I only use blocky (in Lua it is probably very easy to autostart a cyclic timer, but I have not yet dug in to Lua). For me blocky has the advantage that it can be edited and tested easily via the DomoticZ interface (also remotely over the internet).
8PDF5
Posts: 1
Joined: Sunday 02 October 2016 10:01
Target OS: -
Domoticz version:
Contact:

Re: Run shell script after Domoticz startup

Post by 8PDF5 »

Hi alainvm,

I'm looking into a similar solution for controlling the boiler water temp of a Viessmann Vitodens 200 by using a digipot to change the resistance of the remote control circuit. How did you isolate the digipot output connected to the boiler from the digital domain of the raspberry ?

Best,

8PDF5
pagocs
Posts: 5
Joined: Sunday 06 November 2016 17:53
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Run shell script after Domoticz startup

Post by pagocs »

Here is a project which is very useful for Vitodens 200 integration: http://openv.wikispaces.com/Bauanleitung+USB
Andyyyy
Posts: 29
Joined: Saturday 13 July 2013 23:46
Target OS: -
Domoticz version:
Contact:

Re: Run shell script after Domoticz startup

Post by Andyyyy »

Hi,
the new site is: https://github.com/openv/openv/wiki
The question is how can i get the data in domoticz ..

Andyyyy
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest