Turn off a specific device when another specific one is enabled

Moderator: leecollings

Post Reply
tntteam
Posts: 4
Joined: Wednesday 13 November 2019 14:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Turn off a specific device when another specific one is enabled

Post by tntteam »

Hello there!

Sorry for my approximative english...

I have a domoticz with custom webapp ontop to manage the heating of my house.

I have a specific issue is that my heating is made with an electric system that can consume up to 8kW.
My water heater box is consumming 3kW.

Both systems are controlled with domoticz.

My energy provider is giving me 9kW maximum.

To deal with that at this moment, I tweaked my electric heating system to use only 4kW maximum.

I would like to "script" (or any other way) a "priority" like system :
I have setup a schedule for my water heater box, say from 22:00 to 05:00

I would like that, when the electric heating system is turned ON, it should force my water heater box to be OFF.
In other words, give the priority to my electric heating system.

Also there should be a timer (like 1 min) for safety, because if the electric heating system turns ON before the water heater box turns OFF (even a few seconds) it will interrupt my whole energy in the house because I would consume 11kW at a specific point of time, and my electric provider will not allow it.

Do you think it is possible, and how ?

Maybe it could be possible to make a "virtual switch" that would be the "electric heating system" switch, and would behave like this :

When ON order received :
turn water heater box switch OFF
disable schedule on water heater box switch
wait 60s
turn electric heating system ON

When OFF order received :
turn electric heating system OFF
wait 60s
enable schedule on water heater box switch (so that if schedule 22:00 to 05:00 says it should be ON, then it will send ON order)

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

Re: Turn off a specific device when another specific one is enabled

Post by waaren »

tntteam wrote: Wednesday 13 November 2019 14:58 I have a specific issue is that my heating is made with an electric system that can consume up to 8kW.
My water heater box is consuming 3kW.
Both systems are controlled with domoticz.
It should be possible to code this. I can do this in dzVents but before doing that I want to make sure you want that and if my understanding of the solution will fly.

Is this approach (in pseudo code) could work ?

switch definitions:
rWH: real waterHeater switch
rEH: real electricHeater switch
vWH: virtual waterHeater switch used in scheduler; controls rWH.
vEH: virtual electricHeater switch; controls rEH

script will be triggered on vWH, vEH

logic:
if vEH -> On then vWH + rWH -> Off and rEH -> On after 1 minute
if vWH -> On then if vEH = On or rEH = On then vWH -> Off
if vEH -> Off then rEH -> Off and if in time window then vWH -> On after 1 minute
if vWH -> Off then rEH -> Off
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
tntteam
Posts: 4
Joined: Wednesday 13 November 2019 14:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Turn off a specific device when another specific one is enabled

Post by tntteam »

Hello!

Thanks for your input, i m happy because it seems what I wrote was somewhat understandable :D

You used eWH but i think you meant vHW, as eWH is not defined ?

Using your pseudo code, here is how I see it working :

switch definitions:
rWH: real waterHeater switch
rEH: real electricHeater switch
vWH: virtual waterHeater switch used in scheduler; controls rWH.
vEH: virtual electricHeater switch; controls rEH

script will be triggered on vWH, vEH

logic:
if vEH -> On then vWH + rWH -> Off and rEH -> On after 1 minute**
if vWH -> On then if vEH = On or rEH = On then vWH -> Off
if vEH -> Off then rEH -> Off and if in time window then vWH -> On after 1 minute **
if vWH -> Off then rWH -> Off

** : quick question about the internal logic of domoticz regarding schedules.
If I have a device scheduled to be on from 8 to 10, domoticz turns it on at 8. If i stop it manually at 9, will domoticz see that it is not respecting the schedule and turn on the device again until 10 ?
Or do domoticz just register 2 triggers, ON at 8 and OFF at 10 ?
Because this would change a bit the logic of the pseudo code.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Turn off a specific device when another specific one is enabled

Post by waaren »

tntteam wrote: Thursday 14 November 2019 7:23 Thanks for your input, i m happy because it seems what I wrote was somewhat understandable :D
You used eWH but i think you meant vHW, as eWH is not defined ?
Yes, I made a typo. I edit the previous post
** : quick question about the internal logic of domoticz regarding schedules.
If I have a device scheduled to be on from 8 to 10, domoticz turns it on at 8. If i stop it manually at 9, will domoticz see that it is not respecting the schedule and turn on the device again until 10 ?
The domoticz schedule will not re-enforce the scheduled On after manual intervention, It is a one time switch.

Can you test below dzVents script ?

Code: Select all

--[[
    This script does manage the power usage of Electrical heating / water heating to
    prevent overloading the electrical system. It does this by preventing both systems to be active at the same time
    with a minimum gap of 1 minute.
    For this it uses two virtual switches that will trigger this script. The logic in the script will decide if and when the 
    real switches are to be activated.
    
    rWH: real waterHeater switch
    rEH: real electricHeater switch
    vWH: virtual waterHeater switch used in scheduler; controls rWH.
    vEH: virtual electricHeater switch; controls rEH

    script will be triggered on vWH, vEH

    logic:
    if vEH -> On then vWH + rWH -> Off and rEH -> On after 1 minute
    if vWH -> On then if vEH = On or rEH = On then vWH -> Off
    if vEH -> Off then rEH -> Off and if in time window then vWH -> On after 1 minute 
    if vWH -> Off then rWH -> Off

    History
    ========
    20191114 Start coding
]]

-- ********* Your settings below this line 
        
        local virtualElectricalHeating = 'vEH'  -- change to name of your virtual switch for Electrical heating
        local realElectricalHeating = 'rEH'  -- change to name of your real switch for Electrical heating
        local virtualWaterHeating = 'vWH'  -- change to name of your virtual switch for Water heating
        local realWaterHeating = 'rWH'  -- change to name of your real switch for Waterheating
        
        local waterHeatingschedule = 'at 22:00-05:00' 
        local spikeProtectionTime = 60
        
-- ********** No  changes required below this line
        
return
{
    on = {devices = { virtualElectricalHeating, virtualWaterHeating }},
       
    logging = { level = domoticz.LOG_DEBUG,}, -- Select one of LOG_INFO, LOG_DEBUG, LOG_ERROR, LOG_FORCE to override system log level

    execute = function(dz, item)
        _G.logMarker =  _G.moduleLabel
       
        local virtualElectricalHeating = dz.devices(virtualElectricalHeating)
        local realElectricalHeating = dz.devices(realElectricalHeating)
        local virtualWaterHeating = dz.devices(virtualWaterHeating)
        local realWaterHeating = dz.devices(realWaterHeating)
              
        -- if vEH -> On then vWH + rWH -> Off and rEH -> On after xx seconds
        if item == virtualElectricalHeating and item.active then
            virtualWaterHeating.switchOff().silent()
            realWaterHeating.switchOff()
            realElectricalHeating.switchOn().afterSec(spikeProtectionTime)
        
        -- if vEH -> Off then rEH -> Off and if in time window then vWH -> On 
        elseif item == virtualElectricalHeating then 
            realElectricalHeating.switchOff() 
            if dz.time.matchesRule(waterHeatingschedule) then
                virtualWaterHeating.switchOn()
            end
            
        -- if vWH -> On then if virtualElectricalHeating = On or rEH = On then vWH -> Off else rWH -> On after xx seconds
        elseif item == virtualWaterHeating and item.active then
            if virtualElectricalHeating.active or realElectricalHeating.active then
                virtualWaterHeating.switchOff().silent()
            else
                realWaterHeating.switchOn().afterSec(spikeProtectionTime)
            end
        
        -- if vWH -> Off then rWH -> Off
        elseif item == virtualWaterHeating then -- vWH switched Off
            realWaterHeating.switchOff()
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
tntteam
Posts: 4
Joined: Wednesday 13 November 2019 14:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Turn off a specific device when another specific one is enabled

Post by tntteam »

Thank you a lot :)

I never tried to do a dzVents script, but I m familiar with coding, so your example gives me a very good start.
  1. I think you missed a timer here :
    Line 62 : virtualWaterHeating.switchOn() -> virtualWaterHeating.switchOn().afterSec(spikeProtectionTime)
  2. Also, wouldn't it be better to avoid problem to use a different variable name for the "text" name of virtual switch and the virtual switch object itself ? I find it easier to read like that but maybe its a constraint in dzVents langage ?

    Like change this :
    local virtualElectricalHeating = 'vEH' -> local virtualElectricalHeatingLabel = 'vEH'

    And then change this :
    local virtualElectricalHeating = dz.devices(virtualElectricalHeating) -> local virtualElectricalHeating = dz.devices(virtualElectricalHeatingLabel)
  3. What does the .silent() only on virtual switches do? It's mandatory on virtual switches or it's just to not have this action into the log ?
  4. If I'm correct, I now have to set the domotics schedule on the virtualWaterHeating and not realWaterHeating
  5. One last question, would it be possible to retrieve the virtualWaterHeating schedule into the dzVents script or it must be hardcoded value ?
Thanks!
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Turn off a specific device when another specific one is enabled

Post by waaren »

When not yet familiar with dzVents please start with reading Get started Before implementing (~ 5 minutes). Special attention please for "In Domoticz go to Setup > Settings > Other and in the section EventSystem make sure the checkbox 'dzVents disabled' is not checked. Also make sure that in the Security section in the settings you allow 127.0.0.1 to not need a password. dzVents uses that port to send certain commands to Domoticz. Finally make sure you have set your current location in Setup > Settings > System > Location, otherwise there is no way to determine nighttime/daytime state."
tntteam wrote: Thursday 14 November 2019 12:20 I think you missed a timer here :
Line 62 : virtualWaterHeating.switchOn() -> virtualWaterHeating.switchOn().afterSec(spikeProtectionTime)
No. This is the virtual Switch. It will trigger the script again and the the virtual switch will look at the conditions and trigger the real switch afterSec(xx) if and when the conditions allow

Also, wouldn't it be better to avoid problem to use a different variable name for the "text" name of virtual switch and the virtual switch object itself ? I find it easier to read like that but maybe its a constraint in dzVents langage ?
I agree that it would be better practice and no; it's not a constraint in Dzvents or Lua
What does the .silent() only on virtual switches do? It's mandatory on virtual switches or it's just to not have this action into the log ?
option silent() prevents the state change to trigger an event. If you don't use it here the script will retrigger itself and here you don't want that.
If I'm correct, I now have to set the domotics schedule on the virtualWaterHeating and not realWaterHeating
Correct.
One last question, would it be possible to retrieve the virtualWaterHeating schedule into the dzVents script or it must be hardcoded value ?
It is possible but will complicate the script a lot. In my opinion it will introduce more potential points of faillure then hardcode the schedule. If you really hate hardcoded data I suggest you enter the schedule also as text in the description field of the switch or in a string type uservariable and get it from there.

If you insist to get it from the schedule data itself; you would need to use something like

local url = dz.settings['Domoticz url'] .. "/json.htm?type=timers&idx=" .. virtualWaterHeating.id
dz.openURL( { url = url, callBack = 'blabla'})

on my system it translates to

Code: Select all

http://192.168.192.109:8084/json.htm?idx=138&type=timers
with the result:

Code: Select all

{
"result": [
{
"Active": "true",
"Cmd": 0,
"Color": "",
"Date": "",
"Days": 128,
"Level": 100,
"MDay": 0,
"Month": 0,
"Occurence": 0,
"Randomness": "false",
"Time": "09:12",
"Type": 2,
"idx": "1"
},
{
"Active": "true",
"Cmd": 1,
"Color": "",
"Date": "",
"Days": 128,
"Level": 100,
"MDay": 0,
"Month": 0,
"Occurence": 0,
"Randomness": "false",
"Time": "09:15",
"Type": 2,
"idx": "2"
}
],
"status": "OK",
"title": "Timers"
}
For more info on this look at this chapter of the dzVents wiki
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
tntteam
Posts: 4
Joined: Wednesday 13 November 2019 14:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Turn off a specific device when another specific one is enabled

Post by tntteam »

Thank you a lot for your time and patience.

I will be looking into that this weekend. :)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest