First Weatherhat temperature always the same

Having sorted out the libraries required for storing the sensor results from the Weatherhat in a database, I’m seeing all that the temperature readings are the same. When I run basic.py from the examples, it gives a first reading - say 16 degrees - and the subsequent ones then vary based on the temperatures.


It doesn’t make a good record though.
Why doesn’t the hat just output the correct temperature from the first request?

Hi Insomniac. How did you get past that error message of your first post in December? I need help.

I have this issue aswell, I’ve started using Cron to run the job instead of having a while loop goin on for days/months, and its really annoying I’m going to have to flush my first reading which is annoying, I believe this is the same with the Enviro, why does this occur

Results for 5 readings demonstating this issue
{'RecordedDT': {1705858124}, 'Temperature': {15.271960980916628}, 'Humidity': {68.08316797076145}, 'Rain mm/Sec': {646.374775243581}, 'Windspeed': {0.0}, 'WindDir': {0}, 'DewPoint': {16.388594575068918}, 'Light': {0.0}, 'Pressure': {646.374775243581}}
{'RecordedDT': {1705858134}, 'Temperature': {6.640831367889609}, 'Humidity': {61.11929451134104}, 'Rain mm/Sec': {989.1253048075257}, 'Windspeed': {2.178589859958381}, 'WindDir': {0}, 'DewPoint': {6.3646902701578165}, 'Light': {0.0}, 'Pressure': {989.1253048075257}}
{'RecordedDT': {1705858144}, 'Temperature': {6.644000577376778}, 'Humidity': {60.96241050372475}, 'Rain mm/Sec': {989.1077125950397}, 'Windspeed': {1.7737278940562262}, 'WindDir': {0}, 'DewPoint': {6.336482678121727}, 'Light': {0.0}, 'Pressure': {989.1077125950397}}
{'RecordedDT': {1705858154}, 'Temperature': {6.64590210312365}, 'Humidity': {60.88127394195644}, 'Rain mm/Sec': {989.1124113836345}, 'Windspeed': {1.5819749226069337}, 'WindDir': {315}, 'DewPoint': {6.322156891514937}, 'Light': {0.0}, 'Pressure': {989.1124113836345}}
{'RecordedDT': {1705858164}, 'Temperature': {6.647803628911447}, 'Humidity': {60.90845620619305}, 'Rain mm/Sec': {989.1041278328364}, 'Windspeed': {0.7670185831668752}, 'WindDir': {0}, 'DewPoint': {6.329494870150057}, 'Light': {0.0}, 'Pressure': {989.1041278328364}}

With my Weatherhat I’ve tried taking the adafruit-io.py file from Examples and changed it to write to SQL.
It ignores the first reading and runs when called in Cron.
The question marks now are how to record wind speed - given it’s a momentary reading - and how to add in an i2c temperature sensor, as the onboard temperature sensor is inside a waterproof box (hence leaving in the OFFSET setting, but it’s not worked so far).

import time
import pymysql
import datetime
import math
from time import sleep

import weatherhat

sensor = weatherhat.WeatherHAT()

print("""
adafruit-io.py - Example showing how to send sensor data from Weather HAT into adafruit.io.
Sign up for an account at https://io.adafruit.com/ to obtain a username and key.
Press Ctrl+C to exit!
""")

# We can compensate for the heat of the Pi and other environmental conditions using a simple offset.
# Change this number to adjust temperature compensation!
OFFSET = -7.5

# Create an instance of the REST client.
#aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
connect = pymysql.connect(host="localhost", user="user",passwd="passwd", db="db")


# Read the BME280 and discard the initial nonsense readings
sensor.update(interval=10.0)
sensor.temperature_offset = OFFSET
temperature = sensor.temperature
humidity = sensor.relative_humidity
#pressure = sensor.pressure
tpressure = "{0:0.2f}".format(int(sensor.pressure * 100)/100.0)
pressure = tpressure
print("Discarding the first few Weatherhat readings...")
sleep(2.0)

# Read all the sensors and start sending data

while True:
    sensor.update(interval=30.0)

    wind_direction_cardinal = sensor.degrees_to_cardinal(sensor.wind_direction)
    temperature = sensor.temperature
    device_temperature = sensor.device_temperature
    humidity = sensor.relative_humidity
    #pressure = sensor.pressure
    tpressure = "{0:0.2f}".format(int(sensor.pressure * 100)/100.0)
    pressure = tpressure
    light = sensor.lux
    windspeed = sensor.wind_speed
    dew = sensor.dewpoint
    winddirection = wind_direction_cardinal
    rain = sensor.rain
    datetime = datetime.datetime.now()
    
    try:
        with connect.cursor() as query:
        	query.execute('INSERT INTO table VALUES (%s, %s, %s, %s, %s, %s, %s)',
        	(datetime, temperature, device_temperature, humidity, light, dew, pressure))
        	connect.commit()
        	connect.close()

        print(f"""
              {datetime}
              """) 
            
    except Exception as e:
        print(e)

    # leave at least 30 seconds between updates for free Adafruit.io accounts
    sleep(30.0)

I use a MQTT broker to do the DB Inserts, Makes things a lot more modular

I do wish i could understand why the first readings are just bad, its it something to do with the initalition of the HAT?

import datetime, time, weatherhat, os, dotenv, json
import paho.mqtt.client as mqtt

def jsonWeather(obj, userInterval):
    
    obj.update(interval=userInterval)
    time.sleep(userInterval)
    data = {
        "RecordedDT":int(datetime.datetime.timestamp(datetime.datetime.now())), 
        "Temperature":obj.temperature, 
        "Humidity":obj.humidity, 
        "Rain mm/Sec":obj.pressure, 
        "Windspeed":obj.wind_speed, 
        "WindDir":obj.wind_direction, 
        "DewPoint":obj.dewpoint, 
        "Light":obj.lux, 
        "Pressure":obj.pressure, 
    }
    return json.dumps(data)



"""
These Variables are configurable.
I personally like storing server information in .env files but its not required
Remove dotenv within the code below and plug the ip and port straight into mqttHost and mqttPort

"""
dotenv.load_dotenv()
mqttHost = str(os.getenv("mqttHost"))
mqttPort = int(os.getenv("mqttPort"))
mqttTopic = "Weather"

"""
Setting up the Weatherhat and MQTT as easily understandable Classes
"""
outPost = weatherhat.WeatherHAT()
client=mqtt.Client()
client.connect(host=mqttHost, port=mqttPort)

jsonWeather(outPost,5)## have to flush first reading as its always wrong.
client.publish(mqttTopic,json.dumps(jsonWeather(outPost,5)))