Triggering dzVents script on string in logfile ?  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
hkemal
Posts: 13
Joined: Wednesday 16 January 2019 14:04
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Triggering dzVents script on string in logfile ?

Post by hkemal »

Hello all,

I have a script in dzVents that controls the plugs for my TV and Hifi based on the changes of the Kodi hardware.
In a trigger section I have devices = { 'Kodi' }, so my script gets triggered every time Kodi changes value (domoticz.devices('Kodi').state == "Video", "Audio" etc.). This is working fine..
What I want now is to scrap what is being played and act on that. But my script doesn't get triggered when the played title changes, for example SongA changes to SongB. The event is visible in Domoticz Log (Kodi: Event "Song A") and is shown on UI, but this doesn't trigger my script.
How can I get the script triggered on this event in the log?

Regards
K
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Triggering dzVents script on string in logfile ?  [Solved]

Post by waaren »

hkemal wrote: Friday 19 June 2020 22:07 I have a script in dzVents that controls the plugs for my TV and Hifi based on the changes of the Kodi hardware.
In a trigger section I have devices = { 'Kodi' }, so my script gets triggered every time Kodi changes value (domoticz.devices('Kodi').state == "Video", "Audio" etc.). This is working fine..
What I want now is to scrap what is being played and act on that. But my script doesn't get triggered when the played title changes, for example SongA changes to SongB. The event is visible in Domoticz Log (Kodi: Event "Song A") and is shown on UI, but this doesn't trigger my script.
How can I get the script triggered on this event in the log?
The Kodi hardware module update the device directly in the database on a played title change and does not use the route via the event system. So you need a workaround to catch this event.

One way is to trigger a bash script via os.execute() in a script, -crontab or with nohup. watching the domoticz log, waiting for a certain string and have it fire a customEvent when the string is send to the log.

bash script to monitor log file and trigger dzVents script.

Code: Select all

#!/bin/bash
#
# signal dzVents about a set string in domoticz logfile
#

#
# linux commands/ programs used in this script: echo, curl, tail, grep, sed, xxd and tr
#
# tail -f -n0 $file &           -->> only look at lines appended to $file after starting this command in the background 
# grep -m1 "$string"            -->> stop when finding the first appearance of $string 
# echo -ne $string              -->> echo string without newline and enable interpretation of backslash escapes
# xxd -plain $string            -->> converts $string to hexdump
# tr -d '\n' $string            -->> remove newline from $string
# sed 's/\(..\)/%\1/g') $string -->> insert % before every pair of chars in $string 
# curl                          -->> call domoticz API

#
# Root required
#
if [[ $EUID -ne 0 ]]; then
   # This script must be run as root
   exit 1
fi

customEvent=kodiLogEvent                # customEvent that triggers your dzVents script
domoticzPort=8084                       # change to port number you use for domoticz
domoticzLog=/var/log/domoticz.log       # logging to OS file required 
string2watch="Kodi: Event"              # Your string to watch for

while true ; do  
	result=$( ( tail -f -n0 $domoticzLog & ) | grep -m1  "$string2watch" )
	urlEncodedResult=$(echo -ne $result | xxd -plain | tr -d '\n' | sed 's/\(..\)/%\1/g')

	curl "http://localhost:$domoticzPort/json.htm?type=command&param=customevent&event=$customEvent&data=$urlEncodedResult"
done

dzVents to receive the catched logline

Code: Select all

scriptVar = 'kodiLogEvent'

return
{
    on =
    {
        customEvents =
        {
            scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when tested and OK
        marker = scriptVar,
    },

    execute = function(dz, item)
        local lodash = dz.utils._

        dz.log(item.trigger .. ' triggered this script ',dz.LOG_DEBUG)

        -- do not echo the whole string in the log or you will create a loop
        dz.log('payload is: '.. lodash.str(item.data:gsub('Kodi: Event','')),dz.LOG_DEBUG)

    end

}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest