Use value custom sensor as input alert sensor [Solved]

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

Moderator: leecollings

Post Reply
Jan Jansen
Posts: 229
Joined: Wednesday 30 April 2014 20:27
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

Use value custom sensor as input alert sensor [Solved]

Post by Jan Jansen »

I want to enter the value of a custom sensor (CO2) into an alert sensor.

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            'CO<sub>2</sub>-sensor',   -- custom sensor
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,  -- Change to domoticz.LOG_ERROR when script is OK
        marker = 'CO2-alert',
    },

    execute = function(dz, device)
        local co2 = dz.devices('CO<sub>2</sub>-sensor').sensorValue
        local co2alert = dz.devices('CO2')
        
        local function CO2_Index2Alert(value)                            
            local alert = dz.ALERTLEVEL_RED
            if      index < 350 then alert = dz.ALERTLEVEL_GREY
            elseif  index < 1000 then alert = dz.ALERTLEVEL_GREEN
            elseif  index < 1100 then alert = dz.ALERTLEVEL_YELLOW
            elseif  index < 1200 then alert = dz.ALERTLEVEL_ORANGE
            end
            return alert
        end

            CO2_AlertText = "CO<sub>2</sub>" .. co2 .. "ppm" .. 
            dz.devices(co2alert).updateAlertSensor(CO2_Index2Alert(co2), CO2_AlertText)
        end
    end
}
The Domoticz editor shows a red cross at line 34, a bracket would be missing. An error message in the log points in the same direction.
CO2.png
CO2.png (22.11 KiB) Viewed 2702 times
As far as I know, the number of brackets is correct. There is an error that I can't fix.
Thanks in advance!
Last edited by Jan Jansen on Wednesday 19 August 2020 18:57, edited 2 times in total.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Use value custom sensor as input alert sensor

Post by waaren »

Jan Jansen wrote: Friday 14 August 2020 14:40 I want to enter the value of a custom sensor (CO2) into an alert sensor.

As far as I know, the number of brackets is correct. There is an error that I can't fix.
Thanks in advance!
Probably this

'CO<sub>2</sub>-sensor', -- custom sensor

is not recognized as a device name can you try again after changing the name of the sensor without format statements ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Jan Jansen
Posts: 229
Joined: Wednesday 30 April 2014 20:27
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

Re: Use value custom sensor as input alert sensor

Post by Jan Jansen »

@ waaren,

Based on your suggestion I changed the script. It now looks like this:

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            'CO2-sensor',   -- custom sensor
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,  -- Change to domoticz.LOG_ERROR when script is OK
        marker = 'CO2-alert',
    },

    execute = function(dz, device)
        local co2 = dz.devices('CO2-sensor').sensorValue
        local co2alert = dz.devices('CO2')
        
        local function CO2_Index2Alert(value)                            
            local alert = dz.ALERTLEVEL_RED
            if      index < 350 then alert = dz.ALERTLEVEL_GREY
            elseif  index < 1000 then alert = dz.ALERTLEVEL_GREEN
            elseif  index < 1100 then alert = dz.ALERTLEVEL_YELLOW
            elseif  index < 1200 then alert = dz.ALERTLEVEL_ORANGE
            end
            return alert
        end

            CO2_AlertText = "CO2" .. co2 .. "ppm" .. 
            dz.devices(co2alert).updateAlertSensor(CO2_Index2Alert(co2), CO2_AlertText)
        end
    end
}
The red cross at line 34 has not disappeared and the log still shows the same error.

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

Re: Use value custom sensor as input alert sensor

Post by waaren »

Jan Jansen wrote: Friday 14 August 2020 20:02 Based on your suggestion I changed the script.
The red cross at line 34 has not disappeared and the log still shows the same error.
The red cross is because you have an additional end but still some other code errors were left in your version.

Can you try this and compare with your version to see what I changed?

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            'CO2-sensor',   -- custom sensor
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,  -- Change to domoticz.LOG_ERROR when script is OK
        marker = 'CO2-alert',
    },

    execute = function(dz, item)
        local co2 = item.sensorValue
        local co2alert = dz.devices('CO2')

        local function CO2_Index2Alert(ppm)
            local alert = dz.ALERTLEVEL_RED
            if      ppm < 350 then alert = dz.ALERTLEVEL_GREY
            elseif  ppm < 1000 then alert = dz.ALERTLEVEL_GREEN
            elseif  ppm < 1100 then alert = dz.ALERTLEVEL_YELLOW
            elseif  ppm < 1200 then alert = dz.ALERTLEVEL_ORANGE
            end
            return alert
        end

        CO2_AlertText = 'CO2: ' .. co2 .. ' ppm' 
        co2alert.updateAlertSensor(CO2_Index2Alert(co2), CO2_AlertText)
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Jan Jansen
Posts: 229
Joined: Wednesday 30 April 2014 20:27
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

