Need help with timer switch on utility counter

Moderator: leecollings

Post Reply
klodek12
Posts: 5
Joined: Sunday 02 May 2021 21:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Need help with timer switch on utility counter

Post by klodek12 »

Hello Guys,

I need help with some easy blockly logic.
I use script to count how much time the switch is ON (viewtopic.php?f=14&t=23713&start=20)
This timer is working good:
Image

Now, I would like to turn off another switch depends of value of this timer:
Image

But it doesn't work. Can someone take a look at this and give me advice what I'm doing wrong?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Need help with timer switch on utility counter

Post by waaren »

klodek12 wrote: Sunday 02 May 2021 21:28 But it doesn't work. Can someone take a look at this and give me advice what I'm doing wrong?
Blockly is not ideal for these kind of comparisons. When you use the "Actual" value in an if block it returns 0 but when used in a "write to log" block it shows 22
If you share the dzVents script you now use to update the timer I could try to include the part to turn off another switch based on the timer value.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
klodek12
Posts: 5
Joined: Sunday 02 May 2021 21:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Need help with timer switch on utility counter

Post by klodek12 »

I'm using this code to count time. The bad thing in this code below is fact that timer is updated only when switch goes from ON to OFF.

Code: Select all

 --getUptime.lua 
local frequency = 30 -- set to desired frequency (1,2,3,4,5,6,10,12,15,20,30) -- lower number ==>> more accurate reporting 
local mySwitch = 'Output5' -- set to name of device to be measured
local timeKeeper = 'CzasPracy2' -- set to name of created virtual counter incremental
local myTimeUnit = 'second' -- set to unit preference (second, minute or hour) 

--[[ also adjust device settings of timeKeeper counter to chosen timeUnit  Press [edit] on the device (on the utility tab)

Type: Counter 
Divider: 0
Meter Offset: 0
Value Quantity (e.g. Weight): Count
Value Units (e.g. Kg): choose seconds,  hours or minutes
 
]]--

return 
{
    on = 
    {   
        timer = {'every '.. frequency .. ' minutes'},  -- this will ensure updates to counter is done every <frequency> minutes the device is switched ON
        devices = { mySwitch },
    },
                
    logging =   
    {   
        level = domoticz.LOG_ERROR,
        marker = "getUptime"    },

    data =
    {   
        state = { initial = "-"   },
        secondsOn = { initial = 0 },
        base = { initial = 0 },
    },
    
    execute = function(dz, item)

        local mySwitch = dz.devices(mySwitch)
        local timeKeeper = dz.devices(timeKeeper) 
        local timeUnits = { 
                            minute = 60,
                            hour = 3600,
                            second = 1, 
                          }

        local function updateCounter()
            dz.data.secondsOn = dz.data.secondsOn + ( dz.time.dDate - dz.data.base )
            if timeKeeper.count ~= dz.data.secondsOn / timeUnits[myTimeUnit] then
                timeKeeper.updateCounter(dz.utils.round(dz.data.secondsOn / timeUnits[myTimeUnit]) )
            end    
        end

        if item.isTimer and mySwitch.active and dz.data.base ~= 0 then
            updateCounter()
            dz.data.base = dz.time.dDate
            dz.data.state = mySwitch.state
        elseif item.isDevice and mySwitch.active and dz.data.state ~= 'On' then
            dz.data.base = dz.time.dDate
            dz.data.state = 'On'
        elseif item.isDevice and dz.data.state == 'On' then
            updateCounter()
            dz.data.state = 'Off'
        elseif item.isTimer and dz.data.base == 0 then
            dz.data.base = dz.time.dDate
            dz.data.state = mySwitch.state
        end    

        dz.log("State ==>> " .. mySwitch.state  .. "; Date Time ==>> " .. dz.time.rawDate .." " .. 
                       dz.time.rawTime .. "; " .. myTimeUnit ..  "s On today ===>> " .. timeKeeper.counterToday ,dz.LOG_DEBUG)
            
    end
}
BTW this is probably your code (I shared link above) :-)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Need help with timer switch on utility counter

Post by waaren »

klodek12 wrote: Monday 03 May 2021 13:11 I'm using this code to count time. The bad thing in this code below is fact that timer is updated only when switch goes from ON to OFF.
You set the frequency to 30 so the script will update the timer also every 30 minutes.

The CzasPracy2 counter will only increase its value. In the Blockly you tried to switch Output13 to Off once the counter reaches 22 seconds but that would mean that it will only be switched once.

So when should the Output13 switched to Off and when should it be switched to On ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
klodek12
Posts: 5
Joined: Sunday 02 May 2021 21:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Need help with timer switch on utility counter

Post by klodek12 »

Yes, you are right.

My simple blockly logic that I shared was just example.
I'd like to get this finall solution: I switch ON "Output5" and timer is counting how long it is ON. When timer reaches for example 10 seconds it should be switched OFF. That's my goal.

You can say that I can use this function:
Image

But this Output will control Roller Shutters that can be control in both ways (physical, local buttons and by domoticz) that's why I'd like to have counter that will collect time.
Example: Window is totally open without roller shutter. Then I press local push button to close it in 50%. After some time I would like to close it in 100% but this time from domoticz. So if from 0% to 100% motor needs 10seconds in this situation motor should work only for 5 seconds. That's why I need "buffer" with timer.

Assumpt, I'd like to get something like this:
Image
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Need help with timer switch on utility counter

Post by waaren »

klodek12 wrote: Monday 03 May 2021 13:54 My simple blockly logic that I shared was just example.
Not sure this is at all possible.
If it is there are quite a number of different conditions and states to control. Probably safer to install a detector that will signal if the screen is fully open or fully closed and use that signal to protect your motor.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
klodek12
Posts: 5
Joined: Sunday 02 May 2021 21:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Need help with timer switch on utility counter

Post by klodek12 »

Thank you for your feedback, I will think about it.

From the other hand could you tell me why this blockly code below doesn't work?
Image

When Selector called "Zdalne" is on "Level1" it should activate my scene "RoletyDół", but nothing changes.
When I active by myself this scene everything is working well, but from event no.

Settings of Selector:
Image

Settings of Scene:
Image
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Need help with timer switch on utility counter

Post by waaren »

klodek12 wrote: Monday 03 May 2021 22:18 From the other hand could you tell me why this blockly code below doesn't work?
You should not use
if else
if else
not.png (2.28 KiB) Viewed 1013 times


but use
if
if
use.png (6.35 KiB) Viewed 1013 times
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
klodek12
Posts: 5
Joined: Sunday 02 May 2021 21:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Need help with timer switch on utility counter

Post by klodek12 »

Really... wow. It's working now.

Thank you. I lost half day today to figure out what's going on :)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest