0

I am running a mini project for remote sensing water level in the underground tank. I am collecting data and uploading it using an ESP8266 Wifi to supabase into a table > reading the tables using python and plotting the data using streamlit.

But for some reason on a given run, it shows the correct local IST timing in the chart, on other runs it plots a different UTC timestamp seemingly randomly. I am unable to troubleshoot the problem. Any help would be appreciated.

Below is my code(Ignore the redundancies, I am still learning to code and will progressively iron out the code with time):

from supabase import create_client
import pandas as pd
import streamlit as st
import plotly.express as px
from datetime import datetime, timedelta


API_URL = [redacted]
API_KEY = [redacted]

supabaseList = supabase.table('Water Level').select('*').execute().data

# time range variables updation to put in x axis range parameters
today = datetime.now()
today = today + \
        timedelta(minutes = 30)
present_date = today.strftime("%Y-%m-%d %X")
hrs48 = today - \
        timedelta(days = 2)
back_date = hrs48.strftime("%Y-%m-%d %X")

df = pd.DataFrame()

for row in supabaseList:
    row["created_at"] = row["created_at"].split(".")[0]
    row["time"] = row["created_at"].split("T")[1]
    row["date"] = row["created_at"].split("T")[0]
    row["DateTime"] = row["created_at"]
    df = df.append(row, ignore_index=True)

orignal_title = '<h1 style="font-family:Helvetica; color:Black; font-size: 45px; text-align: center">[Redacted]</p>'
st.markdown(orignal_title, unsafe_allow_html=True)
st.text("")

custom_range = [back_date, present_date]
st.write(custom_range)
fig = px.area(df, x="DateTime", y="water_level", title='',markers=False)

fig.update_layout(
    title={
        'text': "Water level in %",
        'y':0.9,
        'x':0.5,
        'xanchor': 'center',
        'yanchor': 'top'})
fig.update_layout(yaxis_range=[0,120])
fig.update_layout(xaxis_range=custom_range)

#Add Horizontal line for pump trigger level
fig.add_hline(y=80, line_width=3, line_color="black",
              annotation_text="Pump Start Level",
              annotation_position="top left",
              annotation_font_size=15,
              annotation_font_color="black"
              )

st.plotly_chart(fig,use_container_width=True)

run 1 run 2 database table

I am expecting it to always plot in IST timestamps, but it prints out UTC on seemingly random runs.

Mansueli
  • 6,223
  • 8
  • 33
  • 57
  • It's not entirely clear what your problem is. Showing two figures, one with the correct and the other with incorrect timestamps, would be good. Just going by the time and a quick glance through the code, this, `today = datetime.now()` will certainly be different for each run, causing each plot to be different already. – 9769953 Feb 06 '23 at 19:24
  • Don't put the images inside the code block. I had changed it, now you've changed it back. – 9769953 Feb 06 '23 at 19:36
  • Looking at the accepted answer here: https://stackoverflow.com/a/4974930/9173295 might help you fix it. – Aristotelis Feb 06 '23 at 19:37
  • `06-02-2023 19:30:29 ` would in no way be the UTC variant of `07-02-2023 1:02:29`, if that is what you meant: there are no timezones with that weird a different to UTC. Unless perhaps if you factor in the `today` part and you had about 32 or 28 minutes between those two runs. – 9769953 Feb 06 '23 at 19:49
  • @9769953 I am sorry i am very new to this platform, still learning things. (wrt the images) – Vedant Mhatre Feb 06 '23 at 19:49
  • @Aristotelis I think this is something that might help. I will look into this. – Vedant Mhatre Feb 06 '23 at 19:55
  • Are you running this code always on the same machine? – 9769953 Feb 06 '23 at 20:02
  • No, I'm asking because a difference in setting for different machines may mix up your timezones. If you see the issue happening while staying on the exact same machine, then that's not the case. But if you vary machines and see it happen, this may be the case. – 9769953 Feb 06 '23 at 20:40
  • @9769953 Ohh, okay. The issue does persist on the same machine aswell – Vedant Mhatre Feb 07 '23 at 04:57
  • Okay so while troubleshooting the code, I printed out the o/p at different stages of the program. I think there is some issue while fetching the data from supabase. I put a `st.markdown(supabaseList)` after `supabaseList = supabase.table('Water Level').select('*').execute().data` command. At random times it shows UTC timings & intermittently shows IST ie "+05:30" timings which is causing the problems. Now the question is, 'why'? – Vedant Mhatre Feb 07 '23 at 06:23

1 Answers1

0

It seems like the issue was with the database. I created a new project with a new table & it runs flawlessly now. Still, couldn't figure out what caused supabase to send different timestamps on different runs for the same data. But creating a completely new table & relinking with the other system has solved it.