create account

Using smart lights for witness vote notifications by emrebeyler

View this thread on: hive.blogpeakd.comecency.com
· @emrebeyler · (edited)
$137.78
Using smart lights for witness vote notifications
I had been exploring the scripting options of Hue lights lately. One of the use cases of these smart lightbulbs is that you can actually use the lights and their colors for notifications. While playing with it, I've decided to create notifications on my witness' vote approvals/disapprovals.

The goal is simple:

- Blink green when somebody approves my witness
- Blink red when somebody **dis**approves my witness

To have the same setup, you need:

- At least one Hue white and color smart lightbulb
- Hue bridge

#### Getting Started
***

I've added three scenes to my target room (workspace). 

- Witness Vote Approval
- Witness Vote Disapproval
- Default

<center>![photo5843617973795928702.jpg](https://images.hive.blog/DQmbuMVfE9qHop4e2KAnXGNVHuPZiun3s168q1Rztbu8WV6/photo5843617973795928702.jpg)</center>

The software should listen to every transaction in the HIVE network, and if it sees an `account_witness_vote` operation targeted to our witness account,  it should blink the lights green or red. 

I would make the blinking colors in the scripting side, however, instead, I chose to trigger scenes so that I can play with the colors without touching the code again.


| Operation                                                    | Action |
|--------------------------------------------------|---------------|
|Witness Approval                               |   ![IMG-3061.gif](https://images.hive.blog/DQmeQN8znKgcqiH6mqdSRjvewWjUYiTBTN2pWuudspG3C2Y/IMG-3061.gif)        |
|Witness Disapproval                               |   ![IMG-3058.gif](https://images.hive.blog/DQmVkLpc2FTgXAw45FDsto6xPu2523U9FqYnqw34S9DMdTz/IMG-3058.gif)


#### Python script
***
```
import logging
import time

from lighthive.client import Client
from phue import Bridge
import threading
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# CONFIGURATION
HUE_BRIDGE_IP = "<ip>"  # can be shown at hue app
WITNESSES = ["emrebeyler",]
ROOM_NAME = "Workspace"
LIGHTBULB_NAME = "Workspace light"


class TransactionListener:

    def __init__(self, client, blockchain_mode="head",
                 start_block=None, end_block=None):
        self.client = client
        self.blockchain_mode = blockchain_mode or "irreversible"
        self.start_block = start_block
        self.end_block = end_block
        self.bridge = Bridge(HUE_BRIDGE_IP)


    def get_last_block_height(self):
        props = self.client.get_dynamic_global_properties()
        return props['head_block_number']

    def get_block(self, block_num):
        logger.info("Getting block: %s", block_num)
        block_data = self.client.get_block(block_num)
        return block_data

    def process_block(self, block_data):
        for transaction in block_data.get("transactions", []):
            for op_type, op_value in transaction.get("operations"):
                if op_type != "account_witness_vote":
                    continue

                if op_value.get("witness") not in WITNESSES:
                    continue

                t = threading.Thread(
                    target=self.notify,
                    args=(
                        op_value, ),
                    kwargs={},
                )
                t.start()

    def notify(self, op):

        default_state = self.bridge.get_light(LIGHTBULB_NAME)

        scene_name = 'Witness Vote Approval' if op.get("approve") else \
            'Witness Vote Disapproval'

        for i in range(0, 5):
            self.bridge.run_scene(ROOM_NAME, scene_name,
                        transition_time=5)
            time.sleep(0.5)
            self.bridge.run_scene(ROOM_NAME, 'Default', transition_time=5)
            time.sleep(0.5)

        defaults = {
            'on': default_state['state']['on'],
            'sat': default_state['state']['sat'],
            'hue': default_state['state']['hue'],
            'bri': default_state['state']['bri'],
        }

        self.bridge.set_light(LIGHTBULB_NAME, defaults, transitiontime=5)

    def listen(self):
        current_block = self.get_last_block_height()
        while True:
            while (self.get_last_block_height() - current_block) > 0:
                if self.end_block and current_block > self.end_block:
                    return
                else:
                    yield self.get_block(current_block)
                current_block += 1

            time.sleep(2.9)


if __name__ == '__main__':

    c = Client()
    tx_listener = TransactionListener(c)
    for block_data in tx_listener.listen():
        tx_listener.process_block(block_data)
```
***

Just update the configuration and run it. It requires `lighthive` and `phue` libraries installed in your system. I put this into `supervisord` so if somehow the script stops due to node failures, it should restart itself automatically.

Also, in a different Python file, you need to authenticate the phue library with your Hue bridge. Press the authenticate button in the Hue bridge and run this one:

```
from phue import Bridge
b = Bridge('<bridge_api>')
b.connect()
```
***
This is required only once.

***
#### A call to action, testing time! 😂
***

This lightbulb in the gifs is connected to my workspace's main lightning at the moment. If you didn't vote for my witness yet, vote for my witness on [Hivesigner](https://hivesigner.com.com/sign/account-witness-vote?witness=emrebeyler&approve=1) or on [PeakD](https://peakd.com/witnesses) so that  we can see the live result. 

<sup>Currently testing is limited only to approval actions please don't troll me. :P </sup>


Cheers,
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 137 others
👎  
properties (23)
authoremrebeyler
permlinkusing-smart-lights-for-witness-vote-notifications
categoryhive-139531
json_metadata{"app":"hiveblog/0.1","format":"markdown","image":["https://images.hive.blog/DQmbuMVfE9qHop4e2KAnXGNVHuPZiun3s168q1Rztbu8WV6/photo5843617973795928702.jpg","https://images.hive.blog/DQmeQN8znKgcqiH6mqdSRjvewWjUYiTBTN2pWuudspG3C2Y/IMG-3061.gif","https://images.hive.blog/DQmVkLpc2FTgXAw45FDsto6xPu2523U9FqYnqw34S9DMdTz/IMG-3058.gif"],"links":["https://hivesigner.com.com/sign/account-witness-vote?witness=emrebeyler&approve=1","https://peakd.com/witnesses"],"tags":["smart-home","development","witness","witness-category"]}
created2020-04-24 13:23:30
last_update2020-04-24 15:42:54
depth0
children13
last_payout2020-05-01 13:23:30
cashout_time1969-12-31 23:59:59
total_payout_value72.336 HBD
curator_payout_value65.440 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,580
author_reputation448,535,049,068,622
root_title"Using smart lights for witness vote notifications"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,976,561
net_rshares147,646,596,794,455
author_curate_reward""
vote details (202)
@brianoflondon ·
So cool! I've got an idea for you regarding witnesses and polls, what's the best way to contact you for a chat? I'm @Brianoflondon pretty much everywhere. 
properties (22)
authorbrianoflondon
permlinkre-emrebeyler-2020424t17625434z
categoryhive-139531
json_metadata{"tags":["smart-home","development","witness","witness-category"],"app":"esteem/2.2.5-mobile","format":"markdown+html","community":"hive-125125"}
created2020-04-24 14:06:24
last_update2020-04-24 14:06:24
depth1
children1
last_payout2020-05-01 14:06:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length155
author_reputation760,626,613,375,672
root_title"Using smart lights for witness vote notifications"
beneficiaries
0.
accountesteemapp
weight300
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,977,109
net_rshares0
@emrebeyler ·
$0.30
thanks!

I'll dm you today or tomorrow.
👍  
👎  
properties (23)
authoremrebeyler
permlinkq9aue3
categoryhive-139531
json_metadata{"app":"hiveblog/0.1"}
created2020-04-24 15:44:27
last_update2020-04-24 15:44:27
depth2
children0
last_payout2020-05-01 15:44:27
cashout_time1969-12-31 23:59:59
total_payout_value0.149 HBD
curator_payout_value0.150 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length39
author_reputation448,535,049,068,622
root_title"Using smart lights for witness vote notifications"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,978,337
net_rshares571,246,955,134
author_curate_reward""
vote details (2)
@cardboard ·
https://www.wykop.pl/cdn/c3201142/comment_GqjGdjfDdqce52G5BGKU1DM7ZDPTrwx0.gif
properties (22)
authorcardboard
permlinkre-emrebeyler-q9apn8
categoryhive-139531
json_metadata{"tags":["hive-139531"],"app":"peakd/2020.04.5"}
created2020-04-24 14:02:00
last_update2020-04-24 14:02:00
depth1
children1
last_payout2020-05-01 14:02:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length79
author_reputation31,522,757,177,122
root_title"Using smart lights for witness vote notifications"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,977,047
net_rshares0
@emrebeyler ·
😂
properties (22)
authoremrebeyler
permlinkq9aub4
categoryhive-139531
json_metadata{"app":"hiveblog/0.1"}
created2020-04-24 15:42:42
last_update2020-04-24 15:42:42
depth2
children0
last_payout2020-05-01 15:42:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1
author_reputation448,535,049,068,622
root_title"Using smart lights for witness vote notifications"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,978,312
net_rshares0
@gulshannsaini ·
Hi.. You know so much programming.. You are making different lights to glow for different actions..
properties (22)
authorgulshannsaini
permlinkq9l0mf
categoryhive-139531
json_metadata{"app":"hiveblog/0.1"}
created2020-04-30 03:35:06
last_update2020-04-30 03:35:06
depth1
children0
last_payout2020-05-07 03:35:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length99
author_reputation1,710,729,654,830
root_title"Using smart lights for witness vote notifications"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id97,088,110
net_rshares0
@paragism ·
It actually looks cool
properties (22)
authorparagism
permlinkre-emrebeyler-2020425t1415215z
categoryhive-139531
json_metadata{"tags":["smart-home","development","witness","witness-category"],"app":"esteem/2.2.5-mobile","format":"markdown+html","community":"hive-125125"}
created2020-04-25 08:46:54
last_update2020-04-25 08:46:54
depth1
children1
last_payout2020-05-02 08:46:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length22
author_reputation36,292,755,022,096
root_title"Using smart lights for witness vote notifications"
beneficiaries
0.
accountesteemapp
weight300
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,989,066
net_rshares0
@emrebeyler ·
Thanks! :)
properties (22)
authoremrebeyler
permlinkre-paragism-q9ezix
categoryhive-139531
json_metadata{"tags":["hive-139531"],"app":"peakd/2020.04.5"}
created2020-04-26 21:25:45
last_update2020-04-26 21:25:45
depth2
children0
last_payout2020-05-03 21:25:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length10
author_reputation448,535,049,068,622
root_title"Using smart lights for witness vote notifications"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id97,017,061
net_rshares0
@rishi556 ·
$0.13
So theoretically if you make it blink only once per vote, it's possible to send some morse code through this? Using blinks as the pauses and the time in between as long/short presses.
👍  
properties (23)
authorrishi556
permlinkre-emrebeyler-q9bo3p
categoryhive-139531
json_metadata{"tags":["hive-139531"],"app":"peakd/2020.04.5"}
created2020-04-25 02:26:12
last_update2020-04-25 02:26:12
depth1
children5
last_payout2020-05-02 02:26:12
cashout_time1969-12-31 23:59:59
total_payout_value0.066 HBD
curator_payout_value0.067 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length183
author_reputation132,735,189,827,616
root_title"Using smart lights for witness vote notifications"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,985,605
net_rshares272,586,873,656
author_curate_reward""
vote details (1)
@bubke ·
you need to use time intervals of at least 3 secs then or can you do more than 1 vote in a block?
properties (22)
authorbubke
permlinkre-rishi556-q9btpt
categoryhive-139531
json_metadata{"tags":["hive-139531"],"app":"peakd/2020.04.4"}
created2020-04-25 04:27:33
last_update2020-04-25 04:27:33
depth2
children3
last_payout2020-05-02 04:27:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length97
author_reputation9,285,631,006,103
root_title"Using smart lights for witness vote notifications"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,986,519
net_rshares0
@rishi556 ·
Min time intervals of 3 sec. So dots can be 3 sec wait, and dashes be 6 seconds. 
properties (22)
authorrishi556
permlinkre-bubke-q9btrl
categoryhive-139531
json_metadata{"tags":["hive-139531"],"app":"peakd/2020.04.5"}
created2020-04-25 04:28:33
last_update2020-04-25 04:28:33
depth3
children2
last_payout2020-05-02 04:28:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length81
author_reputation132,735,189,827,616
root_title"Using smart lights for witness vote notifications"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,986,536
net_rshares0
@emrebeyler ·
Well, with a little bit of modification it should be possible. :)
properties (22)
authoremrebeyler
permlinkre-rishi556-q9ezob
categoryhive-139531
json_metadata{"tags":["hive-139531"],"app":"peakd/2020.04.5"}
created2020-04-26 21:29:00
last_update2020-04-26 21:29:00
depth2
children0
last_payout2020-05-03 21:29:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length65
author_reputation448,535,049,068,622
root_title"Using smart lights for witness vote notifications"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id97,017,100
net_rshares0