Example gist by Kevin on using nimbits in the MisterHouse home automation program.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Category = Charting | |
#@ Charts data points to nimbits cloud | |
use JSON; | |
$nimbits_proc = new Process_Item; | |
$house_wattage = new Generic_Item; | |
if ($Reload){ | |
# Used to initialize an item whose state is numeric and should be tracked | |
# directly, such as a thermometer | |
nimbits_initialize($w_temp, "out_temp","value"); | |
nimbits_initialize($upstairs_thermo_temp, "up_temp","value"); | |
nimbits_initialize($garage_battery, "garage_battery","value"); | |
nimbits_initialize($upstairs_thermo_humidity, "up_humidity","value"); | |
#Dimming Object would look like: | |
nimbits_initialize($f_fm_lt_ma, "family_room","wattage", 57, 'house_wattage'); | |
nimbits_initialize($f_kt_sink_lt_ma, "kitchen_sink", "wattage", 65, 'house_wattage'); | |
nimbits_initialize($f_en_lt_ma, "entry_way", "wattage", 405, 'house_wattage'); | |
nimbits_initialize($f_dn_ch_lt_ma, "dining_chandelier", "wattage", 100, 'house_wattage'); | |
# initialize aggregate items last, so not setting the data_point for each | |
# new member | |
nimbits_initialize($house_wattage, "house_wattage","value"); | |
} | |
# Initializes the nimbit data_point. Does many things ...... | |
sub nimbits_initialize{ | |
my ($object, $data_point, $tie_type, $max_watt, $aggregate_name) = @_; | |
#set the proper tie function type and initialize aggregate | |
my $curr_value; | |
if ($tie_type eq "value"){ | |
$object->tie_event("nimbits_post_aggregate('$data_point',\$state," | |
. "'$aggregate_name')"); | |
$curr_value = $object->state; | |
} | |
elsif ($tie_type eq "wattage"){ | |
$object->tie_event("nimbits_post_wattage('$data_point',\$state," | |
."'$max_watt','$aggregate_name')"); | |
$curr_value = nimbits_wattage($object->state, $max_watt); | |
} | |
elsif ($tie_type eq "network"){ | |
$object->tie_event("nimbits_post_network('$data_point',\$state," | |
."'$max_watt','$aggregate_name')"); | |
$curr_value = nimbits_network($object->state, $max_watt); | |
} | |
elsif ($tie_type eq "whf"){ | |
$object->tie_event("nimbits_post_whf('$data_point',\$state," | |
."'$max_watt','$aggregate_name')"); | |
$curr_value = nimbits_whf($object->state, $max_watt); | |
} | |
#Initialize aggregate item | |
if ($aggregate_name){ | |
set_aggregate_item($aggregate_name, $data_point, $curr_value); | |
} | |
# Call nimbits post without a value to initialize the data point | |
nimbits_post_value($data_point); | |
} | |
# Posts a dimmable wattage to nimbits | |
sub nimbits_post_wattage { | |
my ($data_point, $p_state, $max_watt, $aggregate_name) = @_; | |
my $act_watt = nimbits_wattage($p_state, $max_watt); | |
nimbits_post_aggregate($data_point,$act_watt, $aggregate_name); | |
} | |
# Converts onlevel and maximum wattage into a current wattage value | |
sub nimbits_wattage { | |
my ($p_state, $max_watt) = @_; | |
my $level = 100; | |
if ($p_state eq 'off' or $p_state eq 'off_fast') { | |
$level = 0; | |
} | |
elsif ($p_state =~ /\d+%?/) { | |
$level = $p_state; | |
$level =~ /(\d+)%?/; | |
} | |
my $act_watt = $max_watt * ($level/100); | |
return $act_watt; | |
} | |
# Posts a network wattage to nimbits | |
sub nimbits_post_network { | |
my ($data_point, $p_state, $max_watt, $aggregate_name) = @_; | |
my $act_watt = nimbits_network($p_state, $max_watt); | |
nimbits_post_aggregate($data_point,$act_watt, $aggregate_name); | |
} | |
# Converts returns full wattage if up | |
sub nimbits_network { | |
my ($p_state, $max_watt) = @_; | |
my $level = $max_watt; | |
if ($p_state eq 'down') { | |
$level = 0; | |
} | |
return $level; | |
} | |
# Posts a whf wattage to nimbits | |
sub nimbits_post_whf { | |
my ($data_point, $p_state, $max_watt, $aggregate_name) = @_; | |
my $act_watt = nimbits_whf($p_state, $max_watt); | |
nimbits_post_aggregate($data_point,$act_watt, $aggregate_name); | |
} | |
# Converts high low wattage | |
sub nimbits_whf { | |
my ($p_state, $max_watt) = @_; | |
my $level = $max_watt; | |
if ($whf_main->state eq 'off') { | |
$level = 0; | |
} | |
elsif ($whf_speed->state eq 'on'){ | |
$level = 197; | |
} | |
else { | |
$level = 44; | |
} | |
return $level; | |
} | |
# Track aggregate values, and post total value | |
sub nimbits_post_aggregate { | |
my ($data_point, $value, $aggregate_name) = @_; | |
#if aggregate item, call this function on the aggregate_class | |
if ($aggregate_name){ | |
set_aggregate_item($aggregate_name, $data_point, $value); | |
} | |
#then call normal | |
nimbits_post_value($data_point, $value,); | |
} | |
# how to store the aggregate amount | |
sub set_aggregate_item { | |
my ($aggregate_name, $data_point, $value) = @_; | |
my $aggregate_item = get_object_by_name($aggregate_name); | |
$$aggregate_item{aggregate_members}{$data_point} = $value; | |
#Calculate new total value and call set | |
my $aggregate_total = 0.0; | |
my $on_items_string; | |
for (keys %{$$aggregate_item{aggregate_members}}) { | |
$aggregate_total += $$aggregate_item{aggregate_members}{$_}; | |
if ($$aggregate_item{aggregate_members}{$_} > 0){ | |
$on_items_string = $on_items_string . $_ . ", "; | |
} | |
} | |
if ($aggregate_total != $aggregate_item->state()){ | |
$aggregate_item->set($aggregate_total, $on_items_string); | |
} | |
} | |
# Used to post a value to nimbits, uses an external process to post | |
# if no value is defined, it makes sure the data point exists | |
sub nimbits_post_value { | |
my ($data_point, $value, $epoch) = @_; | |
my $nim_url; | |
if (defined $value){ | |
if (defined $epoch){ | |
$nim_url = "get_url -quiet -post 'json={\"d\":$value, \"t\":$epoch}' "; | |
} | |
else { | |
$nim_url = "get_url -quiet -post 'json={\"d\":$value}' "; | |
} | |
$nim_url .= "'http://cloud.nimbits.com/service/v2/value?key=123456&email=test\@example.com&id=test\@example.com/$data_point' " | |
."/dev/null"; | |
} | |
else { | |
#Create the data_point if it doesn't exist | |
$nim_url = "get_url -quiet -post 'json={\"highAlarm\":0.0,\"lowAlarm\":0.0," | |
."\"deltaAlarm\":0.0,\"deltaSeconds\":0,\"expire\":10000,\"unit\":null," | |
."\"highAlarmOn\":false,\"lowAlarmOn\":false,\"idleAlarmOn\":false," | |
."\"deltaAlarmOn\":false,\"idleSeconds\":0,\"idleAlarmSent\":false," | |
."\"filterType\":4,\"filterValue\":0.1,\"inferLocation\":false," | |
."\"pointType\":0,\"values\":null,\"value\":null,\"name\":\"$data_point\"," | |
."\"description\":null,\"entityType\":1,\"protectionLevel\":2," | |
."\"alertType\":0,\"parent\":\"test\@example.com\"," | |
."\"owner\":\"test\@gexamplemail.com\",\"readOnly\":false,\"children\":null," | |
."\"instanceUrl\":null,\"isCached\":true}&action=createmissing' " | |
."'http://cloud.nimbits.com/service/v2/entity?key=123456&email=test\@example.com' " | |
."/dev/null"; | |
} | |
if ($nimbits_proc->done != 0){ | |
$nimbits_proc->set($nim_url); | |
$nimbits_proc->start(); | |
} else { | |
$nimbits_proc->add($nim_url); | |
} | |
} | |
# Used to post a batch of values to nimbits, uses an external process to post | |
sub nimbits_post_batch { | |
my ($batch_hash_ref) = @_; | |
my $batch_json = encode_json $batch_hash_ref; | |
my $nim_url = "get_url -post 'json=$batch_json' " | |
."'http://cloud.nimbits.com/service/v2/batch?key=123456&email=test\@example.com' " | |
."/dev/null"; | |
if ($nimbits_proc->done != 0){ | |
$nimbits_proc->set($nim_url); | |
$nimbits_proc->start(); | |
} else { | |
$nimbits_proc->add($nim_url); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment