Logic Machine Forum
garbage can trigger - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: garbage can trigger (/showthread.php?tid=3794)



garbage can trigger - pioneersteffen - 05.01.2022

Hi @all,

I want to create a script that gives me a trigger object when a garbage can will be picked up today. Therefore I have all possible pick up dates available in an .ics calendar or I can easiliy convert it to an Excel.

Is there any script / starting point you can help me with?

Many thanks for your help.

Best Regards
Steffen


RE: garbage can trigger - admin - 14.01.2022

ICS is a text format so it can be parsed via a script. Can you provide an example ICS that you want to use? Is it stored somewhere where LM can retrieve it automatically?


RE: garbage can trigger - pioneersteffen - 14.01.2022

Hi Admin

many thanks for your reply. 

Attached you can find the example file. I want to check if the actual date is similar to a pick up date in the ics and if yes a group address with the kind of garbage ("Restmüll", "Gelber Sack", etc.) should be send. 

The ics I getting from a website as a download and is not stored anywhere.

Many thanks for your help!

Best Regards
Steffen


RE: garbage can trigger - admin - 17.01.2022

Here's an example that you can use a starting point. data variable should contain the ICS file contents that you get externally.

First the script parses ICS events into a table. Then it looks for an event with a matching date. summary variable is the summary field (kind of garbage in your case) of the found event or an empty string. Change 1/1/1 as needed and use 250 byte string as the data type of this object.

Code:
url = 'https://p35-caldav.icloud.com/published/2/MTQzMzA1NjcxOTE0MzMwNXFFkpAlN-RmUw02iYPAHdjXli1qufT3m2kVvFAFS2VXJLUhJ6r02NrhV15B8p_WoReJ0k5wrU4I1th5F6eq8UI'
data, err = require('socket.http').request(url)

if not data then
  log('request failed', err)
  return
end

data = data:split('\n')

events = {}

for _, line in ipairs(data) do
  line = line:trim()

  if line == 'BEGIN:VEVENT' then
    event = {}
  elseif line == 'END:VEVENT' then
    events[ #events + 1 ] = event
    event = nil
  elseif event then
    pos = line:find(':')
    if pos then
      key = line:sub(1, pos - 1)
      value = line:sub(pos + 1)

      event[ key ] = value
    end
  end
end

now = os.date('%Y%m%d')
summary = ''

for _, event in ipairs(events) do
  if event['DTSTART;VALUE=DATE'] == now then
    summary = event['SUMMARY']
    break
  end
end

grp.checkupdate('1/1/1', summary)



RE: garbage can trigger - pioneersteffen - 17.01.2022

Hi Admin,

many thanks for your help! Highly appreciated!

Two questions:

The content of the variable data. Do I need just to replace „\n“ with the copy and paste text string out of the ics opened by a text editor? I tried to and getting failure messages. Or do you have a recommendation how to convert it?

Would it be also possible to parse it directly from a webcal address?

Many thanks for your help!

Best Regards
Steffen


RE: garbage can trigger - admin - 18.01.2022

What is the URL to get the ICS file?


RE: garbage can trigger - pioneersteffen - 18.01.2022

Here is a test url:

webcal://p35-caldav.icloud.com/published/2/MTQzMzA1NjcxOTE0MzMwNXFFkpAlN-RmUw02iYPAHdjXli1qufT3m2kVvFAFS2VXJLUhJ6r02NrhV15B8p_WoReJ0k5wrU4I1th5F6eq8UI

Many thanks for your help!


RE: garbage can trigger - admin - 18.01.2022

I've modified my original example to fetch ICS from the URL.


RE: garbage can trigger - pioneersteffen - 18.01.2022

Awesome! It works! Many thanks for all of your help!


RE: garbage can trigger - pioneersteffen - 18.01.2022

@admin: If I want to get a message today for a planned pickup tomorrow, how is the modification look like? Sorry for the rookie question I‘ve tried to find a solution via search function without any success.

Many thanks for your help!


RE: garbage can trigger - admin - 18.01.2022

Replace line 32 with this:
Code:
now = os.date('%Y%m%d', os.time() + 86400)



RE: garbage can trigger - pioneersteffen - 18.01.2022

Awesome! Many thanks again! :-)