Xiaomi sensor:attempt to index a nil value (field 'lastUpdate')

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

Moderator: leecollings

Post Reply
RezaRose
Posts: 65
Joined: Sunday 09 February 2020 11:34
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Holland
Contact:

Xiaomi sensor:attempt to index a nil value (field 'lastUpdate')

Post by RezaRose »

I made a script to turn the lights off after the lastUpdate more then xx
I have used this script succesfully with the Philips hue sensor, but is i use the Xioami sensor it gives a error on the last update:

Code: Select all

attempt to index a nil value (field 'lastUpdate')

Code: Select all

return {
       on = {
          timer = {'every 1 minutes'},
          devices = {'Schuursensor'}
       },
      	execute = function(dz, Sensor)
      	
      
      	local Schuur = dz.devices('Schuur')
      	
      	if Sensor.state == 'On' then
      	    
      	    Schuur.switchOn().checkFirst()
      	    
      	    print('Schuursensor is ' .. Sensor.state .. '. Schuur is aangezet omdat er beweging in de Schuur is.  ')
      	    
	elseif Sensor.lastUpdate.minutesAgo >= 5  and Schuur.state == 'On'  then
		    
                Schuur.switchOff()
                print('Schuurverlichting is uitgezet omdat er 5 minuten geen beweging in de Schuur is geweest')
                    
        else
                 
                 print('Nothing changed. Schuursensor updated ' .. Sensor.lastUpdate.minutesAgo .. ' minutes ago')
	    end
    end
    }
When i look in Domoticz i can see the last update status was 31 minutes ago, so it is getting a value somewhere.

I tried to debug with Sensor.dump() but it gives a error with nil value on dump too. Why it is giving a nil value and can i do something about it?
Raspberry Pi 4 - Raspbian bullseye - Python 3.9 - stable
Dashticz on apache2 - Mqqt - node red
Zigate - USB Tll - Philips hue - Ikea - Innr - Xiaomi - TPlink - Nest -v3
jmleglise
Posts: 192
Joined: Monday 12 January 2015 23:27
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: FRANCE
Contact:

Re: Xiaomi sensor:attempt to index a nil value (field 'lastUpdate')

Post by jmleglise »

=> devices = {'Schuursensor'}
=> local Schuur = dz.devices('Schuur')

is it the same device ? So what is the right name ?
My script : https://github.com/jmleglise
RFXTRX433E: Blind Somfy RTS, Portal Somfy Evolvia, chacon IO, Oregon, PIR sensor PT2262
My Last project : Location de vacances a Ouistreham vue mer
KMTronic USB relay
Chinese Z-WAVE: Neo CoolCam
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Xiaomi sensor:attempt to index a nil value (field 'lastUpdate')

Post by waaren »

RezaRose wrote: Wednesday 03 February 2021 13:27 I made a script to turn the lights off after the lastUpdate more then xx
I have used this script succesfully with the Philips hue sensor, but is i use the Xioami sensor it gives a error on the last update:
Your script can be triggered by the timer or by the device but your code only take the device triggered version into account.
You should do something like below. (check the script logic around if item.isDevice)

Code: Select all

