pythonasynchronous

Python Tibber module just return latest value, not a loop


Ultimately I want to write a value into a database.

I found a script to output desired data. However it does return it in a loop - but I only need one single value each time I trigger the script, not a loop.

import tibber

account = tibber.Account("tokenstring")
home = account.homes[0]


@home.event("live_measurement")
async def process_data(
        data):
    print(data.power)


home.start_live_feed(user_agent="Homey/10.0.0")

print(home.event("live_measurement"))`

This will output values in a loop - but I only need one value each time the code runs.

Help!


Solution

  • The start_live_feed function starts an infinite loop if called with default arguments. In other words, Python will not run any code after you call this function.

    The function takes an argument exit_condition. This is a function that is run after every time data is received. If it returns a truthy value, the loop will end and Python will continue running whatever comes after the line home.start_live_feed(...).

    So, to run it a single time, you just need to pass a function that always returns True.

    import tibber
    
    account = tibber.Account(TOKEN_STRING)
    home = account.homes[0]
    
    
    @home.event("live_measurement")
    async def process_data(data):
        # Database storing logic here
    
    home.start_live_feed(user_agent="Homey/10.0.0", exit_condition=lambda data: True)