Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

How to add another trace to a plotly graph?

Hi all,

I have a python script running to output room temperatures from my Evohome heating system to a Plotly graph, which works beautifully using the following code:

# We make a plot for every room
for device in client.temperatures():
    stream_id = Config.get('Rooms', device['id'])
    stream = Stream(token=stream_id,
        maxpoints=288
    )
    trace1 = Scatter(
           x=[],
           y=[],
           mode='lines+markers',
           line=Line(
               shape='spline'
               ),
           stream = stream
           )

    data = Data([trace1])
    layout = Layout(title=device['name'])
    fig = Figure(data=data, layout=layout)
    py.plot(fig, filename=device['name'], fileopt='extend')

# Infinite loop every 5 minutes, send temperatures to plotly
while True:

# Get current time and then send all thermostat readings to plotly
    try:
        client = EvohomeClient(USERNAME, PASSWORD)
        from datetime import datetime
        j=0
        for device in client.temperatures():
            stream_id = Config.get('Rooms', device['id'])
            j+=1
            s = py.Stream(stream_id)
            s.open()
            tijd = datetime.now().strftime('%Y-%m-%d %H:%M')
            temperatuur = float(device['temp'])
            print tijd + " : " + device['name'] + " " + str(temperatuur)
            s.write(dict(x=tijd ,y=temperatuur))
            s.close()
        print "Going to sleep for 5 minutes"
        time.sleep(300)
    except Exception, e:
        print "An error occured! Trying again in 15 seconds"
        print str(e)
        time.sleep(15)

As you can see, the existing trace is plotting “device[‘temp’]”. I’d like to add a second trace to the same axes which plots "device[‘setpoint’]. The code to extract the information from the Honeywell servers is working fine, I just can’t get the code to add another trace to the Plotly graphs.

Struggling to get it to work, any help appreciated!

Thanks,

Gareth

Hi, so you need to create a new traces (ie: trace2) for this data, along with a unique stream id for each trace in the initial for loop.
Then in the second for loop, you would have to write information to these additional stream ids as well.

Hi garmcqui ,
Did you manage to achieve this ? Any config you could share please as I’m looking at doing the same with my Evohome.
Thanks