Re: Use value custom sensor as input alert sensor

Post by Jan Jansen »

waaren wrote: Friday 14 August 2020 23:08
Can you try this and compare with your version to see what I changed?
@ waaren,

Thanks for pointing out my learning points ((item, ppm instead of value and co2alert instead dz.devices (co2alert)).
I tried the script you sent. The log shows:
CO2 alert.png
CO2 alert.png (21.41 KiB) Viewed 2658 times
This output was confusing to me but also provides a lot more clarity.

The hardware sensor is an ESPeasy with a MH-Z19. The MH-Z19 sends the CO2 value, the temperature and an U value. The Domoticz custom sensor only shows the ppm value. I only took the CO2 value into account. So first find out the output in a next script. I understand where the error message comes from.

I think the output of the sensor should be split. I've read that the stringSplit (string, [separator]) can be used for this, but I don't know how.

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

Re: Use value custom sensor as input alert sensor

Post by waaren »

Jan Jansen wrote: Saturday 15 August 2020 16:48 I think the output of the sensor should be split. I've read that the stringSplit (string, [separator]) can be used for this, but I don't know how.
I tested my version successfully with a numeric value in the custom Sensor.

Let's check first what the actual value in your system is.
Can you try replacing line 32

Code: Select all

        co2alert.updateAlertSensor(CO2_Index2Alert(co2), CO2_AlertText)
with these 2 lines

Code: Select all

        dz.log('CO2_AlertText: ' .. CO2_AlertText ,dz.LOG_DEBUG)
        co2alert.updateAlertSensor(CO2_Index2Alert(tonumber(co2)), CO2_AlertText)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Jan Jansen
Posts: 229
Joined: Wednesday 30 April 2014 20:27
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

Re: Use value custom sensor as input alert sensor

Post by Jan Jansen »

waaren wrote: Saturday 15 August 2020 17:48
Let's check first what the actual value in your system is.
Can you try replacing line 32
@waaren,

After the adjustment, the log shows:
CO2 alert.png
CO2 alert.png (23.1 KiB) Viewed 2626 times
Regards
Jan
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Use value custom sensor as input alert sensor

Post by waaren »

Van you please share your loglines as text within code tags. I have difficulties reading them as png files.
Thx


Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Jan Jansen
Posts: 229
Joined: Wednesday 30 April 2014 20:27
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

Re: Use value custom sensor as input alert sensor

Post by Jan Jansen »

Code: Select all

 2020-08-16 15:19:37.182 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-08-16 15:20:19.068 Status: dzVents: Info: Handling events for: "CO2-sensor", value: "911;29.0;10376"
2020-08-16 15:20:19.068 Status: dzVents: Info: CO2-alert: ------ Start internal script: CO2-alert: Device: "CO2-sensor (Sensoren)", Index: 58
2020-08-16 15:20:19.070 Status: dzVents: Debug: CO2-alert: Processing device-adapter for CO2: Alert sensor adapter
2020-08-16 15:20:19.070 Status: dzVents: Debug: CO2-alert: CO2_AlertText: CO2: 911;29.0;10376 ppm
2020-08-16 15:20:19.070 Status: dzVents: Info: CO2-alert: ------ Finished CO2-alert
2020-08-16 15:20:19.070 Error: dzVents: Error: (3.0.11) CO2-alert: An error occurred when calling event handler CO2-alert
2020-08-16 15:20:19.070 Error: dzVents: Error: (3.0.11) CO2-alert: ...domoticz/scripts/dzVents/generated_scripts/CO2-alert.lua:23: attempt to compare nil with number
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Use value custom sensor as input alert sensor

Post by waaren »

Jan Jansen wrote: Sunday 16 August 2020 15:26

Code: Select all

2020-08-16 15:20:19.070 Status: dzVents: Debug: CO2-alert: CO2_AlertText: CO2: 911;29.0;10376 ppm
It seems that your custom sensor also receives two other datapoints; temperature and barometer?
The rawData table contains all separate values of the sValue atrribute

Can you try this?

Code: Select all

return
{
    on =
    {
        devices =
        {
            'CO2-sensor',   -- custom sensor
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,  -- Change to domoticz.LOG_ERROR when script is OK
        marker = 'CO2-alert',
    },

    execute = function(dz, item)
        local co2alert = dz.devices('CO2')
        co2 = tonumber(item.rawData[1])

        local function CO2_Index2Alert(ppm)
            local alert = dz.ALERTLEVEL_RED
            if      ppm < 350 then alert = dz.ALERTLEVEL_GREY
            elseif  ppm < 1000 then alert = dz.ALERTLEVEL_GREEN
            elseif  ppm < 1100 then alert = dz.ALERTLEVEL_YELLOW
            elseif  ppm < 1200 then alert = dz.ALERTLEVEL_ORANGE
            end
            return alert
        end

        CO2_AlertText = 'CO2: ' .. co2 .. ' ppm'
        co2alert.updateAlertSensor(CO2_Index2Alert(co2), CO2_AlertText)
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Jan Jansen
Posts: 229
Joined: Wednesday 30 April 2014 20:27
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

Re: Use value custom sensor as input alert sensor

Post by Jan Jansen »

@ waaren,
Thank you so much for your time and attention! And YESS it works as desired.

Best regards,

Jan
kozzen
Posts: 13
Joined: Saturday 27 February 2021 7:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Use value custom sensor as input alert sensor [Solved]

Post by kozzen »

This sensor is this really a custom sensor ?

I'm getting the following error :

2021-03-28 15:47:11.684 Error: dzVents: Error: (3.1.6) Method updateAlertSensor is not available for device "Gassensor" (deviceType=General, deviceSubType=Custom Sensor). If you believe this is not correct, please report.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Use value custom sensor as input alert sensor [Solved]

Post by waaren »

kozzen wrote: Sunday 28 March 2021 15:48 This sensor is this really a custom sensor ?
The input device 'CO2-sensor' should be a custom sensor
The reporting device (in the script named 'CO2' ) is a type Alert.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
kozzen
Posts: 13
Joined: Saturday 27 February 2021 7:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Use value custom sensor as input alert sensor [Solved]

Post by kozzen »

Thanks for the information

it's working
WvdM78
Posts: 9
Joined: Wednesday 01 April 2020 14:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.1
Location: The Netherlands
Contact:

Re: Use value custom sensor as input alert sensor [Solved]

Post by WvdM78 »

It took me a while to find this thread. I have been struggling with exactly the same issue, that the MH-19B sensor broadcasts three values.

Thank you for the right direct with the rawdata[1] suggestion. Unfortunately I find it quite hard to find the right syntax to read a value with dzvents from a custom sensor.

For others, this is my night ventilation script based on measured CO2 levels in my sleeping room:

Code: Select all

return {
-- Ventilatie script CO2 in de nacht WvdM v1 01-2022
		on = {
        timer = {   "at 23:30-07:00 every 5 minutes"  },
  		},

   execute = function(domoticz, device, triggerInfo)
        local MV_half         = domoticz.devices('Ventilator halftoeren')
        local MV_high         = domoticz.devices('Ventilator hoogtoeren')
        local PM10              = domoticz.devices('PM10')
        local CO2               = domoticz.devices('CO2')
        local PM10_value
        local CO2_value
        
    
    PM10_value = tonumber(PM10.state)
    CO2_value = tonumber(CO2.rawData[1])

VentState = 0
    domoticz.log('DzVents: PM10-value: ' .. PM10_value)
--    domoticz.log('DzVents: CO2-value: ' .. CO2_value)

-- control based on typical values
if CO2_value > 800 and PM10_value < 40 then
    
    if MV_high.state == 'On' or MV_half.state == 'On' then
    domoticz.log('DzVents: Ventilator no change')
   
    else
    MV_half.switchOn()
    domoticz.log('DzVents: Ventilator half power on')

    end
end


if CO2_value <= 750 then
    
    if MV_high.state == 'On' then
    MV_high.switchOff()
    elseif MV_half.state == 'On' then
    MV_half.switchOff()
    end
end

-- control based on more extreme values

if CO2_value > 1100 and PM10_value < 60 and (MV_high.state == 'Off' or MV_half.state == 'Off') then
    MV_half.switchOn()
end

if CO2_value <= 850 and PM10_value > 40 then
    
    if MV_high.state == 'On' then
    MV_high.switchOff()
    elseif MV_half.state == 'On' then
    MV_half.switchOff()
    end
end

end

}
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest