Setting user variable on startup

Moderator: leecollings

Post Reply
flabrosse
Posts: 18
Joined: Thursday 07 July 2016 20:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Setting user variable on startup

Post by flabrosse »

Hi all,

I have a user variable that counts how many underfloor heating zone valves are activated so that the pump and main valve are shut when the last zone valve is closed. It all works well, until the system is rebooted (my domoticz runs on a RPI3). Indeed, the vlue of the user variable is kept, ie does not reflect the real state of the system.

Is there a way to initialise a user variable at statup time?

Cheers,

Fred
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Setting user variable on startup

Post by Egregius »

If you mean only upon reboot of the rpi setting to a default value? Yes, use a shell script activated by @reboot in crontab.
flabrosse
Posts: 18
Joined: Thursday 07 July 2016 20:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Setting user variable on startup

Post by flabrosse »

Yes, that is what I mean. But can a domoticz user variable be changed from a shell script? I can't see that anywhere.
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Setting user variable on startup

Post by Egregius »

Off course, do a curl of a json command.
Will post a example tomorrow.
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Setting user variable on startup

Post by Egregius »

Here we go:
Off course change variables on top to your needs.

login with ssh
nano /home/pi/atreboot.sh

Code: Select all

#!/bin/bash
ip=192.168.2.10
port=8084
idx=1
name='Test'
type=0
value=22

function jsonValue() {
   KEY=$1
   num=$2
   awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p
}

while :
do
        DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://$ip:$port/json.htm?type=command&param=updateuservariable&idx=$idx&vname=$name&vtype=$type&vvalue=$value"`
        STATUS=$(echo $DOMOTICZ | jsonValue status 1)
        if [ $STATUS = 'OK' ] ; then
                exit
        fi
        sleep 3
        exit
done
chmod +x /home/pi/atreboot.sh
sudo crontab -e
@reboot /home/pi/atreboot.sh

So, upon reboot the script will be started by cron and try to update the variable with a short time-out. If Domoticz isn't running yet that'll fail and tried again 3 seconds later until the update is OK.
flabrosse
Posts: 18
Joined: Thursday 07 July 2016 20:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Setting user variable on startup

Post by flabrosse »

Thanks for that. Doesn't quite work though. After obvious modifications (IP address, variable name, etc.), running it complains:

Code: Select all

