DZvent don't refresh user variables Topic is solved

Please use template to report bugs and problems. Post here your questions when not sure where else to post
Only for bugs in the Domoticz application! other problems go in different subforums!

Moderators: leecollings, remb0

Forum rules
Before posting here, make sure you are on the latest Beta or Stable version.
If you have problems related to the web gui, clear your browser cache + appcache first.

Use the following template when posting here:

Version: xxxx
Platform: xxxx
Plugin/Hardware: xxxx
Description:
.....

If you are having problems with scripts/blockly, always post the script (in a spoiler or code tag) or screenshots of your blockly

If you are replying, please do not quote images/code from the first post

Please mark your topic as Solved when the problem is solved.
Post Reply
Arjan2019
Posts: 37
Joined: Tuesday 16 July 2019 12:47
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

DZvent don't refresh user variables

Post by Arjan2019 »

I'm trying to make a DZvent script which read the user variable Enever_Electricity_33 and Enever_Electricity_tomorrow_33
The goal is to trigger a switch which is active in the cheap hours and show a table white cheap hours and expensive hours.
It looks like this:
Image

The script is running every 1 min because it also need to set a switch is the cheap hours.
The script only refresh correct after a Domoticz restart without a restart it looks like DZvent is using cached values.
So without a restart of Domoticz my table stay the same for days.

I did make a test script to check which values are in memory.
I use copy Enever_Electricity_33 and Enever_Electricity_tomorrow_33 use user variable to stroom_prijs_morgen and stroom_prijs_vandaag.
Script:

Code: Select all

return {
    on = {
        timer = {
            'at 00:02',
            'at 17:50'
        }
    },

    execute = function(domoticz)

        local vandaag = domoticz.variables('Enever_Electricity_33').value
        local morgen  = domoticz.variables('Enever_Electricity_tomorrow_33').value

--        domoticz.variables('Stroom_prijs_vandaag').set(0)
--        domoticz.variables('Stroom_prijs_morgen').set(0)

        domoticz.variables('Stroom_prijs_vandaag').set(vandaag).afterSec(30)
        domoticz.variables('Stroom_prijs_morgen').set(morgen).afterSec(30)

    end
}
Result:
Image

It didn't copy the value from Enever_Electricity_33 to stroom_prijs_vandaag the string are different
So DZvent is using stored in memory string instead of the new string

How can I solve this DZvent need to read the new value every time so my tables stay correct
User avatar
gizmocuz
Posts: 3026
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: DZvent don't refresh user variables

Post by gizmocuz »

I looked into this and dzVents does not cache user variable values between executions. Each time a script runs,
Domoticz creates a completely fresh Lua environment, reads all user variables directly from memory, and discards
everything when done. There is no in-memory persistence between runs at the dzVents level.

Your script looks correct — domoticz.variables('Enever_Electricity_33').value will always return whatever value that
variable currently holds at the moment the script fires.

The likely culprit is the plugin or script that updates Enever_Electricity_33 and Enever_Electricity_tomorrow_33 (the
thing fetching prices from enever.nl). If that stops updating the variables — due to a network error, API timeout,
rate limit, or a silent crash — those variables stay frozen at the last value they had. Since Domoticz restarts tend
to re-initialize plugins and re-run startup scripts, the values appear correct right after a restart, giving the
impression that dzVents was the problem.

To confirm: go to Setup → More options → User variables and watch whether Enever_Electricity_33 changes over the
course of a day, completely independently of your dzVents script. If it doesn't change, the issue is in the enever.nl
integration, not in dzVents.

Also worth checking the Domoticz log for any errors from that plugin around the time the prices should have been
updated.
Quality outlives Quantity!
Arjan2019
Posts: 37
Joined: Tuesday 16 July 2019 12:47
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: DZvent don't refresh user variables

Post by Arjan2019 »

Did another test.
I did run this script

Code: Select all

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

    execute = function(domoticz)

        local vandaag = domoticz.variables('Enever_Electricity_33').value
        local morgen  = domoticz.variables('Enever_Electricity_tomorrow_33').value
        
        local vandaag1 = domoticz.variables('Stroom_prijs_vandaag').value
        local morgen1  = domoticz.variables('Stroom_prijs_morgen').value

        if vandaag ~= vandaag1
            then
             domoticz.log("Variabele vandaag zijn niet gelijk", domoticz.LOG_ERROR) 
         else
             domoticz.log("Variabele vandaag zijn gelijk", domoticz.LOG_ERROR)
        end
        
        if morgen ~= morgen1
            then
            domoticz.log("Variabele morgen zijn niet gelijk", domoticz.LOG_ERROR)
            else
            domoticz.log("Variabele morgen zijn gelijk", domoticz.LOG_ERROR)
        end  

    end
}
Logs:

Code: Select all

