1

my dynamodb table has timestamp(in YYYY-MM-DD HH:MN:SS) as PrimaryKey column and temperature as sortkey while in data {"humidity" : 42 ,"location":"room" , "temperature":,"thermostat":}

in boto3 python i need to scan based on timestamp (now and 15min ago) with condition if difference(temperature - thermostat) > 5 for more than 10 times then return thermostat-5 and if (temperature - thermostat) < 5 for more than 10 times then returns thermostat+5... following is the code

import boto3
import math
import json
import time
import dateutil.tz
from datetime import datetime,timedelta
from dateutil import tz
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= "TableDynamo"
    Primary_Column_Name = 'timestamp'
    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
    
    south = dateutil.tz.gettz('Asia/Kolkata')
   
    now = datetime.now(tz=south)
    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":'+ str(thermonew) + '}'
        print(tosensor)
        #response = client.publish(topic="updatehomesensor", qos=1, payload=tosensor)
        return

    elif response['Count'] < 10:
        print('{"thermostat":' + str(thermostatVal) + '}')
        return

   
    
2
  • show your work please Commented Jun 8, 2020 at 22:01
  • def lambda_handler(event, context): 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: thermonew = thermostatVal + 5 tosensor = '{"thermostat":'+ str(thermonew) + '}' print(tosensor) return elif response['Count'] < 10: print('{"thermostat":' + str(thermostatVal) + '}') return Commented Jun 9, 2020 at 13:37

1 Answer 1

0

If timestamp was a sort key, you could have used a Query request to scan through all the items with timestamp > now-15min.

However, unfortunately, timestamp is your hash key. The only way you can find the items with timestamp > now-15min is to Scan through all your items. This will cost you a lot of money: You pay Amazon for each item scanned, not each item returned after the filtering.

Another problem is that the DynamoDB filtering syntax (look at the FilterExpression documentation) doesn't actually allow to do addition and subtractions as part of the test. If you always want to do "temperature - thermostat", you can use that as one of the attributes (so you can do a FilterExpression on it), and the second attribute would be "thermostat", and later you can add the two up to get the "temperature".

Sign up to request clarification or add additional context in comments.

1 Comment

I am using following ... fe = Key('timeStamp').between(fifteen_min_ago,now); response = table.scan(FilterExpression=fe & Attr('temperature').lt(thermostatVal)) ... how to do temperature - thermostat with FilterExpression ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.