Power plug script need big help

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

Moderator: leecollings

hoeby
Posts: 531
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: Power plug script need big help

Post by hoeby »

I nower read the problem with the original script, wherefor the thread was started.

Code: Select all

If (domoticzz.time == 'Between 23:00 and 07:00') or (dz.day == 'Saturday') or (dz.day == 'Sunday') then     -- this is new
You made a typo, it has a double z in domoticz.
Other then that. Choose to work with domoticz.command of dz.command, not both
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
Fredom
Posts: 160
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Krimpen aan den IJssel
Contact:

Re: Power plug script need big help

Post by Fredom »

HvdW wrote: Friday 20 January 2023 20:42 Read this carefully.
https://www.huizebruin.nl/domoticz/slim ... z-met-lua/
I learned a lot reading other people's scripts.
I do indeed use this script to divide my P1 meter.
It works perfectly
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Trixie - Domoticz 2025.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
Fredom
Posts: 160
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Krimpen aan den IJssel
Contact:

Re: Power plug script need big help

Post by Fredom »

hoeby wrote: Saturday 21 January 2023 9:12 I nower read the problem with the original script, wherefor the thread was started.

Code: Select all

If (domoticzz.time == 'Between 23:00 and 07:00') or (dz.day == 'Saturday') or (dz.day == 'Sunday') then     -- this is new
You made a typo, it has a double z in domoticz.
Other then that. Choose to work with domoticz.command of dz.command, not both
Hi hoeby,
Thank you for your response.
This I had not seen sloppy of me.
At the bottom of the script is the following error which I don't understand.

Code: Select all

-- This is a dzVents-script for monitoring a machine. It will send a notification when the machine has finnished its run. The message will include duration, powewr used and the cost.
-- I found the script on the domoticz forum, created by Ragdag and I wanted changed it to use Telegram and also added duration and some other features.
-- How to set it up? Find all comments with SETUP and update to fit your needs. For test, use a lamp as a load and set MachineStandbyUsage to 1 and MachineStartUsage to 3. Easier than starting and stopping your washer ?
-- If my suggested parameters dont work with your machine, start the machine and look in domoticz to see the power used by the machine in the beginning of the cycle and in the end.
-- Regards, Jens M�nsson

-- 220119, Version 1.0: Release, started of with https://www.domoticz.com/forum/viewtopic.php?f=59&t=32445&p=245629&hilit=ragdag#p245629 and added some features. Running fine for 1 week....

local scriptVar = 'Machine kosten'                                                 -- SETUP, Will be visable in the log, set it to something you can relate to like Dishwasher

