Need help with groovy to tweak InfluxDB Logger

Hi, all.

I wish to change the code to log motion activity as a boolean instead of a string. The reason being is that the database I use for all device data already has lots of motion activity and I really don't want to make a parallel database.

Here is an example of what is in my database now

> select * from motion where time  > now()-1h
name: motion
time                           deviceId deviceName           groupId groupName hubId hubName locationId locationName type unit value
----                           -------- ----------           ------- --------- ----- ------- ---------- ------------ ---- ---- -----
2022-09-21T23:48:32.862585057Z          Driveway Motion                                                              bool      false

HE logs show this error

postToInfluxDB(): Something went wrong! Response from InfluxDB: Status: 400, Headers: [X-Influxdb-Error:partial write: field type conflict: input field "value" on measurement "motion" is type string, already exists as type boolean 

I'm almost certain this is all that needs to be changed but I'm not a groovy/HE programmer. This is the InfluxDB Logger app code at line 450

    else if ('motion' == evt.name) { // Motion: Calculate a binary value (active = 1, inactive = 0)
        unit = 'motion'
        value = '"' + value + '"'
        valueBinary = ('active' == evt.value) ? '1i' : '0i'
        data += ",unit=${unit} value=${value},valueBinary=${valueBinary}"
    }

Bonus points if you can also tell me how to change the instance name so I can easily see which is which like we can with Maker API

image

Thanks for reading this.

More of a workaround than a solution.... Could you maintain a virtual switch in HE in line with the motion sensor, e.g. when motion is detected the switch is on, else off? Not ideal... I'm sure someone will be able to assist with the code....

Also, is this not a custom app? If so, I think the post would belong under the developers category....

1 Like

Thanks

Nobody?

What's the driver for the device causing the error?

Generic Zigbee Motion Sensor

Hmmm... That makes it a little hard.... :slight_smile: ie. am assuming that is a built-in driver... Now I realise the issue, given I got you to move this thread....

1 Like

Yep. But I don't see how the driver matters because it's only the write to InfluxDB that's having an issue. Maybe I wasn't clear but I think all that needs to be done is to change the type of the motion attribute from string to bool in the write.

I am expecting the logger is setup to subscribe / listen for events on devices you select. Potentially in this case the device is recording (or not recording) a different unit for motion, hence the error about a string verses a boolean.

What do you see if you open the Events screen for the device? Where there are events for motion, what are the units. That said, I see my motion sensors do not record a unit... Anyway, would be interesting to see what is recorded.

Guessing here based on casual Googling, but if you change this line to something like this, does it work?

valueBinary = ('active' == evt.value) ? 'true' : 'false'

I have no familiarity with InfluxDB or whatever API or other mechanism of access this app might be using, so this may not work.

Some apps offer this; if not, you can add a line like this to the UI, maybe before the first input() (it's pretty similar in that it needs to reside in a section):

label title: "App name:"
1 Like

Would be odd for that to not have been an issue for others...

Throwing this out there, is Influx db column expecting a bool or string?

I'll try it but I suspect not because 'true' : 'false' are also strings. Maybe true : false instead?

I thought it might be something simple :grin: Thanks! :+1:

bool

You could try that too; again, I don't know how how either InfluxDB or the app you're using works. The other consideration would be what type, if any, was specified for valueBinary, which is not apparent from the snippet.

binary would indicate it is expecting just that, a binary value, but like you said, it is not clear... I still feel like the issue is more likely fiurther back in the device driver, given others must be using the logger successfully.

Line 397:   def valueBinary = ''

So it seems I need to add a boolean variable or just change the last line

data += ",unit=${unit} value=${value},valueBinary=${valueBinary}"

to specify bool directly.

Maybe I should include a link to the source code?! :roll_eyes:

My gut-feel would be to setup a virtual sensor and test that with the logger. If that works then I would look at what is different about the logging of events for the device you are interested in compared to the test virtual motion sensor, looking at the Events page for both when recording motion activation, in particular the Unit.

Download the Hubitat app