Page 1 of 1

Run shell script after Domoticz startup

Posted: Monday 14 March 2016 17:07
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

Re: Run shell script after Domoticz startup

Posted: Monday 14 March 2016 18:34
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

Re: Run shell script after Domoticz startup

Posted: Monday 14 March 2016 23:27
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

Re: Run shell script after Domoticz startup

Posted: Wednesday 16 March 2016 7:47
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.

Re: Run shell script after Domoticz startup

Posted: Wednesday 16 March 2016 13:23
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.

Re: Run shell script after Domoticz startup

Posted: Wednesday 16 March 2016 23:24
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).

Re: Run shell script after Domoticz startup

Posted: Sunday 02 October 2016 10:41
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

Re: Run shell script after Domoticz startup

Posted: Saturday 25 March 2017 9:59
by pagocs
Here is a project which is very useful for Vitodens 200 integration: http://openv.wikispaces.com/Bauanleitung+USB

Re: Run shell script after Domoticz startup

Posted: Thursday 11 October 2018 10:02
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