Author Topic: Reading rain rate more frequently than the archive period  (Read 1309 times)

0 Members and 1 Guest are viewing this topic.

Offline sylvaing

  • Member
  • *
  • Posts: 2
Reading rain rate more frequently than the archive period
« on: April 08, 2022, 03:43:24 PM »
Hi, I've been using WeeWx for many years now but first time posting here.

I'm writing a program in Python that queries my Acurite 5 in 1 probe through WeeWX for the rain rate and if it's above 0mm, then close the car windows if they are opened. It works well but relies on daily.json to fetch the rain rate and that value I believe updates at the archive frequency (ie, 5 minutes) but the stations sends that data every 36 seconds. Is there a way to fetch this value? The only other option that I can think of is to set the "archive_interval" to 60 instead of 300 in [StdArchive]. If I do that, will that mess up all the historical data already in the database? Any other side effects?

Thanks for your help at keeping the inside of my car dry  :oops:
 

Offline sylvaing

  • Member
  • *
  • Posts: 2
Re: Reading rain rate more frequently than the archive period
« Reply #1 on: April 09, 2022, 09:18:26 PM »
I switched from reading the JSON data to subscribing to my HomeAssistant MQTT where WeeWX also publishes its loop data (raw data ever 18 seconds). It's working perfectly.

This is the code if anyone is interested.
Code: [Select]
#!/usr/bin/env python3

import teslapy
import paho.mqtt.client as mqtt
import json
import time
from datetime import datetime

raining = False

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe("acurite/loop")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
#    print(msg.topic+" "+str(msg.payload.decode('utf-8')))
    jsondata = json.loads(str(msg.payload.decode('utf-8')))
    rain_cm = jsondata.get('rain_cm')

    if rain_cm is not None:
        now = datetime.now()
        current_time = now.strftime("%H:%M:%S")
        print(current_time + ": " + rain_cm)

        rain = float(rain_cm)
        if rain > 0.0:
            if raining == False:
                raining = True
                print("Reading car")
                tesla = teslapy.Tesla('username@example.com')
                if not tesla.authorized:
                    tesla.refresh_token(refresh_token=input('Enter SSO refresh token: '))
                vehicles = tesla.vehicle_list()
                vehicles[0].sync_wake_up()  # We need to get up to date data so no choice but to wake it
                vehicleData = vehicles[0].get_vehicle_data()

                fd_window = int(vehicleData['vehicle_state']['fd_window'])
                fp_window = int(vehicleData['vehicle_state']['fp_window'])
                rd_window = int(vehicleData['vehicle_state']['rd_window'])
                rp_window = int(vehicleData['vehicle_state']['rp_window'])
                windows = fd_window + fp_window + rd_window + rp_window
                print('Number of windows open: ' + str(windows))

                moving = vehicleData['drive_state']['shift_state']
                print('Driving: ' + str(moving))

                if moving is None and windows > 0:
                    print("Closing windows!")

                    latitude=vehicleData['drive_state']['latitude']
                    longitude=vehicleData['drive_state']['longitude']

                    print(latitude)
                    print(longitude)
                    vehicles[0].command('WINDOW_CONTROL', command='close', lat=latitude, lon=longitude)
                else:
                    print("It's raining but our windows are closed")

                tesla.close()
            else:
                print("Already checked for this rain period, skipping")
        else:
            raining = False
            print("All is fine")

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.username_pw_set(username="username", password="password")

print("Connecting...")
client.connect("hostname", 1883, 60)

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()

 

anything