def run_example():
    sensor = SensorAsOutputThing(DummyTempSensor('temp-1', input_sequence))
    sensor.output()  # let us see the raw values
    dispatcher = sensor.transduce(RunningAvg(4))\
                       .passthrough(lambda evt:
                                    print("Running avg temp: %s" %
                                          round(evt.val, 2))) \
                       .dispatch([(lambda v: v[2]>=T_high, 't_high'),
                                  (lambda v: v[2]<=T_low, 't_low')])
    controller = Controller()
    dispatcher.connect(controller, port_mapping=('t_high', 't_high'))
    dispatcher.connect(controller, port_mapping=('t_low', 't_low'))
    dispatcher.connect(controller, port_mapping=('default', 'between'))
    controller.connect(BypassValveActuator())
    sensor.print_downstream()
    scheduler = Scheduler(asyncio.get_event_loop())
    scheduler.schedule_periodic(sensor, 0.5)
    scheduler.run_forever()
    print("got to the end")

def create_dispatch_rule(sensor_id):
    return (lambda evt: evt.sensor_id == sensor_id, sensor_id)


dispatch_rules = [
    create_dispatch_rule(remote_id) for remote_id in REMOTE_SENSORS
]

scheduler = Scheduler(asyncio.get_event_loop())

if HAS_LOCAL_SENSOR:
    sensor = SensorAsOutputThing(LuxSensor(sensor_id=LOCAL_SENSOR_ID))
    sensor.rolling_csv_writer(DIRECTORY, LOCAL_SENSOR_ID)
    sensor.output()
    scheduler.schedule_periodic_on_separate_thread(sensor, 60)

mqtt_reader = MQTTReader('localhost',
                         client_id='rpi',
                         topics=[
                             ('remote-sensors', 0),
                         ])
# we convert the tuple received into a SensorEvent, overwriting the timestamp.
dispatcher = mqtt_reader.map(lambda m:(m.payload).decode("utf-8"))\
                        .from_json()\
                        .map(lambda tpl: SensorEvent(sensor_id=tpl[0], ts=time.time(), val=tpl[2]))\
                        .dispatch(dispatch_rules)
# For each remote sensor, we create a separate csv writer
for remote in REMOTE_SENSORS:
    dispatcher.rolling_csv_writer(DIRECTORY, remote, sub_port=remote).output()