Page 1 of 2

Power plug script need big help

Posted: Thursday 19 January 2023 10:25
by Fredom
Dear forum members,

I have a zigbee power plug (see photo).
I would like to monitor consumption with this.
Now I found a script here on the forum that does exactly what I want.
I have modified a few things to suit my need.
Unfortunately I can't get the script to work, I've been trying it out for days.
To test I use a 15 watt bulb.
Can someone please help me with this.
I have put the IDX numbers on the photo
Everything with (--this is new) I have adapted myself

Script

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 (domoticzz.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
}

Error

Code: Select all

2023-01-17 21:32:00.325 Error: dzVents: Error: (3.1.8) Machine kosten: An error occurred when calling event handler Machine test
2023-01-17 21:32:00.325 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')
json from device 512

Code: Select all

{
	"ActTime" : 1673991319,
	"AstrTwilightEnd" : "19:04",
	"AstrTwilightStart" : "06:40",
	"CivTwilightEnd" : "17:41",
	"CivTwilightStart" : "08:02",
	"DayLength" : "08:22",
	"NautTwilightEnd" : "18:23",
	"NautTwilightStart" : "07:20",
	"ServerTime" : "2023-01-17 22:35:19",
	"SunAtSouth" : "12:52",
	"Sunrise" : "08:40",
	"Sunset" : "17:03",
	"app_version" : "2022.2",
	"result" : 
	[
		{
			"AddjMulti" : 1.0,
			"AddjMulti2" : 1.0,
			"AddjValue" : 0.0,
			"AddjValue2" : 0.0,
			"BatteryLevel" : 255,
			"CounterToday" : "0.080 kWh",
			"CustomImage" : 0,
			"Data" : "118.250 kWh",
			"Description" : "",
			"EnergyMeterMode" : "0",
			"Favorite" : 0,
			"HardwareDisabled" : false,
			"HardwareID" : 26,
			"HardwareName" : "Zigbee",
			"HardwareType" : "Zigbee2MQTT",
			"HardwareTypeVal" : 94,
			"HaveTimeout" : false,
			"ID" : "0x84fd27fffe3e26a4_power",
			"LastUpdate" : "2023-01-17 22:35:16",
			"Name" : "WCD smart (Power)",
			"Notifications" : "false",
			"PlanID" : "0",
			"PlanIDs" : 
			[
				0
			],
			"Protected" : false,
			"ShowNotifications" : true,
			"SignalLevel" : 1,
			"SubType" : "kWh",
			"SwitchTypeVal" : 0,
			"Timers" : "false",
			"Type" : "General",
			"TypeImg" : "current",
			"Unit" : 63,
			"Usage" : "13 Watt",
			"Used" : 1,
			"XOffset" : "0",
			"YOffset" : "0",
			"idx" : "512"
		}
	],
	"status" : "OK",
	"title" : "Devices"
}

Re: Power plug script need big help

Posted: Thursday 19 January 2023 11:05
by HvdW
Here you go.
First create a dummy device like this.
In fact on the left you see two devices of this type: Plug Power and Shelly21
plug.jpg
plug.jpg (36.87 KiB) Viewed 2220 times
Here is my script:

Code: Select all

-- Shelly (get watt)
-- Howto at Luftdaten https://www.domoticz.com/forum/viewtopic.php?f=72&t=23406&hilit=luftdaten
-- Call Shelly with http://192.168.x.x/status
--local Shelly = 'Shelly 21-0'

return {
	active = true,
	on = {
		timer = { 'every 10 minutes' },            -- every minute for testing, 10 minutes when active
		httpResponses = { 'Shelly21Retrieved' }  -- matches callback string below
	},
	execute = function(domoticz, item)

		if (item.isTimer) then
			domoticz.openURL({
				url = 'http://192.168.2.21/status',
				method = 'GET',
				callback = 'Shelly21Retrieved'
			})

		elseif (item.isHTTPResponse) then
			if (item.ok and item.isJSON) then
			    --domoticz.utils.dumpTable(item.json)   -- dumpTable laat alle waarden zien, mooi voor debugging
				domoticz.devices('Shelly 21').updateCustomSensor(item.json.meters[1].power)                
				-- domoticz.devices('Shelly 21').updateCustomSensor(item.json.meters[2].power)
				-- domoticz.notify('Power     ' .. item.json.meters[1].power, domoticz.PRIORITY_HIGH)     
				-- domoticz.notify('Is_valid  ' .. item.json.meters[1].is_valid, domoticz.PRIORITY_LOW) --howto display TRUE or FALSE?
				-- domoticz.log(item.json.meters[1].power, domoticz.LOG_ERROR)                         
				-- domoticz.notify('Timestamp ' .. item.json.meters[1].timestamp, domoticz.PRIORITY_LOW)  -- success
			else
				-- oops
				domoticz.log('Error fetching Shelly data', domoticz.LOG_ERROR)
				domoticz.log(item.data, domoticz.LOG_ERROR)
			end
		end
	end
}
I have left the extra information inside the script.
On top you can find where I have found the information.
The <domoticz.utils.dumpTable(item.json)> line shows all information in the domoticz log.

Re: Power plug script need big help

Posted: Thursday 19 January 2023 11:42
by Fredom
HvdW wrote: Thursday 19 January 2023 11:05 Here you go.
First create a dummy device like this.
In fact on the left you see two devices of this type: Plug Power and Shelly21
plug.jpg

Here is my script:

Code: Select all

-- Shelly (get watt)
-- Howto at Luftdaten https://www.domoticz.com/forum/viewtopic.php?f=72&t=23406&hilit=luftdaten
-- Call Shelly with http://192.168.x.x/status
--local Shelly = 'Shelly 21-0'

return {
	active = true,
	on = {
		timer = { 'every 10 minutes' },            -- every minute for testing, 10 minutes when active
		httpResponses = { 'Shelly21Retrieved' }  -- matches callback string below
	},
	execute = function(domoticz, item)

		if (item.isTimer) then
			domoticz.openURL({
				url = 'http://192.168.2.21/status',
				method = 'GET',
				callback = 'Shelly21Retrieved'
			})

		elseif (item.isHTTPResponse) then
			if (item.ok and item.isJSON) then
			    --domoticz.utils.dumpTable(item.json)   -- dumpTable laat alle waarden zien, mooi voor debugging
				domoticz.devices('Shelly 21').updateCustomSensor(item.json.meters[1].power)                
				-- domoticz.devices('Shelly 21').updateCustomSensor(item.json.meters[2].power)
				-- domoticz.notify('Power     ' .. item.json.meters[1].power, domoticz.PRIORITY_HIGH)     
				-- domoticz.notify('Is_valid  ' .. item.json.meters[1].is_valid, domoticz.PRIORITY_LOW) --howto display TRUE or FALSE?
				-- domoticz.log(item.json.meters[1].power, domoticz.LOG_ERROR)                         
				-- domoticz.notify('Timestamp ' .. item.json.meters[1].timestamp, domoticz.PRIORITY_LOW)  -- success
			else
				-- oops
				domoticz.log('Error fetching Shelly data', domoticz.LOG_ERROR)
				domoticz.log(item.data, domoticz.LOG_ERROR)
			end
		end
	end
}
I have left the extra information inside the script.
On top you can find where I have found the information.
The <domoticz.utils.dumpTable(item.json)> line shows all information in the domoticz log.
Hi HvdW,

Thank you very much for the quick response. I'll get to work on it right away and let you know if this is what I want

Re: Power plug script need big help

Posted: Thursday 19 January 2023 13:17
by Fredom
Hi HvdW,

Unfortunately this is not what I want.
What I want is a script that starts, resets everything, then starts measuring number of watts, the price total and running time then stop.
The script in my earlier post does that if I understood it correctly.
Unfortunately I can't get it to work

Re: Power plug script need big help

Posted: Thursday 19 January 2023 13:25
by waltervl
What device did the Zigbee gateway add to the power plug? As that should be a device like your 512 and that monitors everything you want Out of the Box. No need to script anything...

What Zigbee Gateway do you use? Zigbee2MQTT with plugin or MQTT Autodiscover? ZigbeeforDomoticz plugin?

