Sunscreen not reacting to this script

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

Moderator: leecollings

Post Reply
studiocascade
Posts: 36
Joined: Thursday 13 July 2017 23:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Sunscreen not reacting to this script

Post by studiocascade »

I got a script from this forum and adjusted it a bit to my needs.
The script i'm currently trying is this:

Code: Select all

-- Define all the sensors which needs to be considered for the sunscreen to close
local sensors = {
    temperature = {
        active = true,
        device = 'Gevoelstemperatuur',
        closeRule = function(device)
            return device.temperature <= 15
        end
    },
    wind = {
        active = false,
        device = 'S Windmeter',
        closeRule = function(device)
            return device.speed >= 50 or device.gust >= 150
        end
    },
    rain = {
        active = false,
        device = 'Regen intensiteit',
        closeRule = function(device)
            return device.rainRate > 0
        end
    },
    rainExpected = {
        active = false,
        device = 'Rain expected', -- This needs to be a virtual sensor of type 'percentage'
        closeRule = function(device)
            return device.percentage > 15
        end
    },
    uv = {
        active = true,
        device = 'Zonkracht',
        closeRule = function(device)
            return device.uv <= 3
        end
    },
    lux = {
        active = false,
        device = 'Lux',
        closeRule = function(device)
            return device.lux <= 500
        end
    }
}

-- Define the name of your sunscreen device
local sunscreenDevice = 'Zonnescherm_dummy'

-- Enable dry run mode to test the sunscreen script without actually activating the sunscreen
local dryRun = false

-- Define the name of a virtual switch which you can use to disable the sunscreen automation script
-- Set to false to disable this feature
local manualOverrideSwitch = false

-- Minutes to wait after a sunscreen close before opening it again.
local timeBetweenOpens = 10

return {
    active = true,
    on = {
        timer = {'every minute'}
    },
    logging = {
        level = domoticz.LOG_DEBUG,
        marker = 'Sunscreen'
    },
    execute = function(domoticz)

        local function switchOn(sunscreen, message)
            if (sunscreen.state == 'Closed') then
                if (not dryRun) then
                    sunscreen.switchOn()
                    domoticz.notify('Sunscreen', message)
                end
                domoticz.log(message, domoticz.LOG_INFO)
            end
        end

        local function switchOff(sunscreen, message)
            if (sunscreen.state == 'Open') then
                if (not dryRun) then
                    sunscreen.switchOff()
                    domoticz.notify('Sunscreen', message)
                end
                domoticz.log(message, domoticz.LOG_INFO)
            end
        end

        if (manualOverrideSwitch and domoticz.devices(manualOverrideSwitch).state == 'On') then
            domoticz.log('Automatic sunscreen script is manually disabled', domoticz.LOG_DEBUG)
            return
        end

        local sunscreen = domoticz.devices(sunscreenDevice)

        -- Sunscreen must always be up during nighttime
        if (domoticz.time.isNightTime) then
            switchOff(sunscreen, 'Closing sunscreen, It is night')
            return
        end

        -- Check all sensor tresholds and if any exeeded close sunscreen
        for sensorType, sensor in pairs(sensors) do

            if (sensor['active'] == true) then

                local device = domoticz.devices(sensor['device'])
                local closeRule = sensor['closeRule']

                domoticz.log('Checking sensor: ' .. sensorType, domoticz.LOG_DEBUG)

                if (closeRule(device)) then

                    switchOff(sunscreen, sensorType .. ' treshold exceeded, Sunscreen up')

                    domoticz.log(sensorType .. ' treshold exceeded', domoticz.LOG_DEBUG)
                    -- Return early when we exeed any tresholds
                    return
                end
            else
                domoticz.log('Sensor not active skipping: ' .. sensorType, domoticz.LOG_DEBUG)
            end
        end

        -- All tresholds OK, sunscreen may be lowered
        if (sunscreen.lastUpdate.minutesAgo > timeBetweenOpens) then
            switchOn(sunscreen, 'Sun is shining, all thresholds OK, lowering sunscreen')
        end
    end
}

I am sure all names of the devices in this script are written correctly.

Some devices i intentionally set to 'false' because i like to test first with some simple tresholds and then build up.
I created a dummy sunscreen so i can play with this script without touching my actual sunscreen.
(i'll wait with publishing this script to my "live" situation until the dummy sunscreen acts close to the state of the real device. Important for the WAF)

I hope someone can help me determine why this script doesn't work in my situation
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Sunscreen not reacting to this script

Post by waltervl »

The best thing to troubleshoot is to set log statements like below (and modified to your own need) on places in the script you expect things to happen.

Code: Select all

domoticz.log('This is my log statement showing value: ' .. yourvalue, domoticz.LOG_INFO)
I also see there are already log statements in the script. So what do you expect to see on log statements and which will not show up?
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
studiocascade
Posts: 36
Joined: Thursday 13 July 2017 23:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Sunscreen not reacting to this script

Post by studiocascade »

Actually i expect that virtual switch to be on open/close state when the tresholds are met.
I think, due to an earlier problem where 'webserver' caused many log messages, a friend of mine managed to turn off nearly all log messages.
(and i honestly don't know where he did)
No matter if i enable "Debug" on DzVents scripts in settings, it only shows me some status messages in the log.
So maybe i first need to find out where i can enable full logging again.
Do you know any places outside the GUI where i can enable it?
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Sunscreen not reacting to this script

Post by waltervl »

For dzvents the logging settings is on page Setup - Settings, tab other. It has improved a lot in latest stable 2024.7
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
studiocascade
Posts: 36
Joined: Thursday 13 July 2017 23:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Sunscreen not reacting to this script

Post by studiocascade »

Thanks, i knew where i could find it and i think it's all activated to show the most of it:
Schermafdruk van 2024-07-29 16-51-57.png
Schermafdruk van 2024-07-29 16-51-57.png (23.69 KiB) Viewed 1007 times
But regardless of the setting my Domoticz only shows some status messages.
Could it be that it's deactivated at another place?
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Sunscreen not reacting to this script

Post by waltervl »

Then you should be able to see some 'Sunscreen' logging statements in the Setup - Log, tab All, every minute.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
studiocascade
Posts: 36
Joined: Thursday 13 July 2017 23:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Sunscreen not reacting to this script

Post by studiocascade »

Sadly there's not!
I only get these messages:
2024-07-29 17:00:01.968 Status: Schedule item started! Name: DinnerTableControl, Type: On Time, DevID: 2475, Time: 2024-07-29 17:00:01
2024-07-29 17:00:14.667 Status: Login successful from 192.168.2.130 for user 'HomeHabit'
2024-07-29 17:00:14.668 Status: [web:8080] Incoming connection from: 192.168.2.130
Only the messages "Login successful" and "Incoming connection" are shown every minute
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Sunscreen not reacting to this script

Post by waltervl »

Do you use the event editor for dzvents scripts (menu Setup - Other - Events) or do you store the scripts on the dzvents script folder directly? If on the drive directly is the naming correct (incl time)?
Else you have to ask your friend how he disabled the logging..... could be in /etc/init.d/domoticz.sh
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
studiocascade
Posts: 36
Joined: Thursday 13 July 2017 23:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Sunscreen not reacting to this script

Post by studiocascade »

Yes i'm using the built-in event editor for the scripts, so that should be OK
This is what my domoticz.sh looks like:

Code: Select all

#! /bin/sh
### BEGIN INIT INFO
# Provides:          domoticz
# Required-Start:    $network $remote_fs $syslog $time
# Required-Stop:     $network $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Home Automation System
# Description:       This daemon will start the Domoticz Home Automation System
### END INIT INFO

# Do NOT "set -e"

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
DESC="Domoticz Home Automation System"
NAME=domoticz
USERNAME=pi
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

DAEMON=/home/$USERNAME/domoticz/$NAME
DAEMON_ARGS="-daemon"
#DAEMON_ARGS="$DAEMON_ARGS -daemonname $NAME -pidfile $PIDFILE"
DAEMON_ARGS="$DAEMON_ARGS -www 8080"
DAEMON_ARGS="$DAEMON_ARGS -sslwww 443"
#DAEMON_ARGS="$DAEMON_ARGS -log /tmp/domoticz.txt"
#DAEMON_ARGS="$DAEMON_ARGS -syslog"

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

pidof_domoticz() {
    # if there is actually a domoticz process whose pid is in PIDFILE,
    # print it and return 0.
    if [ -e "$PIDFILE" ]; then
        if pidof domoticz | tr ' ' '\n' | grep -w $(cat $PIDFILE); then
            return 0
        fi
    fi
    return 1
}

#
# Function that starts the daemon/service
#
do_start()
{
	# Script that waits for time synchronisation before starting Domoticz to prevent a crash
	# on a cold boot when no real time clock is available.

	# Check if systemd-timesyncd is enabled and running, otherwise ignore script
	if systemctl --quiet is-enabled systemd-timesyncd && systemctl --quiet is-active systemd-timesyncd; then
	    # Check if time is already synced, if so start Domoticz immediately
	    if [ -f "/run/systemd/timesync/synchronized" ]; then
		printf "Time synchronized, starting Domoticz...\n"
	    else
		# Check if custom time server(s) are defined
		if grep -q "^#NTP=" /etc/systemd/timesyncd.conf; then
		    printf "INFO: No time server(s) defined in /etc/systemd/timesyncd.conf, using default fallback server(s)\n"
		fi
		# Wait a maximum of 30 seconds until sync file is generated indicating successful time sync
		printf "Waiting for time synchronization before starting Domoticz"
		count=30
		while [ ! -f "/run/systemd/timesync/synchronized" ]
		do
		    count=$((count-1))
		    if [ $((count)) -lt 1 ]
		    then
			# If failed, print error message, exit loop and start Domoticz anyway
			printf "\nWARNING: Time synchronization failed, check network and /etc/systemd/timesyncd.conf\n"
			printf "Starting Domoticz without successful time synchronization...\n"
			break
		    fi
		    printf "."
		    sleep 1
		done
		#If the file was found in time, sync was successful and Domoticz will start immediately
		if [ -f "/run/systemd/timesync/synchronized" ]
		then
		    printf "\nTime synchronization successful, starting Domoticz...\n"
		fi
	    fi
	fi

	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started
	start-stop-daemon --chuid $USERNAME --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
		|| return 1
	start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
		$DAEMON_ARGS \
		|| return 2
}

#
# Function that stops the daemon/service
#
do_stop()
{
        # Return
        #   0 if daemon has been stopped
        #   1 if daemon was already stopped
        #   2 if daemon could not be stopped
        #   other if a failure occurred
        start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
        RETVAL="$?"
        [ "$RETVAL" = 2 ] && return 2
        # Wait for children to finish too if this is a daemon that forks
        # and if the daemon is only ever run from this initscript.
        # If the above conditions are not satisfied then add some other code
        # that waits for the process to drop all resources that could be
        # needed by services started subsequently.  A last resort is to
        # sleep for some time.
        start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
        [ "$?" = 2 ] && return 2
        # Many daemons don't delete their pidfiles when they exit.
        rm -f $PIDFILE
        return "$RETVAL"
}

case "$1" in
  start)
        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
        do_start
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  stop)
        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
        do_stop
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  status)
        status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
        ;;
  reload)
        log_daemon_msg "Reloading $DESC" "$NAME"
        PID=$(pidof_domoticz) || true
        if [ "${PID}" ]; then
                kill -HUP $PID
                log_end_msg 0
        else
                log_end_msg 1
        fi
        ;;
  restart)
        log_daemon_msg "Restarting $DESC" "$NAME"
        do_stop
        case "$?" in
          0|1)
                do_start
                case "$?" in
                        0) log_end_msg 0 ;;
                        1) log_end_msg 1 ;; # Old process is still running
                        *) log_end_msg 1 ;; # Failed to start
                esac
                ;;
          *)
                # Failed to stop
                log_end_msg 1
                ;;
        esac
        ;;
  *)
        echo "Usage: $SCRIPTNAME {start|stop|status|restart|reload}" >&2
        exit 3
        ;;
esac

:
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Sunscreen not reacting to this script

Post by waltervl »

Domoticz.sh looks unmodified so that is not causing it.
Per hardware gateway you can set the log level of that particular hardware gateway but there is no other setting to switch off logging.
You also have other dzvents scripts running?
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
studiocascade
Posts: 36
Joined: Thursday 13 July 2017 23:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Sunscreen not reacting to this script

Post by studiocascade »

no, only some LUA scripts and Blockly
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Sunscreen not reacting to this script

Post by waltervl »

What version of Domoticz do you have?
What happens if you set the logging of dzvents in setup - settings to info+ execution?
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
studiocascade
Posts: 36
Joined: Thursday 13 July 2017 23:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Sunscreen not reacting to this script

Post by studiocascade »

I'm using Domoticz 2024.7

Changed the logging to info, status, errors & execution info
Nothing seems to change in the log.
All the same.

2024-07-31 19:02:00.519 Status: LUA: 19:02:00 gc_main: 584: -> Start update for GarbageCalendar text device "Container"
2024-07-31 19:02:00.521 Status: LUA: 19:02:00 gc_main: 786: -> found schedule:ma 5 aug: GFT ;
2024-07-31 19:02:00.588 Status: [web:8080] Incoming connection from: 192.168.2.81
2024-07-31 19:02:00.811 Status: EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_time_garbagecalendar.lua
2024-07-31 19:05:00.516 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2024-07-31 19:10:00.572 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua


So i get some information, but these are only Status messages (in bold, light green)

Maybe there aren't any errors at all, but that's a bit odd in my opinion.
I used to have a lot of messages more frequently
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Sunscreen not reacting to this script

Post by waltervl »

Normally you should also see some info messages.
There seems to be a dzvents script running every 5 minutes but that is not your sunscreen script as that should run every minute.

It seems somehow the log is filtered but I have no idea how that is done.
You could try to change in your script all LOG_INFO to LOG_STATUS
Then the logging will be pushed as status messages and hopefully be shown in the log.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests