in my python lambda code that scans dynamodb based on primary key as 'deviceId' & sortkey timestamp( which is in YYYY-MM-DD HH:MN:SS format) , i need to scan every for last 15min data (from time now). I am in Mumbai region (ap-south-1), how do I set local timezone in my following lambda code. since it is picking default utc datetime.The lambda ideally has to scan & if return count >= 10 then thermostat+5 .
import boto3
import math
import json
import time
from datetime import datetime,timedelta
from dateutil.tz import tzlocal
from boto3.dynamodb.conditions import Key, Attr
client = boto3.client('dynamodb')
dynamodb = boto3.resource('dynamodb')
def lambda_handler(event, context):
#table_name= "thermostat_dynamo"
table_name= "newsensor"
Primary_Column_Name = 'deviceId'
table = dynamodb.Table(table_name)
#key_param = "thermostat"
#thermostatVal = table.get_item(Key={key_param:event[key_param]}) ## get record from dynamodb for this sensor
thermostatVal= 77
#now = datetime.now()
now = datetime.now(tzlocal())
fifteen_min_ago = now - timedelta(seconds=900)
now = now.strftime('%F %T')
fifteen_min_ago = fifteen_min_ago.strftime('%F %T')
fe = Key('timeStamp').between(fifteen_min_ago,now);
response = table.scan(FilterExpression=fe & Attr('temperature').lt(thermostatVal))
if response['Count'] == 10:
#return thermostatVal+5
thermonew = thermostatVal + 5
tosensor = '{"thermostat":'+'"%s"}' %thermonew
print(tosensor)
#response = client.publish(topic="updatehomesensor", qos=1, payload=tosensor)
return
elif response['Count'] < 10:
#tosensor = '{"thermostat":'+'"%s"}' %thermostatVal
print('{"thermostat":'+'"%s"}' %thermostatVal)
#response = client.publish(topic="updatehomesensor", qos=1, payload=tosensor)
return