return
{
    on =
    {
        timer = 
        {
            'every 1 minutes',
        },
        httpResponses = 
        {
            scriptVar,
        },
    },

    logging = 
    {
    level = domoticz.LOG_INFO,                                                       -- OPTIONAL, Set to LOG_DEBUG for debug info, set to LOG_INFO for minimal logs, Default = LOG_INFO
        marker = scriptVar,
    },
    
    data = 
    {
        MachineTimeout = { initial = 5 },                                            -- SETUP, Timeout value used in combination with stand by power to detect Machine is stopped, default value for Dishwasher/WashingMach/Dryer: 5/3/1
        MachineInUse = { initial = 0 },
        MachineStartkWh = { initial = 0 },
        MachineEndkWh = { initial = 0 },
        StartTime = { initial = 0 },
        StopTime = { initial = 0 },
    },

    execute = function(domoticz,dz, item)
    
        if item.isHTTPResponse then
            domoticz.log(item.data,domoticz.LOG_DEBUG)
            return
        end

        local Time = require('Time')
        local MachineUsage = domoticz.devices(512)              			        	-- SETUP, This is the energy measuring device (see photo)
        local MachinePricePerkWh = domoticz.CurrentPrice.value                      --this is new 
      --local MachinePricePerkWh = domoticz.variables('VariabelCurrentPrice').value     -- SETUP, This is the price per kWh, replace with a fixed value if you prefer that
        local MachineStandbyUsage = 1.0                                             	-- SETUP, If below this power the Machine is in standby = finished, default value for Dishwasher/WashingMach/Dryer: 3/3/3
        local MachineStartUsage = 3.0                                             	 	-- SETUP, If above this power the Machine us running, default value for Dishwasher/WashingMach/Dryer: 50/10/500
        local SendStartNotification = true                                          	-- SETUP, If you like to get a notification when machine starts set to true
        local MachineCost
        local MachineUsed
        local MachineDuration
        local StartTimeCalc
        local StopTimeCalc
        local CurrentPrice                                                              --this is new

        if (MachineUsage.actualWatt >= MachineStartUsage) and (domoticz.data.MachineInUse == 0) then
            domoticz.data.MachineStartkWh = MachineUsage.WhTotal
            domoticz.data.StartTime = Time(domoticz.time.raw)
            domoticz.data.MachineInUse = 1
            if (SendStartNotification == true) then
            -- SETUP, Change the message into what ever you want.
            domoticz.notify('Machine is gestart!\nEn kWh kostar just nu ' .. MachinePricePerkWh ..' Euro')     -- SETUP, Change text to fit your needs
            end
            domoticz.log("------ The Machine was started " .. tostring(domoticz.data.StartTime.raw), domoticz.LOG_INFO)
            domoticz.log("Timeout value is set to " .. domoticz.data.MachineTimeout, domoticz.LOG_DEBUG)
        end

        if (MachineUsage.actualWatt >= MachineStandbyUsage) and (domoticz.data.MachineInUse == 1) and (domoticz.data.MachineTimeout == 0) then
            domoticz.data.MachineTimeout = 1
            domoticz.log("Reseting counter, not done yet.", domoticz.LOG_DEBUG)
            domoticz.log("Timeout value is now " .. domoticz.data.MachineTimeout, domoticz.LOG_DEBUG)
            domoticz.log("In use = " .. domoticz.data.MachineInUse, domoticz.LOG_DEBUG)
        end
        
        if (domoticz.time == 'Between 23:00 and 07:00') or (dz.day == 'Saturday') or (dz.day == 'Sunday') then     -- this is new
             CurrentPrice = kwhPrijs ( 0.56565)  -- Daltarief                                                       -- this is new
        else CurrentPrice = kwhPrijs ( 0.6772)   -- Normaal tarief                                                  --this is new
           
        end                                                                                                         --this is new
        
        if (MachineUsage.actualWatt <= MachineStandbyUsage) and (domoticz.data.MachineInUse == 1) then
            if (domoticz.data.MachineTimeout > 0) then
                domoticz.data.MachineTimeout = domoticz.data.MachineTimeout - 1
                domoticz.log(MachineUsage.actualWatt .. ' Watt usage, below standby usage, timout minus 1', domoticz.LOG_DEBUG)
                domoticz.log('Timeout value is now ' .. domoticz.data.MachineTimeout, domoticz.LOG_DEBUG)
                domoticz.log('In use = ' .. domoticz.data.MachineInUse, domoticz.LOG_DEBUG)
        elseif (domoticz.data.MachineTimeout == 0) then
                domoticz.data.MachineInUse = 0
                domoticz.data.MachineTimeout = 1
                domoticz.data.MachineEndkWh = MachineUsage.WhTotal
                MachineCost = (domoticz.data.MachineEndkWh - domoticz.data.MachineStartkWh) / 100000 * MachinePricePerkWh       -- Deviding factor might need to be adjusted
                MachineUsed = (domoticz.data.MachineEndkWh - domoticz.data.MachineStartkWh) / 1000                              -- Deviding factor might need to be adjusted
                StartTimeCalc = domoticz.data.StartTime
                domoticz.data.StopTime = Time(domoticz.time.raw)
                StopTimeCalc = Time(domoticz.time.raw)
                MachineDuration =  StopTimeCalc.compare(StartTimeCalc).minutes

                -- SETUP, Change message, power unit and currency to fit your needs
                domoticz.notify('Machine is klaar!\n??:' .. MachineDuration .. 'min ?:' .. domoticz.utils.numDecimals(MachineUsed, 2, 2) .. 'kWh ?:' .. domoticz.utils.numDecimals(MachineCost, 2, 2) .. 'Euro')

                domoticz.log("------ The Machine was stopped " .. tostring(domoticz.data.StopTime.raw), domoticz.LOG_INFO)
                domoticz.log('Start time = ' .. tostring(StartTimeCalc.raw), domoticz.LOG_DEBUG)
                domoticz.log('Stop time = ' .. tostring(StopTimeCalc.raw), domoticz.LOG_DEBUG)
                domoticz.log('Start kWh = ' .. domoticz.data.MachineStartkWh, domoticz.LOG_DEBUG)
                domoticz.log('End kWh = ' .. domoticz.data.MachineEndkWh, domoticz.LOG_DEBUG)
                domoticz.log('Washing cost = ' .. domoticz.utils.numDecimals(MachineCost, 2, 2), domoticz.LOG_DEBUG)
                domoticz.log('Timeout = ' .. domoticz.data.MachineTimeout, domoticz.LOG_DEBUG)
                domoticz.log('In use = ' .. domoticz.data.MachineInUse, domoticz.LOG_DEBUG)
                
            end
        end
    
        if (domoticz.data.MachineInUse == 1) then
            domoticz.log('------ Machine is running', domoticz.LOG_INFO)
        else
            domoticz.log('------ Machine is NOT running', domoticz.LOG_INFO)
        end
    end
}

--[[
2023-01-21 11:28:00.573 Error: dzVents: Error: (3.1.8) Machine kosten: An error occurred when calling event handler Machine test
2023-01-21 11:28:00.573 Error: dzVents: Error: (3.1.8) Machine kosten: ...oticz/scripts/dzVents/generated_scripts/Machine test.lua:50: attempt to index a nil value (field 'CurrentPrice')
]]--
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Trixie - Domoticz 2025.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
hoeby
Posts: 531
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: Power plug script need big help

Post by hoeby »

The problem on line 50 is, that you set to look to variable "currentprice".
But currentprice is set on line 86. This is to late.
The script is running from line 1 to the end. It doesn't search in the script where it can't find its information.

This part where the "CurrentPrice" is set, needs to be above line 50, before "local MachinePricePerkWh" is set

Code: Select all

        if (domoticz.time == 'Between 23:00 and 07:00') or (dz.day == 'Saturday') or (dz.day == 'Sunday') then     -- this is new
             CurrentPrice = kwhPrijs ( 0.56565)  -- Daltarief                                                       -- this is new
        else CurrentPrice = kwhPrijs ( 0.6772)   -- Normaal tarief                                                  --this is new
           
        end    [code]

Have a look at what you set your CurrentPrice to.
kwhPrijs ( 0.56565) is not something you can use. It is not a value, it is text, but on line 50 you want a value.

Also my advice. Use domoticz.xxxx or dz.xxxx. Not both

Change this line in:
[code] execute = function(domoticz,dz, item)
to

Code: Select all

 execute = function(domoticz, item)
and change all dz.xxxx to domoticz.xxxx

domoticz and dz are the same. only 1 is complety written out, the otherone is the short version.
choose 1 of these in your script, not both

Second advice. remove everything with CurrentPrice.
And change it to MachinePricePerkWh. No you have made a variable to set an other variable with the same setting

Not tested, but this is what i mean.

Code: Select all

-- This is a dzVents-script for monitoring a machine. It will send a notification when the machine has finnished its run. The message will include duration, powewr used and the cost.
-- I found the script on the domoticz forum, created by Ragdag and I wanted changed it to use Telegram and also added duration and some other features.
-- How to set it up? Find all comments with SETUP and update to fit your needs. For test, use a lamp as a load and set MachineStandbyUsage to 1 and MachineStartUsage to 3. Easier than starting and stopping your washer ?
-- If my suggested parameters dont work with your machine, start the machine and look in domoticz to see the power used by the machine in the beginning of the cycle and in the end.
-- Regards, Jens M nsson

-- 220119, Version 1.0: Release, started of with https://www.domoticz.com/forum/viewtopic.php?f=59&t=32445&p=245629&hilit=ragdag#p245629 and added some features. Running fine for 1 week....

local scriptVar = 'Machine kosten'                                                 -- SETUP, Will be visable in the log, set it to something you can relate to like Dishwasher

