2

first time post, long time reader. I am very new to coding and just trying to teach myself using the net.

I have some code in python im having trouble with and would love some help. I have 2 python scripts that basically read data from my AC unit and the second sends data to it.

Here is the code to get data...(the results are just a long line of text hence the formatted data to extract particular info)

#!/usr/bin/python

import requests
import json

link = "https://actron.ninja.is/rest/v0/device/ACONNECT001EC015ABFE_0_2_4?<user_access_token>"
f = requests.get(link)

raw_data = f.text

formatted_data = json.loads(raw_data)

amOn = formatted_data['data']['last_data']['DA']['amOn']
tempTarget = formatted_data['data']['last_data']['DA']['tempTarget']

print (tempTarget)

and the code to send data...

#!/usr/bin/python

import requests

headers = {
    'Origin': 'https://actronair.com.au',
    'Accept-Encoding': 'gzip, deflate, br',
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'Referer': 'https://actronair.com.au/aconnect/',
    'Connection': 'keep-alive',
}

params = (
    ('user_access_token', '<user access token>'),
)

data = '{"DA":{"tempTarget":24}}'

response = requests.put('https://actron.ninja.is/rest/v0/device/ACONNECT001EC015ABFE_0_2_4', headers=headers, params=params, data=data)

My question and what my goal is, i'd like to merge this into one python file and basically have the program read the current 'tempTarget' and then increase it by an increment of 1 in which this script will be eventually attached to a button.

I am struggling with this as the data sent is a string so i'm imaging i need to convert it to say a dict and +1 the received tempTarget and then back to a string, i just cant seem to get the converting and sequencing right. Or maybe this isnt the way to do it.

Your help is much appreciated.

akkaria
  • 21
  • 1

1 Answers1

0

Use json.dumps()

>>> data = json.loads('{"DA":{"tempTarget":24}}')
>>> data['DA']['tempTarget'] += 1
>>> json.dumps(data)
'{"DA": {"tempTarget": 25}}'
>>> 
  • Thanks for your reply. Unforunatley your first line of code defines the tempTarget as 24 however, what i am trying to do is rather than specify the tempTarget, it will get specified by using the request.get to get the current tempTarget and then adding 1 to that. – akkaria Mar 01 '18 at 00:22