Re: Power plug script need big help

Posted: Thursday 19 January 2023 13:27
by Fredom
HvdW wrote: Thursday 19 January 2023 11:05 Here you go.
First create a dummy device like this.
In fact on the left you see two devices of this type: Plug Power and Shelly21
plug.jpg

Here is my script:

Code: Select all

-- Shelly (get watt)
-- Howto at Luftdaten https://www.domoticz.com/forum/viewtopic.php?f=72&t=23406&hilit=luftdaten
-- Call Shelly with http://192.168.x.x/status
--local Shelly = 'Shelly 21-0'

return {
	active = true,
	on = {
		timer = { 'every 10 minutes' },            -- every minute for testing, 10 minutes when active
		httpResponses = { 'Shelly21Retrieved' }  -- matches callback string below
	},
	execute = function(domoticz, item)

		if (item.isTimer) then
			domoticz.openURL({
				url = 'http://192.168.2.21/status',
				method = 'GET',
				callback = 'Shelly21Retrieved'
			})

		elseif (item.isHTTPResponse) then
			if (item.ok and item.isJSON) then
			    --domoticz.utils.dumpTable(item.json)   -- dumpTable laat alle waarden zien, mooi voor debugging
				domoticz.devices('Shelly 21').updateCustomSensor(item.json.meters[1].power)                
				-- domoticz.devices('Shelly 21').updateCustomSensor(item.json.meters[2].power)
				-- domoticz.notify('Power     ' .. item.json.meters[1].power, domoticz.PRIORITY_HIGH)     
				-- domoticz.notify('Is_valid  ' .. item.json.meters[1].is_valid, domoticz.PRIORITY_LOW) --howto display TRUE or FALSE?
				-- domoticz.log(item.json.meters[1].power, domoticz.LOG_ERROR)                         
				-- domoticz.notify('Timestamp ' .. item.json.meters[1].timestamp, domoticz.PRIORITY_LOW)  -- success
			else
				-- oops
				domoticz.log('Error fetching Shelly data', domoticz.LOG_ERROR)
				domoticz.log(item.data, domoticz.LOG_ERROR)
			end
		end
	end
}
I have left the extra information inside the script.
On top you can find where I have found the information.
The <domoticz.utils.dumpTable(item.json)> line shows all information in the domoticz log.
Hoi HvdW

Now I use this script but it doesn't do everything I want (see my previous post)

Code: Select all

--[[     dzVents script dzVents script time
         om je opbrengst en verbruk om te zetten naar geld sensoren
        Script gedownload van huizebruin.nl
        Te gebruiken voor domoticz versie >= V2020.1
]]--
local ScriptVersion = '1.0.3' -- domoticz > V2020.1 / dzVents >= 2.4.28

return {
             on = { timer = { 'every 1 minutes'}},
        
        logging = { level = domoticz.LOG_DEBUG,    -- Maak commentaar op deze regel om de instelling van dzVents global logging te overschrijven
         marker = 'Dagkosten'
       }, 
 
        execute = function(domoticz, device, timer)
            
        local vandaagKwh      = domoticz.devices(512).counterToday 
        local StroomKosten    = domoticz.devices(962) --dummy sensor
--      Eenheidsprijs in Euro's / Kwh
--        local kwhPrijs = 0.45
         if (domoticz.time == 'Between 23:00 and 07:00') or (domoticz.day == 'Saturday') or (domoticz.day == 'Sunday') then
            kwhPrijs = 0.40 -- Daltarief
        else
            kwhPrijs = 0.45 -- Normaal tarief
        end 
        -- Vaste kosten in Euro's per dag (zoals vastrecht) 
        local kwhPrijsVast = 0.02
        -- Kosten berekenen
        local kwhKosten = tonumber(domoticz.utils.round( (kwhPrijs * vandaagKwh) + kwhPrijsVast,2))   
        -- Dummy sensor Kosten updaten
          StroomKosten.updateCustomSensor(kwhKosten)

    end

Re: Power plug script need big help

Posted: Thursday 19 January 2023 13:41
by Fredom
waltervl wrote: Thursday 19 January 2023 13:25 What device did the Zigbee gateway add to the power plug? As that should be a device like your 512 and that monitors everything you want Out of the Box. No need to script anything...

What Zigbee Gateway do you use? Zigbee2MQTT with plugin or MQTT Autodiscover? ZigbeeforDomoticz plugin?
Hoi Walter,
I am using Zigbee2MQTT with plugin v.1.19.1 (zStack12 20201127) and usb Texas_Instruments_TI_CC2531_USB_CDC___0X00124B001CCE0853-if00 -> ../../ttyACM0.
Zigbee has created
513 switch,
512 power,
514 current,
515 volts

Re: Power plug script need big help

Posted: Thursday 19 January 2023 13:44
by waltervl
Did you already look at the log and then the report page of device 512? as it contains all information. It only cannot differ between day and night prices. See also wiki https://www.domoticz.com/wiki/Managing_ ... ice_Report The example there is from a P1 smart meter device but power device has something similar.

Re: Power plug script need big help

Posted: Thursday 19 January 2023 14:56
by HvdW
@Fredom
Maybe you can find some inspiration here.

Re: Power plug script need big help

Posted: Thursday 19 January 2023 17:31
by Fredom
waltervl wrote: Thursday 19 January 2023 13:44 Did you already look at the log and then the report page of device 512? as it contains all information. It only cannot differ between day and night prices. See also wiki https://www.domoticz.com/wiki/Managing_ ... ice_Report The example there is from a P1 smart meter device but power device has something similar.
Hi walter,
Glad you want to help me with this.
I took some pictures of the logs and the switches.
Which I hope the script can work the way I want it to.
Unfortunately I can't figure out how to process something like this from the log in a script.
I am now able to make very small adjustments by trying a lot. At Cost test (947) there I would like to see the cost of the measurement

Re: Power plug script need big help

Posted: Thursday 19 January 2023 19:48
by Fredom
Hi Walter,
This is the switch of the smart plug.
Ik was hem vergeten erbij te plaatsen

Re: Power plug script need big help

Posted: Thursday 19 January 2023 20:28
by waltervl
I really do not understand what you are trying to report. The cost of the switched on? Per time it was switched on?
So something like
20-01-2022, 15:01 - 15:15 , 125 Wh, €0.05
20-01-2022, 15:45 - 16:50, 1000 Wh, €0.40

As this can only be reported in a text sensor.

If you just want to see the costs per day, month, year they are in the device report as you can see in your screenshots. As you only have 1 month of available data in year 2023 there is not much to report yet. What about year 2022?

Re: Power plug script need big help

Posted: Thursday 19 January 2023 21:14
by HvdW
Hi Fredom

There is enough information.
I suggest you to take a look at this topic.
From there you will be guided to huizebruin.

It takes a bit of studying a bit of patience, but I'm shure it will bring you to what you want to achieve.

Re: Power plug script need big help

Posted: Friday 20 January 2023 10:52
by Fredom
waltervl wrote: Thursday 19 January 2023 20:28 I really do not understand what you are trying to report. The cost of the switched on? Per time it was switched on?
So something like
20-01-2022, 15:01 - 15:15 , 125 Wh, €0.05
20-01-2022, 15:45 - 16:50, 1000 Wh, €0.40

As this can only be reported in a text sensor.

If you just want to see the costs per day, month, year they are in the device report as you can see in your screenshots. As you only have 1 month of available data in year 2023 there is not much to report yet. What about year 2022?
Hi walter,
What I would like if possible.
Switch (513) on then reset (512) and start measurement (512). Stop (513) Report duration, watts and costs in a report to a text sensor and Telegram.
If I understand the above script a bit then that should be about it.
The day and night cost is not that important but would be nice if possible.

Re: Power plug script need big help

Posted: Friday 20 January 2023 10:58
by Fredom
HvdW wrote: Thursday 19 January 2023 21:14 Hi Fredom

There is enough information.
I suggest you to take a look at this topic.
From there you will be guided to huizebruin.

It takes a bit of studying a bit of patience, but I'm shure it will bring you to what you want to achieve.
Hi HvdW,
Thank you for thinking along.
I've also seen what you suggested on this forum, but unfortunately it's not what I'm looking for and want.
I already have a script for my solar panels that works great.
It is now about a power plug that I can always use somewhere else to do a measurement. Then I want to know the duration, number of watts and the cost in a text sensor and telegram

Re: Power plug script need big help

Posted: Friday 20 January 2023 12:43
by waltervl
Fredom wrote: Friday 20 January 2023 10:52
waltervl wrote: Thursday 19 January 2023 20:28 I really do not understand what you are trying to report. The cost of the switched on? Per time it was switched on?
So something like
20-01-2022, 15:01 - 15:15 , 125 Wh, €0.05
20-01-2022, 15:45 - 16:50, 1000 Wh, €0.40

As this can only be reported in a text sensor.

If you just want to see the costs per day, month, year they are in the device report as you can see in your screenshots. As you only have 1 month of available data in year 2023 there is not much to report yet. What about year 2022?
Hi walter,
What I would like if possible.
Switch (513) on then reset (512) and start measurement (512). Stop (513) Report duration, watts and costs in a report to a text sensor and Telegram.
If I understand the above script a bit then that should be about it.
The day and night cost is not that important but would be nice if possible.
So the script should be triggered on device switch 513
if switched On, remember the time it is switched on and save the current Wh count (.WhToday) of device 512
If switched Off get the current Wh count (.WhToday) of device 512 and the delta in time of switch 513 and the delta in Wh count of device 512 and the delta Wh * costs should be reported to text sensor and telegram.

I think I gave you enough clues now to program this.

Re: Power plug script need big help

Posted: Friday 20 January 2023 17:09
by Fredom
waltervl wrote: Friday 20 January 2023 12:43
Fredom wrote: Friday 20 January 2023 10:52
waltervl wrote: Thursday 19 January 2023 20:28 I really do not understand what you are trying to report. The cost of the switched on? Per time it was switched on?
So something like
20-01-2022, 15:01 - 15:15 , 125 Wh, €0.05
20-01-2022, 15:45 - 16:50, 1000 Wh, €0.40

As this can only be reported in a text sensor.

If you just want to see the costs per day, month, year they are in the device report as you can see in your screenshots. As you only have 1 month of available data in year 2023 there is not much to report yet. What about year 2022?
Hi walter,
What I would like if possible.
Switch (513) on then reset (512) and start measurement (512). Stop (513) Report duration, watts and costs in a report to a text sensor and Telegram.
If I understand the above script a bit then that should be about it.
The day and night cost is not that important but would be nice if possible.
So the script should be triggered on device switch 513
if switched On, remember the time it is switched on and save the current Wh count (.WhToday) of device 512
If switched Off get the current Wh count (.WhToday) of device 512 and the delta in time of switch 513 and the delta in Wh count of device 512 and the delta Wh * costs should be reported to text sensor and telegram.

I think I gave you enough clues now to program this.

Hi walter,

Thanks for the thoughts and tips

Re: Power plug script need big help

Posted: Friday 20 January 2023 19:37
by HvdW
What I wanted to show you is this.
My Shelly plug script.
The other one I showed is the script to count electricity usage and return.
These are examples on how to handle things.
Read, understand and combine and you'll end up with what you want to achieve.

Re: Power plug script need big help

Posted: Friday 20 January 2023 20:01
by Fredom
HvdW wrote: Friday 20 January 2023 19:37 What I wanted to show you is this.
My Shelly plug script.
The other one I showed is the script to count electricity usage and return.
These are examples on how to handle things.
Read, understand and combine and you'll end up with what you want to achieve.
Hi HvdW,
I totally understood what you meant. Only I'm not a programmer this will never work unfortunately.
I can make very small adjustments by comparing a lot with other scripts. Then sometimes it works to get it working.
I had hoped to get the script in my earlier post working. But I have now also seen that if you read the log of a switch, you can also see what I want. I have to calculate it myself. Then it should be like this. Thank you for your effort.

Re: Power plug script need big help

Posted: Friday 20 January 2023 20:42
by HvdW
Read this carefully.
https://www.huizebruin.nl/domoticz/slim ... z-met-lua/
I learned a lot reading other people's scripts.