Page 1 of 1

Use value custom sensor as input alert sensor [Solved]

Posted: Friday 14 August 2020 14:40
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 2940 times
As far as I know, the number of brackets is correct. There is an error that I can't fix.
Thanks in advance!

Re: Use value custom sensor as input alert sensor

Posted: Friday 14 August 2020 17:09
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 ?

Re: Use value custom sensor as input alert sensor

Posted: Friday 14 August 2020 20:02
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

Re: Use value custom sensor as input alert sensor

Posted: Friday 14 August 2020 23:08
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
}

Re: Use value custom sensor as input alert sensor

Posted: Saturday 15 August 2020 16:48
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 2896 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

Re: Use value custom sensor as input alert sensor

Posted: Saturday 15 August 2020 17:48
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)

Re: Use value custom sensor as input alert sensor

Posted: Sunday 16 August 2020 11:57
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 2864 times
Regards
Jan

Re: Use value custom sensor as input alert sensor

Posted: Sunday 16 August 2020 15:00
by waaren
Van you please share your loglines as text within code tags. I have difficulties reading them as png files.
Thx



Re: Use value custom sensor as input alert sensor

Posted: Sunday 16 August 2020 15:26
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

Re: Use value custom sensor as input alert sensor

Posted: Sunday 16 August 2020 17:48
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
}

Re: Use value custom sensor as input alert sensor

Posted: Sunday 16 August 2020 19:14
by Jan Jansen
@ waaren,
Thank you so much for your time and attention! And YESS it works as desired.

Best regards,

Jan

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

Posted: Sunday 28 March 2021 15:48
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.

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

Posted: Sunday 28 March 2021 16:21
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.

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

Posted: Sunday 28 March 2021 16:40
by kozzen
Thanks for the information

it's working

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

Posted: Tuesday 18 January 2022 16:55
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

}