2026-04-12 13:24:01.077  Error: dzVents: Variabele vandaag zijn gelijk
2026-04-12 13:24:01.077  Error: dzVents: Variabele morgen zijn gelijk
2026-04-12 13:25:01.194  Error: dzVents: Variabele vandaag zijn gelijk
2026-04-12 13:25:01.195  Error: dzVents: Variabele morgen zijn gelijk
2026-04-12 13:26:01.186  Error: dzVents: Variabele vandaag zijn gelijk
2026-04-12 13:26:01.187  Error: dzVents: Variabele morgen zijn gelijk
2026-04-12 13:27:01.239  Error: dzVents: Variabele vandaag zijn gelijk
2026-04-12 13:27:01.240  Error: dzVents: Variabele morgen zijn gelijk
2026-04-12 13:28:01.285  Error: dzVents: Variabele vandaag zijn gelijk
2026-04-12 13:28:01.285  Error: dzVents: Variabele morgen zijn gelijk
2026-04-12 13:29:01.420  Error: dzVents: Variabele vandaag zijn niet gelijk
2026-04-12 13:29:01.421  Error: dzVents: Variabele morgen zijn niet gelijk
2026-04-12 13:30:01.520  Error: dzVents: Variabele vandaag zijn gelijk
2026-04-12 13:30:01.604  Error: dzVents: Variabele morgen zijn gelijk
2026-04-12 13:31:01.432  Error: dzVents: Variabele vandaag zijn gelijk
2026-04-12 13:31:01.433  Error: dzVents: Variabele morgen zijn gelijk
2026-04-12 13:32:01.466  Error: dzVents: Variabele vandaag zijn gelijk
2026-04-12 13:32:01.466  Error: dzVents: Variabele morgen zijn gelijk
At 13:29:01 I did set manual Stroom_prijs_vandaag to 0
Then I did run my first script to copy Enever_Electricity_33 to Stroom_prijs_vandaag
The log is telling that they are the same but they are not

Image
User avatar
gizmocuz
Posts: 3026
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: DZvent don't refresh user variables

Post by gizmocuz »

Root cause — This is a race condition, not a caching bug:

1. At the same timer tick (e.g. 00:02), both the Enever plugin and the dzVents copy script fire
simultaneously.
2. ExportDomoticzDataToLua() (in dzVents.cpp:1158) takes a shared lock on m_uservariablesMutex to
snapshot all variables into domoticzData.
3. UpdateUserVariable() (in EventSystem.cpp:1388) takes an exclusive lock to write the new Enever
value.
4. If dzVents wins the lock first, it snapshots the old value of Enever_Electricity_33 into
domoticzData — the copy script then copies the old price data to Stroom_prijs_vandaag.
5. The Enever plugin then wins the lock and writes the new value to both DB and m_uservariables.
6. Result: Enever_33 = new (DB + cache), Stroom = old (DB + cache) — UI shows mismatch.
7. BUT: the comparison script at 13:30 ran in the same domoticzData snapshot (same Lua execution),
where both were old value → reported "gelijk".

This is confirmed by the 13:30 test: comparison says "gelijk" (both old value in snapshot) while the
UI shows "niet gelijk" (Enever_33 was already updated to new value in the cache between snapshot time
and UI query time).

"Works after restart" is because after restart, GetCurrentUserVariables() reads the DB fresh, and the
Enever plugin has already run its first update at 00:02 the night before. The timestamps align
differently post-restart.

The fix is to change the dzVents copy script trigger from a time trigger to a user variable change
trigger:

Code: Select all

  return {
      on = {
          variables = { 'Enever_Electricity_33', 'Enever_Electricity_tomorrow_33' }
      },
      execute = function(domoticz, variable)
          -- now runs AFTER the Enever plugin has finished writing,
          -- guaranteed to see the new value
          local vandaag = domoticz.variables('Enever_Electricity_33').value
          local morgen  = domoticz.variables('Enever_Electricity_tomorrow_33').value
          domoticz.variables('Stroom_prijs_vandaag').set(vandaag)
          domoticz.variables('Stroom_prijs_morgen').set(morgen)
      end
  }
This way the copy script fires as a reaction to the variable change event, which is queued after
m_uservariables has been updated with the new value — guaranteed no race.
Quality outlives Quantity!
Arjan2019
Posts: 37
Joined: Tuesday 16 July 2019 12:47
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: DZvent don't refresh user variables

Post by Arjan2019 »

The script didn't trigger when the price changed at midnight.
When I selected the user variable Enever_Electricity_33 and clicked on update the script was triggered.
It looks like that the enever plugin use silent to write the variable.
Will check again at 15:00 when we get the new price for tomorrow.
User avatar
gizmocuz
Posts: 3026
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: DZvent don't refresh user variables

Post by gizmocuz »

You could be (are) right there!

The user variables where meant only for internal storage (API call limit), and not created to be used in scripting.

This has now been changed in beta 17726
Quality outlives Quantity!
Arjan2019
Posts: 37
Joined: Tuesday 16 July 2019 12:47
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: DZvent don't refresh user variables

Post by Arjan2019 »

Thanks alot
Did update to the last beta and will report it back after 15:00
Arjan2019
Posts: 37
Joined: Tuesday 16 July 2019 12:47
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: DZvent don't refresh user variables

Post by Arjan2019 »

All work fine now super Thanks :D
User avatar
gizmocuz
Posts: 3026
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: DZvent don't refresh user variables

Post by gizmocuz »

Great to hear, and thanks for the feedback!
Quality outlives Quantity!
Post Reply