return
{
    on =
    {
        timer = 
        {
            'every 1 minutes',
        },
        httpResponses = 
        {
            scriptVar,
        },
    },

    logging = 
    {
    level = domoticz.LOG_INFO,                                                       -- OPTIONAL, Set to LOG_DEBUG for debug info, set to LOG_INFO for minimal logs, Default = LOG_INFO
        marker = scriptVar,
    },
    
    data = 
    {
        MachineTimeout = { initial = 5 },                                            -- SETUP, Timeout value used in combination with stand by power to detect Machine is stopped, default value for Dishwasher/WashingMach/Dryer: 5/3/1
        MachineInUse = { initial = 0 },
        MachineStartkWh = { initial = 0 },
        MachineEndkWh = { initial = 0 },
        StartTime = { initial = 0 },
        StopTime = { initial = 0 },
    },

    execute = function(domoticz, item)
    
        if item.isHTTPResponse then
            domoticz.log(item.data,domoticz.LOG_DEBUG)
            return
        end

        local Time = require('Time')
        local MachineUsage = domoticz.devices(512)              			        	-- SETUP, This is the energy measuring device (see photo)
        local MachinePricePerkWh                                                        --this is new 
      --local MachinePricePerkWh = domoticz.variables('VariabelCurrentPrice').value     -- SETUP, This is the price per kWh, replace with a fixed value if you prefer that
        local MachineStandbyUsage = 1.0                                             	-- SETUP, If below this power the Machine is in standby = finished, default value for Dishwasher/WashingMach/Dryer: 3/3/3
        local MachineStartUsage = 3.0                                             	 	-- SETUP, If above this power the Machine us running, default value for Dishwasher/WashingMach/Dryer: 50/10/500
        local SendStartNotification = true                                          	-- SETUP, If you like to get a notification when machine starts set to true
        local MachineCost
        local MachineUsed
        local MachineDuration
        local StartTimeCalc
        local StopTimeCalc

        if (domoticz.time == 'Between 23:00 and 07:00') or (domoticz.day == 'Saturday') or (domoticz.day == 'Sunday') then     -- this is new
            MachinePricePerkWh = 0.56565  --kwhPrijs Daltarief                                                       -- this is new
        else 
            MachinePricePerkWh = 0.6772   --kwhPrijs Normaal tarief                                                  --this is new
        end                  

        if (MachineUsage.actualWatt >= MachineStartUsage) and (domoticz.data.MachineInUse == 0) then
            domoticz.data.MachineStartkWh = MachineUsage.WhTotal
            domoticz.data.StartTime = Time(domoticz.time.raw)
            domoticz.data.MachineInUse = 1
            if (SendStartNotification == true) then
            -- SETUP, Change the message into what ever you want.
            domoticz.notify('Machine is gestart!\nEn kWh kostar just nu ' .. MachinePricePerkWh ..' Euro')     -- SETUP, Change text to fit your needs
            end
            domoticz.log("------ The Machine was started " .. tostring(domoticz.data.StartTime.raw), domoticz.LOG_INFO)
            domoticz.log("Timeout value is set to " .. domoticz.data.MachineTimeout, domoticz.LOG_DEBUG)
        end

        if (MachineUsage.actualWatt >= MachineStandbyUsage) and (domoticz.data.MachineInUse == 1) and (domoticz.data.MachineTimeout == 0) then
            domoticz.data.MachineTimeout = 1
            domoticz.log("Reseting counter, not done yet.", domoticz.LOG_DEBUG)
            domoticz.log("Timeout value is now " .. domoticz.data.MachineTimeout, domoticz.LOG_DEBUG)
            domoticz.log("In use = " .. domoticz.data.MachineInUse, domoticz.LOG_DEBUG)
        end
        
        if (MachineUsage.actualWatt <= MachineStandbyUsage) and (domoticz.data.MachineInUse == 1) then
            if (domoticz.data.MachineTimeout > 0) then
                domoticz.data.MachineTimeout = domoticz.data.MachineTimeout - 1
                domoticz.log(MachineUsage.actualWatt .. ' Watt usage, below standby usage, timout minus 1', domoticz.LOG_DEBUG)
                domoticz.log('Timeout value is now ' .. domoticz.data.MachineTimeout, domoticz.LOG_DEBUG)
                domoticz.log('In use = ' .. domoticz.data.MachineInUse, domoticz.LOG_DEBUG)
        elseif (domoticz.data.MachineTimeout == 0) then
                domoticz.data.MachineInUse = 0
                domoticz.data.MachineTimeout = 1
                domoticz.data.MachineEndkWh = MachineUsage.WhTotal
                MachineCost = (domoticz.data.MachineEndkWh - domoticz.data.MachineStartkWh) / 100000 * MachinePricePerkWh       -- Deviding factor might need to be adjusted
                MachineUsed = (domoticz.data.MachineEndkWh - domoticz.data.MachineStartkWh) / 1000                              -- Deviding factor might need to be adjusted
                StartTimeCalc = domoticz.data.StartTime
                domoticz.data.StopTime = Time(domoticz.time.raw)
                StopTimeCalc = Time(domoticz.time.raw)
                MachineDuration =  StopTimeCalc.compare(StartTimeCalc).minutes

                -- SETUP, Change message, power unit and currency to fit your needs
                domoticz.notify('Machine is klaar!\n??:' .. MachineDuration .. 'min ?:' .. domoticz.utils.numDecimals(MachineUsed, 2, 2) .. 'kWh ?:' .. domoticz.utils.numDecimals(MachineCost, 2, 2) .. 'Euro')

                domoticz.log("------ The Machine was stopped " .. tostring(domoticz.data.StopTime.raw), domoticz.LOG_INFO)
                domoticz.log('Start time = ' .. tostring(StartTimeCalc.raw), domoticz.LOG_DEBUG)
                domoticz.log('Stop time = ' .. tostring(StopTimeCalc.raw), domoticz.LOG_DEBUG)
                domoticz.log('Start kWh = ' .. domoticz.data.MachineStartkWh, domoticz.LOG_DEBUG)
                domoticz.log('End kWh = ' .. domoticz.data.MachineEndkWh, domoticz.LOG_DEBUG)
                domoticz.log('Washing cost = ' .. domoticz.utils.numDecimals(MachineCost, 2, 2), domoticz.LOG_DEBUG)
                domoticz.log('Timeout = ' .. domoticz.data.MachineTimeout, domoticz.LOG_DEBUG)
                domoticz.log('In use = ' .. domoticz.data.MachineInUse, domoticz.LOG_DEBUG)
                
            end
        end
    
        if (domoticz.data.MachineInUse == 1) then
            domoticz.log('------ Machine is running', domoticz.LOG_INFO)
        else
            domoticz.log('------ Machine is NOT running', domoticz.LOG_INFO)
        end
    end
}
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
Fredom
Posts: 160
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Krimpen aan den IJssel
Contact:

Re: Power plug script need big help

Post by Fredom »

hoeby wrote: Saturday 21 January 2023 18:52 The problem on line 50 is, that you set to look to variable "currentprice".
But currentprice is set on line 86. This is to late.
The script is running from line 1 to the end. It doesn't search in the script where it can't find its information.

This part where the "CurrentPrice" is set, needs to be above line 50, before "local MachinePricePerkWh" is set

Code: Select all

        if (domoticz.time == 'Between 23:00 and 07:00') or (dz.day == 'Saturday') or (dz.day == 'Sunday') then     -- this is new
             CurrentPrice = kwhPrijs ( 0.56565)  -- Daltarief                                                       -- this is new
        else CurrentPrice = kwhPrijs ( 0.6772)   -- Normaal tarief                                                  --this is new
           
        end    [code]

Have a look at what you set your CurrentPrice to.
kwhPrijs ( 0.56565) is not something you can use. It is not a value, it is text, but on line 50 you want a value.

Also my advice. Use domoticz.xxxx or dz.xxxx. Not both

Change this line in:
[code] execute = function(domoticz,dz, item)
to

Code: Select all

 execute = function(domoticz, item)
and change all dz.xxxx to domoticz.xxxx

domoticz and dz are the same. only 1 is complety written out, the otherone is the short version.
choose 1 of these in your script, not both

Second advice. remove everything with CurrentPrice.
And change it to MachinePricePerkWh. No you have made a variable to set an other variable with the same setting

Not tested, but this is what i mean.

Code: Select all

-- This is a dzVents-script for monitoring a machine. It will send a notification when the machine has finnished its run. The message will include duration, powewr used and the cost.
-- I found the script on the domoticz forum, created by Ragdag and I wanted changed it to use Telegram and also added duration and some other features.
-- How to set it up? Find all comments with SETUP and update to fit your needs. For test, use a lamp as a load and set MachineStandbyUsage to 1 and MachineStartUsage to 3. Easier than starting and stopping your washer ?
-- If my suggested parameters dont work with your machine, start the machine and look in domoticz to see the power used by the machine in the beginning of the cycle and in the end.
-- Regards, Jens M nsson

-- 220119, Version 1.0: Release, started of with https://www.domoticz.com/forum/viewtopic.php?f=59&t=32445&p=245629&hilit=ragdag#p245629 and added some features. Running fine for 1 week....

local scriptVar = 'Machine kosten'                                                 -- SETUP, Will be visable in the log, set it to something you can relate to like Dishwasher

return
{
    on =
    {
        timer = 
        {
            'every 1 minutes',
        },
        httpResponses = 
        {
            scriptVar,
        },
    },

    logging = 
    {
    level = domoticz.LOG_INFO,                                                       -- OPTIONAL, Set to LOG_DEBUG for debug info, set to LOG_INFO for minimal logs, Default = LOG_INFO
        marker = scriptVar,
    },
    
    data = 
    {
        MachineTimeout = { initial = 5 },                                            -- SETUP, Timeout value used in combination with stand by power to detect Machine is stopped, default value for Dishwasher/WashingMach/Dryer: 5/3/1
        MachineInUse = { initial = 0 },
        MachineStartkWh = { initial = 0 },
        MachineEndkWh = { initial = 0 },
        StartTime = { initial = 0 },
        StopTime = { initial = 0 },
    },

    execute = function(domoticz, item)
    
        if item.isHTTPResponse then
            domoticz.log(item.data,domoticz.LOG_DEBUG)
            return
        end

        local Time = require('Time')
        local MachineUsage = domoticz.devices(512)              			        	-- SETUP, This is the energy measuring device (see photo)
        local MachinePricePerkWh                                                        --this is new 
      --local MachinePricePerkWh = domoticz.variables('VariabelCurrentPrice').value     -- SETUP, This is the price per kWh, replace with a fixed value if you prefer that
        local MachineStandbyUsage = 1.0                                             	-- SETUP, If below this power the Machine is in standby = finished, default value for Dishwasher/WashingMach/Dryer: 3/3/3
        local MachineStartUsage = 3.0                                             	 	-- SETUP, If above this power the Machine us running, default value for Dishwasher/WashingMach/Dryer: 50/10/500
        local SendStartNotification = true                                          	-- SETUP, If you like to get a notification when machine starts set to true
        local MachineCost
        local MachineUsed
        local MachineDuration
        local StartTimeCalc
        local StopTimeCalc

        if (domoticz.time == 'Between 23:00 and 07:00') or (domoticz.day == 'Saturday') or (domoticz.day == 'Sunday') then     -- this is new
            MachinePricePerkWh = 0.56565  --kwhPrijs Daltarief                                                       -- this is new
        else 
            MachinePricePerkWh = 0.6772   --kwhPrijs Normaal tarief                                                  --this is new
        end                  

        if (MachineUsage.actualWatt >= MachineStartUsage) and (domoticz.data.MachineInUse == 0) then
            domoticz.data.MachineStartkWh = MachineUsage.WhTotal
            domoticz.data.StartTime = Time(domoticz.time.raw)
            domoticz.data.MachineInUse = 1
            if (SendStartNotification == true) then
            -- SETUP, Change the message into what ever you want.
            domoticz.notify('Machine is gestart!\nEn kWh kostar just nu ' .. MachinePricePerkWh ..' Euro')     -- SETUP, Change text to fit your needs
            end
            domoticz.log("------ The Machine was started " .. tostring(domoticz.data.StartTime.raw), domoticz.LOG_INFO)
            domoticz.log("Timeout value is set to " .. domoticz.data.MachineTimeout, domoticz.LOG_DEBUG)
        end

        if (MachineUsage.actualWatt >= MachineStandbyUsage) and (domoticz.data.MachineInUse == 1) and (domoticz.data.MachineTimeout == 0) then
            domoticz.data.MachineTimeout = 1
            domoticz.log("Reseting counter, not done yet.", domoticz.LOG_DEBUG)
            domoticz.log("Timeout value is now " .. domoticz.data.MachineTimeout, domoticz.LOG_DEBUG)
            domoticz.log("In use = " .. domoticz.data.MachineInUse, domoticz.LOG_DEBUG)
        end
        
        if (MachineUsage.actualWatt <= MachineStandbyUsage) and (domoticz.data.MachineInUse == 1) then
            if (domoticz.data.MachineTimeout > 0) then
                domoticz.data.MachineTimeout = domoticz.data.MachineTimeout - 1
                domoticz.log(MachineUsage.actualWatt .. ' Watt usage, below standby usage, timout minus 1', domoticz.LOG_DEBUG)
                domoticz.log('Timeout value is now ' .. domoticz.data.MachineTimeout, domoticz.LOG_DEBUG)
                domoticz.log('In use = ' .. domoticz.data.MachineInUse, domoticz.LOG_DEBUG)
        elseif (domoticz.data.MachineTimeout == 0) then
                domoticz.data.MachineInUse = 0
                domoticz.data.MachineTimeout = 1
                domoticz.data.MachineEndkWh = MachineUsage.WhTotal
                MachineCost = (domoticz.data.MachineEndkWh - domoticz.data.MachineStartkWh) / 100000 * MachinePricePerkWh       -- Deviding factor might need to be adjusted
                MachineUsed = (domoticz.data.MachineEndkWh - domoticz.data.MachineStartkWh) / 1000                              -- Deviding factor might need to be adjusted
                StartTimeCalc = domoticz.data.StartTime
                domoticz.data.StopTime = Time(domoticz.time.raw)
                StopTimeCalc = Time(domoticz.time.raw)
                MachineDuration =  StopTimeCalc.compare(StartTimeCalc).minutes

                -- SETUP, Change message, power unit and currency to fit your needs
                domoticz.notify('Machine is klaar!\n??:' .. MachineDuration .. 'min ?:' .. domoticz.utils.numDecimals(MachineUsed, 2, 2) .. 'kWh ?:' .. domoticz.utils.numDecimals(MachineCost, 2, 2) .. 'Euro')

                domoticz.log("------ The Machine was stopped " .. tostring(domoticz.data.StopTime.raw), domoticz.LOG_INFO)
                domoticz.log('Start time = ' .. tostring(StartTimeCalc.raw), domoticz.LOG_DEBUG)
                domoticz.log('Stop time = ' .. tostring(StopTimeCalc.raw), domoticz.LOG_DEBUG)
                domoticz.log('Start kWh = ' .. domoticz.data.MachineStartkWh, domoticz.LOG_DEBUG)
                domoticz.log('End kWh = ' .. domoticz.data.MachineEndkWh, domoticz.LOG_DEBUG)
                domoticz.log('Washing cost = ' .. domoticz.utils.numDecimals(MachineCost, 2, 2), domoticz.LOG_DEBUG)
                domoticz.log('Timeout = ' .. domoticz.data.MachineTimeout, domoticz.LOG_DEBUG)
                domoticz.log('In use = ' .. domoticz.data.MachineInUse, domoticz.LOG_DEBUG)
                
            end
        end
    
        if (domoticz.data.MachineInUse == 1) then
            domoticz.log('------ Machine is running', domoticz.LOG_INFO)
        else
            domoticz.log('------ Machine is NOT running', domoticz.LOG_INFO)
        end
    end
}
hi hoeby,

The script works without errors, which is nice.
What doesn't work yet is that it doesn't work with the off-peak rate. No problem if it's not possible. I have attached a picture from Telegram.
What is also not correct are the number of watts and the final price, but I still have to adjust that with the share factor
Attachments
Screenshot_20230122_212705_Telegram.jpg
Screenshot_20230122_212705_Telegram.jpg (136.62 KiB) Viewed 335 times
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Trixie - Domoticz 2025.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest