SHAC email function -

Discussion in 'C-Bus Automation Controllers' started by philthedill, May 10, 2020.

  1. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    22
    Likes Received:
    0
    Location:
    Melbourne
    I have my SHAC generated email working as a straight bit of text. Where do I find how to format things to include some variables from my system? (I am wanting to export my power consumption data each month via email and have the data as a CBus measurement)

    This is the first time I have asked for help as it has (until now) been solved by someone before me!
     
    philthedill, May 10, 2020
    #1
    1. Advertisements

  2. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    22
    Likes Received:
    0
    Location:
    Melbourne
    I've made some progress and now my request is far more specific. This is an extract of the message part of an email generated by the SHAC and work fine but I have 2 asks. The first is to have each meter reading on a new line (the currently appear as a single sentence. The second is to get more detail on the quantities (they currently appear as integers and I want 1 decimal place).

    "
    subject = 'Electricity Meter Readings'
    message = 'Hi, Here are the most recent meter readings from your house:'

    ..string.format('grid peak ') ..string.format('%d', pcumgrid) ..string.format(' kWh; ')
    ..string.format("grid off-peak ") ..string.format("%d", opcumgrid) ..string.format(" kWh; ")
    ..string.format("total usage peak ") ..string.format("%d", pcumelec) ..string.format(" kWh; ")
    ..string.format("total usage off-peak ") ..string.format("%d", opcumelec) ..string.format(" kWh; ")
    ..string.format("hotwater peak ") ..string.format("%d", pcumhotwater) ..string.format(" kWh; ")"
     
    philthedill, May 15, 2020
    #2
    1. Advertisements

  3. philthedill

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,060
    Likes Received:
    92
    Location:
    Adelaide, Australia
    To add a line break in a string just include the \n sequence

    To format decimals you need to use the floating point formatter f, not d

    e.g. %.1f will give you one decimal point.
     
    Ashley, May 15, 2020
    #3
  4. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    22
    Likes Received:
    0
    Location:
    Melbourne
    thanks Ashley - formatting decimals worked a treat! However, would you mind showing exactly where to drop the \n sequence into my script? I've tried everything I can think of without success and I know the answer will be something simple.
    cheers
     
    philthedill, May 15, 2020
    #4
  5. philthedill

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,060
    Likes Received:
    92
    Location:
    Adelaide, Australia
    You're right. \n doesn't work in mail messages.

    You have to use the \012 instead of \n

    This is a mime requirement as \n means different things in different systems

    e.g 'Line 1\012Line 2'
     
    Ashley, May 16, 2020
    #5
  6. philthedill

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,060
    Likes Received:
    92
    Location:
    Adelaide, Australia
    You can also simplify all your format statements.

    e.g.

    ..string.format('grid peak ') ..string.format('%d', pcumgrid) ..string.format(' kWh; ')

    can become

    ..string.format('grid peak %.1f kWh ', pcumgrid ) etc

    You can also use any number of % specifiers in one format statement. Just include all the values in a list

    e.g.

    string.format('Value 1 %d, value 2 %d, value 3 %d', 10, 20, 30) etc

    or

    string.format('Value 1 %d\012 value 2 %d\012 value 3 %d', 10, 20, 30) to put then on seperate lines
     
    Ashley, May 16, 2020
    #6
  7. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    22
    Likes Received:
    0
    Location:
    Melbourne
    thanks for all that - I have tried the \012 and now get a bold up arrow in the spot where the \012 has been inserted??
     
    philthedill, May 16, 2020
    #7
  8. philthedill

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,060
    Likes Received:
    92
    Location:
    Adelaide, Australia
    Post the exact code you are using.
     
    Ashley, May 16, 2020
    #8
  9. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    22
    Likes Received:
    0
    Location:
    Melbourne
    I've only added the \012 to the first line

    "subject = 'Electricity Meter Readings'
    message = 'Here are the most recent meter readings from your house:'

    ..string.format('grid peak \012 ') ..string.format('%.2f', pcumgrid) ..string.format(' kWh; ')
    ..string.format("grid off-peak ") ..string.format("%.2f", opcumgrid) ..string.format(" kWh; ")
    ..string.format("total usage peak ") ..string.format("%.2f", pcumelec) ..string.format(" kWh; ")
    ..string.format("total usage off-peak ") ..string.format("%.2f", opcumelec) ..string.format(" kWh; ")"
     
    philthedill, May 16, 2020
    #9
  10. philthedill

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,060
    Likes Received:
    92
    Location:
    Adelaide, Australia
    I think what is happening is the string.format is converting the \012 into a single ascii new line character that the email mime encoding won't recognise. Try moving the \012 outside of the string.format

    i.e.

    ..string.format('grid peak ') ..'\012'..string.format('%.2f', pcumgrid) ..string.format(' kWh; ')

    Of course you don't need the string.format for simple strings.

    So

    .. 'grid peak\012'..string.format('%.2f', pcumgrid) ..'kWh;'
     
    Ashley, May 16, 2020
    #10
  11. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    22
    Likes Received:
    0
    Location:
    Melbourne
    thanks again Ashley, still no luck with the new line however the "short" version worked fine. back to the drawing board!. btw I cut and pasted your text directly into my message so I have it as you intended. I tried both the \n and the \012
     
    philthedill, May 16, 2020
    #11
  12. philthedill

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,060
    Likes Received:
    92
    Location:
    Adelaide, Australia
    I just tried this and it worked fine for me:

    string.format('grid peak \012%.2fkWh', pcumgrid)

    I set pcumgrid to 123.456 and received

    grid peak
    123.46kWh
     
    Ashley, May 16, 2020
    #12
  13. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    22
    Likes Received:
    0
    Location:
    Melbourne
    i copied that exactly and got the up arrow again and no new line. I wonder if it is a quirk with gmail?
     
    philthedill, May 16, 2020
    #13
  14. philthedill

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,060
    Likes Received:
    92
    Location:
    Adelaide, Australia
    I just tried it via smtp.gmail.com and it worked fine.

    Post your mail function (but hide the password :))
     
    Ashley, May 16, 2020
    #14
  15. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    22
    Likes Received:
    0
    Location:
    Melbourne
    here's the mail function from the user function library - anything look screwy?

    -- user function library

    -- send an e-mail
    function mail(to, subject, message)
    -- make sure these settings are correct
    local settings = {
    -- "from" field, only e-mail must be specified here
    from = '[email protected]',
    -- smtp username
    user = '[email protected]',
    -- smtp password
    password = 'xyz123',
    -- smtp server
    server = 'smtp.gmail.com',
    -- smtp server port
    port = 465,
    -- enable ssl, required for gmail smtp
    secure = 'sslv23',
    }
     
    philthedill, May 16, 2020
    #15
  16. philthedill

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    70
    Likes Received:
    12
    Location:
    New Zealand
    Have you set up secure apps password in gmail, I had to do that to get Shac to send an email,
     
    Pie Boy, May 17, 2020
    #16
  17. philthedill

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,060
    Likes Received:
    92
    Location:
    Adelaide, Australia
    Yes. I created a secure app password to test gmail in the SHAC. Works fine for me.

    You only posted half of the email function you are using.
     
    Ashley, May 18, 2020
    #17
  18. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    22
    Likes Received:
    0
    Location:
    Melbourne
    sorry - I thought that was all - here is the complete function from the "common functions" scripting tab

    -- send an e-mail
    function mail(to, subject, message)
    -- make sure these settings are correct
    local settings = {
    -- "from" field, only e-mail must be specified here
    from = '[email protected]',
    -- smtp username
    user = '[email protected]',
    -- smtp password
    password = '1234554321',
    -- smtp server
    server = 'smtp.gmail.com',
    -- smtp server port
    port = 465,
    -- enable ssl, required for gmail smtp
    secure = 'sslv23',
    }

    local smtp = require('socket.smtp')

    if type(to) ~= 'table' then
    to = { to }
    end

    for index, email in ipairs(to) do
    to[ index ] = '<' .. tostring(email) .. '>'
    end

    -- message headers and body
    settings.source = smtp.message({
    headers = {
    to = table.concat(to, ', '),
    subject = subject,
    ['Content-type'] = 'text/html; charset=utf-8',
    },
    body = message
    })

    -- fixup from field
    settings.from = '<' .. tostring(settings.from) .. '>'
    settings.rcpt = to

    return smtp.send(settings)
    end
     
    philthedill, May 18, 2020
    #18
  19. philthedill

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,060
    Likes Received:
    92
    Location:
    Adelaide, Australia
    Can't see any problems there.

    Post the whole script you are using and I'll try it from here.

    If you give me your e-mail address I can try sending mine that works to you, and we can figure out which end is the problem.
     
    Ashley, May 21, 2020
    #19
  20. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    22
    Likes Received:
    0
    Location:
    Melbourne
    just to confirm, the email and information is being sent and received fine. It is the formatting of the email that is the problem. I am only want to add a new line for each meter reading. my email is [email protected]
     
    philthedill, May 22, 2020
    #20
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.