return
{
    on =
    {
        timer =
        {
            'every minute',
        },

        devices =
        {
            'Schuursensor',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'schuur logic',
    },

    execute = function(dz, item)

        local Schuur = dz.devices('Schuur')
        local sensor = dz.devices('Schuursensor')

        if item.isDevice then
            if sensor.state == 'On' then
                Schuur.switchOn().checkFirst()
                dz.log('Schuursensor is ' .. sensor.state .. '. Schuur is aangezet omdat er beweging in de Schuur is.  ', dz.LOG_DEBUG)
            else
                dz.log('Schuursensor switched to ' .. sensor.state , dz.LOG_DEBUG)
            end

        elseif sensor.lastUpdate.minutesAgo >= 5  and Schuur.state == 'On'  then

            Schuur.switchOff()
            dz.log('Schuurverlichting is uitgezet omdat er 5 minuten geen beweging in de Schuur is geweest', dz.LOG_DEBUG)
        else
             dz.log('Nothing changed. Schuursensor updated ' .. sensor.lastUpdate.minutesAgo .. ' minutes ago', dz.LOG_DEBUG)
        end
    end
}


Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
RezaRose
Posts: 65
Joined: Sunday 09 February 2020 11:34
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Holland
Contact:

Re: Xiaomi sensor:attempt to index a nil value (field 'lastUpdate')

Post by RezaRose »

waaren wrote: Wednesday 03 February 2021 13:54
RezaRose wrote: Wednesday 03 February 2021 13:27 I made a script to turn the lights off after the lastUpdate more then xx
I have used this script succesfully with the Philips hue sensor, but is i use the Xioami sensor it gives a error on the last update:
Your script can be triggered by the timer or by the device but your code only take the device triggered version into account.
You should do something like below. (check the script logic around if item.isDevice)

Code: Select all

return
{
    on =
    {
        timer =
        {
            'every minute',
        },

        devices =
        {
            'Schuursensor',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'schuur logic',
    },

    execute = function(dz, item)

        local Schuur = dz.devices('Schuur')
        local sensor = dz.devices('Schuursensor')

        if item.isDevice then
            if sensor.state == 'On' then
                Schuur.switchOn().checkFirst()
                dz.log('Schuursensor is ' .. sensor.state .. '. Schuur is aangezet omdat er beweging in de Schuur is.  ', dz.LOG_DEBUG)
            else
                dz.log('Schuursensor switched to ' .. sensor.state , dz.LOG_DEBUG)
            end

        elseif sensor.lastUpdate.minutesAgo >= 5  and Schuur.state == 'On'  then

            Schuur.switchOff()
            dz.log('Schuurverlichting is uitgezet omdat er 5 minuten geen beweging in de Schuur is geweest', dz.LOG_DEBUG)
        else
             dz.log('Nothing changed. Schuursensor updated ' .. sensor.lastUpdate.minutesAgo .. ' minutes ago', dz.LOG_DEBUG)
        end
    end
}


Thanks I understand what it does, I put it in the script! But is does not fix the error

Code: Select all


return {
       on = {
          timer = {'every 1 minutes'},
          devices = {'Schuursensor'}
       },
      	execute = function(dz, Sensor)
      	
      
      	local Schuur = dz.devices('Schuur')
      	
      	if Sensor.isDevice then
      	if Sensor.state == 'On' then
      	    
      	    Schuur.switchOn().checkFirst()
      	    
      	    print('Schuursensor is ' .. Sensor.state .. '.. Schuur is aangezet omdat er beweging in de Schuur is.  ')
      	    
  	    end
  	    
	elseif Sensor.lastUpdate.minutesAgo >= 5  and Schuur.state == 'On'  then
		    
                Schuur.switchOff()
                print('Schuurverlichting is uitgezet omdat er 5 minuten geen beweging in de Schuur is geweest')
                    
        else
                 
                 print('Nothing changed. Schuursensor updated ' .. Sensor.lastUpdate.minutesAgo .. ' minutes ago')
	    end
    end
    }

Code: Select all

2021-02-03 14:07:00.330 Error: dzVents: Error: (3.1.4) An error occurred when calling event handler Schuursensor
2021-02-03 14:07:00.330 Error: dzVents: Error: (3.1.4) ...oticz/scripts/dzVents/generated_scripts/Schuursensor.lua:22: attempt to index a nil value (field 'lastUpdate')
Raspberry Pi 4 - Raspbian bullseye - Python 3.9 - stable
Dashticz on apache2 - Mqqt - node red
Zigate - USB Tll - Philips hue - Ikea - Innr - Xiaomi - TPlink - Nest -v3
RezaRose
Posts: 65
Joined: Sunday 09 February 2020 11:34
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Holland
Contact:

Re: Xiaomi sensor:attempt to index a nil value (field 'lastUpdate')

Post by RezaRose »

jmleglise wrote: Wednesday 03 February 2021 13:35 => devices = {'Schuursensor'}
=> local Schuur = dz.devices('Schuur')

is it the same device ? So what is the right name ?
Device 'Schuursensor' is renamed to sensor and it is the motiondetector.
Device Schuur is the lightbulb switch
Raspberry Pi 4 - Raspbian bullseye - Python 3.9 - stable
Dashticz on apache2 - Mqqt - node red
Zigate - USB Tll - Philips hue - Ikea - Innr - Xiaomi - TPlink - Nest -v3
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Xiaomi sensor:attempt to index a nil value (field 'lastUpdate')

Post by waaren »

RezaRose wrote: Wednesday 03 February 2021 14:09

Code: Select all

2021-02-03 14:07:00.330 Error: dzVents: Error: (3.1.4) An error occurred when calling event handler Schuursensor
2021-02-03 14:07:00.330 Error: dzVents: Error: (3.1.4) ...oticz/scripts/dzVents/generated_scripts/Schuursensor.lua:22: attempt to index a nil value (field 'lastUpdate')
You are not using the script I posted and merged it partially (and wrong) in yours. Please try the script I posted first in an unmodified form and when you see it works amend where needed.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
RezaRose
Posts: 65
Joined: Sunday 09 February 2020 11:34
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Holland
Contact:

Re: Xiaomi sensor:attempt to index a nil value (field 'lastUpdate')

Post by RezaRose »

I'm sorry, i thought did understand :roll:

Code: Select all

2021-02-03 14:21:00.604 Status: dzVents: Info: schuur logic: ------ Start internal script: Script #2:, trigger: "every minute"
2021-02-03 14:21:00.629 Status: dzVents: Debug: schuur logic: Processing device-adapter for Schuur: RGB(W) device adapter
2021-02-03 14:21:00.630 Status: dzVents: Debug: schuur logic: Processing device-adapter for Schuur: Switch device adapter
2021-02-03 14:21:00.631 Status: dzVents: Debug: schuur logic: Processing device-adapter for Schuursensor: Switch device adapter
2021-02-03 14:21:00.631 Status: dzVents: Debug: schuur logic: Nothing changed. Schuursensor updated 3 minutes ago
2021-02-03 14:21:00.631 Status: dzVents: Info: schuur logic: ------ Finished Script #2
It worked! I will take a minute to figure out what is the difference.
Raspberry Pi 4 - Raspbian bullseye - Python 3.9 - stable
Dashticz on apache2 - Mqqt - node red
Zigate - USB Tll - Philips hue - Ikea - Innr - Xiaomi - TPlink - Nest -v3
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Xiaomi sensor:attempt to index a nil value (field 'lastUpdate')

Post by waaren »

RezaRose wrote: Wednesday 03 February 2021 14:22 I'm sorry, i thought did understand
It worked! I will take a minute to figure out what is the difference.
No problem. The thing most people starting with dzVents find hard to get there minds to, are the first two parameters of the execute function.
The execute function is the "motor" of the script and to do something useful it needs at least one and most of the time two parameters.

A detailed description on how that works can be found in this chapter of the dzVents wiki

some special attention for the line:

Since you can define multiple on-triggers in your script, it is not always clear what the type is of this second parameter. In your code you need to know this in order to properly respond to the different events. To help you inspect the object you can use these attributes like if (item.isDevice) then ... end:
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
RezaRose
Posts: 65
Joined: Sunday 09 February 2020 11:34
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Holland
Contact:

Re: Xiaomi sensor:attempt to index a nil value (field 'lastUpdate')

Post by RezaRose »

Thanks! I merged it right now! ( i will look into the difference between dz.log and print later)

I thought i found in the video somewhere i could rename the function. But i did not used it properly because i have two triggers. I will stay with the variables untill i understand it better. Thank you for the help!

Code: Select all

return {
       on = {
          timer = {'every 1 minutes'},
          devices = {'Schuursensor'}
       },
      	execute = function(dz, item)
      	
        local Sensor = dz.devices('Schuursensor')      
      	local Schuur = dz.devices('Schuur')
      	
      	if item.isDevice then
      	    
      	    if  Sensor.state == 'On' then
      	        Schuur.switchOn().checkFirst()
      	        print('Schuursensor is ' .. Sensor.state .. '.. Schuur is aangezet omdat er beweging in de Schuur is.  ')
      	    end
  	    
	    elseif Sensor.lastUpdate.minutesAgo >= 5  and Schuur.state == 'On'  then
		    
                Schuur.switchOff()
                print('Schuurverlichting is uitgezet omdat er 5 minuten geen beweging in de Schuur is geweest')
                    
        else
                 
                 print('Nothing changed. Schuursensor updated ' .. Sensor.lastUpdate.minutesAgo .. ' minutes ago')
	    end
    end
    }
Raspberry Pi 4 - Raspbian bullseye - Python 3.9 - stable
Dashticz on apache2 - Mqqt - node red
Zigate - USB Tll - Philips hue - Ikea - Innr - Xiaomi - TPlink - Nest -v3
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests