Page 1 of 1
event when a domoticz error is logged?
Posted: Tuesday 03 November 2020 19:11
by leonmoonen
Hi,
I was wondering if it's possible to automatically trigger an action when a certain domoticz error occurs?
Background: Once in a while my youless unit freezes, which shows up in the error log "<timestamp> Error: YouLess: Error connecting to: <ip-address>". I power the youless over a controllable socket, so I can then 'reset' it by turning off its power. Using a 15 second "On Delay" and by calling the switchcmd=On as the Off Action, it's then automatically turned on again 15 seconds later. This works well, but it still requires me to notice the error, which I would like to be done by the computer
I know I can set up an automated reboot every x hours, or I could write an external script that pings the youless device and performs the power-off when it's unreachable, but it seemed nice to be able to solve this inside of domoticz.
cheers,
Leon
Re: event when a domoticz error is logged?
Posted: Tuesday 03 November 2020 19:53
by waaren
leonmoonen wrote: ↑Tuesday 03 November 2020 19:11
I was wondering if it's possible to automatically trigger an action when a certain domoticz error occurs?
Maybe
this or
this can get you started.
__________________________________________________________________________________________________________________________
When not yet familiar with dzVents please start with reading
Get started Before implementing (~ 5 minutes). Special attention please for "In Domoticz go to Setup > Settings > Other and in the section EventSystem make sure the checkbox 'dzVents enabled' is checked. Also make sure that in the Security section in the settings you allow 127.0.0.1 to not need a password. dzVents uses that port to send certain commands to Domoticz. Finally make sure you have set your current location in Setup > Settings > System > Location, otherwise there is no way to determine nighttime/daytime state."
___________________________________________________________________________________________________________________________
Re: event when a domoticz error is logged?
Posted: Friday 06 November 2020 17:32
by leonmoonen
Thanks! that did help me get started.
I ended up writing a crontab script that does both the check and the power-cycle instead of triggering a custom event, because that felt like a better fit in my use-case, but the payload in the script can be easily adapted to generate custom events. I'm posting it here so others may benefit:
Code: Select all
#!/bin/bash
# Monitor log for occurrence of events in the last minute and trigger
# a payload when they occurred (for example a power cycle)
# use this in a cron job, but run it less then once every minute
# or the payload may get triggered twice...
# setup
host=127.0.0.1
port=8080
triggeridx=6
payload="type=command¶m=switchlight&idx=$triggeridx&switchcmd=Off"
error="Error: YouLess: Error connecting to: 192.168."
# the next string needs to be url-encoded: https://www.url-encode-decode.com/
logmsg="YouLess+was+power-cycled+by+MonitorLog"
tstamp=$(date '+%F %H:%M')
# see how many matches we had in the last minute
matches=$(curl -s "http://$host:$port/json.htm?type=command¶m=getlog&loglevel=4" | grep "$error" | grep "$tstamp" | wc -l)
if [ "$matches" != "0" ];
then
curl -s "http://$host:$port/json.htm?$payload"
curl -s "http://$host:$port/json.htm?type=command¶m=addlogmessage&message=$logmsg"
fi
I'm running this every 5 mins in a cronjob, and it works well because a failing YouLess results in an error every 10 seconds. If the error you're interested in is more sporadic, the grep "logic" may need some tweaks, e.g., check what happened in the last hour (just remove %M from tstamp). Note that if you make that change, you should run it less than once an hour to avoid being triggered twice by the same error (there are ways around this, for example by storing the lastlogtime of every run in a persistent variable and only get the log entries from after that time).