./atreboot.sh 
./atreboot.sh: line 19: [: =: unary operator expected
Also, I'm not sure what the type should be. Is that an index in an array of choices? Would zero be the first in the menu, ie integer?
flabrosse
Posts: 18
Joined: Thursday 07 July 2016 20:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Setting user variable on startup

Post by flabrosse »

So for the record, two answers to the above.

$STATUS on line 19 must be surrounded by quotes, to make it a string so that the comparison works, and all the quotes have to be double quotes. I also removed an exit otherwise the scripts only tries once, even if that failed. Finally, the variable STATUS contains a space in front of "OK", as in " OK" (my awk/sed is not good enough to get rid of it. So here is the new script (heavily based on Egregius', thanks):

Code: Select all

#!/bin/bash
ip=192.168.2.10
port=8084
idx=1
name='Test'
type=0
value=22

function jsonValue() {
   KEY=$1
   num=$2
   awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p
}

while :
do
        DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://$ip:$port/json.htm?type=command&param=updateuservariable&idx=$idx&vname=$name&vtype=$type&vvalue=$value"`
        STATUS=$(echo $DOMOTICZ | jsonValue status 1)
        if [ "$STATUS" == " OK" ] ; then
                exit
        fi
        sleep 3
done
I hope this helps somebody else. Thank you Egregius for the original script.
User avatar
Domosapiens
Posts: 232
Joined: Wednesday 20 August 2014 12:08
Target OS: Windows
Domoticz version: V3.5981
Location: NL
Contact:

Re: Setting user variable on startup

Post by Domosapiens »

I would be a huge improvement for Domoticz if there is a shared System-/User- Dummy Device,
that would be SET by Domoticz on start-up and can be Re-SET by the user after initialization of Blockly's and/or Scripts.
With that Dummy Device you can control a set of "Run Once" routines.
Good programming starts with a defined environment.
Win Vista&7; 1#Aeon Z-Stick S2; 1#Aeotec Z-Sick Gen5, 6#Fib.FGBS001; 24#DS18B20; 8#Everspr.AN158-2; 3#Philio PAN04; 1#Philio PAN06, 1#YouLess El; 1#Fib.FGWPE; 1#ZME_RC2; 2#FAK_ZWS230, 2#Quib.ZMNHCDx, 1#Quib.ZMNHDD1, 7#EM6555
flabrosse
Posts: 18
Joined: Thursday 07 July 2016 20:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Setting user variable on startup

Post by flabrosse »

In fact, I had not realised that my problem was not because of the user variable not matching the current state, but that my switches that control GPIOs on the Pi are remembered to be on (if they were) but the matching GPIOs are not set to on at startup, leading to inconsistency!

Indeed, being able to define a starting point would be good.
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Setting user variable on startup

Post by Egregius »

Sorry, tested on a online system, didn't try when it was offline, but you solved it correctly ;)

I don't really understand the problem, I have no need for variables and statusses after startup. All scripts are made that they set them when needed.
flabrosse
Posts: 18
Joined: Thursday 07 July 2016 20:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Setting user variable on startup

Post by flabrosse »

No worries!

Let me explain a bit more. My underfloor heating has a number of actuators: 1 valve per zone, plus 1 pump and 1 valve for the whole manifold. Whenever heat is requested for any of the zones (either temperature too low or a boost is requested) then the corresponding zone valve is opened, along with the pump and manifold valve. When heat is not needed anymore, the zone valve is shut, and the pump and manifold valve stopped/shut only if no other zone needs heat. I do this last bit by having a user variable that counts how many zones are currently active, incremented when a new one becomes active and decremented when one is stopped. The pump/manifold valve are stopped only when this reaches 0 (in fact 1 because of when the update is made).

Another approach could have been to go though all switches corresponding to zone valves and activate/deactivate the pump and manifold valve is any is on/all off. However that implies a loop that knows about all relevant switch names, and I don't like that because that becomes difficult to maintain if any is modified/added/removed (don't replicate information in software if you can avoid it).

In any case, none of that would work because the scripts are only triggered when something happens and at start up time, as I explained above, the switches might say they are on but the corresponding GPIOs are off. In other words, a domoticz GPIO switch does not tell the Pi what the status of the GPIO should be at startup.

But this is becoming off-topic given the subject line. I'll start a new topic later today. But I'll do some googling first to see if this has been mentioned before.
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Setting user variable on startup

Post by Egregius »

All valves are switches known in Domoticz?
All valves are switched depending on room temperature and setpoint?
Then I would just use all the switches and setpoints in one script and let that handle everything from heating.
At least, that's sort of how I'm doing it.
In my system I have a thermometer, lets call it 'room_temp'.
For each room I also have a setpoint 'room_set' and one or more valves, those are called 'roomZ'.
For each room I calculate the difference between real temp and setpoint and memorize the coldest room.
Then, if that difference is lower than 0 I switch the heating and adjust the valves.
All the code is in my pass2php script. Works really well and accurate. I have 0,1° histeris :)
flabrosse
Posts: 18
Joined: Thursday 07 July 2016 20:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Setting user variable on startup

Post by flabrosse »

That is more or less what I do, but I do it on a per zone basis, when an event is triggered about the zone (change in temperature or boost requested). In other words, I was trying to get my script not do anything with the other zones than the one for which an event was triggered.

My logic works, apart from the fact that in case of a reboot, if a valve was activated (corresponding gpio set to on) before the reboot, it is believed to be activated by domoticz after (state saved in the database and recovered when domoticz starts) but is in fact not set (gpio remains low). Obviously the Pi should not have to reboot, but these things do happen now and then :cry: .

Someone else mentioned the opposite problem, ie a gpio high but not seen as such by domoticz when starting: viewtopic.php?t=7548. Unfortunately no real answer was provided.

It seems that the answer should be in making sure that devices that correspond to GPIOs should either be set from the state of the hardware or set the hardware at start up time from the values stored in the database (the latter would be my preferred solution as it would solve my problem ;) ).
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Setting user variable on startup

Post by Egregius »

How about upon start checking the devices in Domoticz and sent that state again to the GPIO?
Your valves are connected to the GPIO?
What is domoticz says they're on but they are of because of the reboot and you sent another ON signal? Does it switch then?

I combined all the zones into one script because I wanted to know the status of all of them before deciding if the heater needs to go on, and is so for how long it has to go off. My heater needs more time to heat when 7 radiators are open, then when only 1 is open.
flabrosse
Posts: 18
Joined: Thursday 07 July 2016 20:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Setting user variable on startup

Post by flabrosse »

I guess I could have a script running when starting and going through all valves, but I really don't like that because that script will need to know about all valves. I suspect setting them on if they already are on should work, but I have not tried.

Can an event be triggered by the start of domoticz, or would I have to call one using the same @reboot trick?
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Setting user variable on startup

Post by Egregius »

The @reboot trick should work, when rebooting off course.
Maybe a change to the start-stop script could call your custom script.
flabrosse
Posts: 18
Joined: Thursday 07 July 2016 20:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Setting user variable on startup

Post by flabrosse »

The problem is that I suspect there will be cases when one may want to have such a script that is run when domoticz starts, not necessarily when the Pi reboots. I don't know if this is possible, but would be nice.
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Setting user variable on startup

Post by Egregius »

Add it to /etc/init.d/domoticz.sh
Shouldn't be to hard to see where to put it.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest