Servus zusammen,
ich stelle euch hier das angepasste Skript zum "Zählen von States" zur Verfügung.
Hiemit ist es möglich anhand von ids, funktionen und räumen, geräte aufzulisten und den Zustand abzufragen und in ein Datenpunkt zu schreiben.
Hilfreich zum Beispiel für Fenstersensoren und Batterieabfragung.
Code
//Add device rules
addDeviceCounter("battery_maxcube",$('state[id=*.battery_low](functions=batterie)'), true);
addDeviceCounter("battery_mihome", $('state[id=*.percent](functions=batterie)'), function(state){return state<30;});
addDeviceCounter("fenster",$('state[id=*](functions=fenster)'), true);
addDeviceCounter("lichter",$('state[id=*](functions=licht)'), true);
//addDeviceCounter("sticky_unreach", $('channel[state.id=*.STICKY_UNREACH]'), true);
//addDeviceCounter("unreach", $('channel[state.id=*.UNREACH]'), true);
//addDeviceCounter("config_pending", $('channel[state.id=*.CONFIG_PENDING]'), true);
//addDeviceCounter("open_windows", $('channel{TYPE=SHUTTER_CONTACT}[state.id=*.STATE]'), true);
//addDeviceCounter("on_lights", $('channel{TYPE=DIMMER}[state.id=*.LEVEL]'), function(state){return state>0;});
//addDeviceCounter("open_shutters", $('channel{TYPE=BLIND}[state.id=*.LEVEL]'), function(state){return state>0;});
//addDeviceCounter("playing_music", $('channel[state.id=*audio-*.playing]'), true);
// Beispiele iobroker Forum
//$('state[id=*.state](functions=fenster)'); --- Nach .state und enum.functions suchen
//$('state[id=*](functions=fenster)'); --- Nach enum.functions suchen
//$('channel[state.id=mihome.0.devices.magnet*.state]'); --- Nach id und state suchen
//$(‘state[id=.STATE]’) or $(‘state[state.id=.STATE]’) or $(‘*.STATE’) – select all states where id ends with “.STATE”.
//$(‘state[id=’hm-rpc.0.]’) or $(‘hm-rpc.0.‘) – returns all states of adapter instance hm-rpc.0
//$(‘channel(rooms=Living room)’) – all states in room “Living room”
//$(‘channel{TYPE=BLIND}[state.id=*.LEVEL]’) – Get all shutter of Homematic
//$(‘channel[role=switch](rooms=Living room)[state.id=*.STATE]’).setState(false) –
//Switch all states with .STATE of channels with role “switch” in “Living room” to false
//$(‘channel[state.id=*.STATE](functions=Windows)’).each(function (id, i) {log(id);}); – print all states of enum “windows” in log
/******************************************
* Code - do not change anything bellow this line *
******************************************/
function addDeviceCounter(name, selector, val_to_count) {
//Total amount of devices found by selector
var state_total = 'deviceCounter.' + name + '.total';
createState(state_total, 0);
//List of devices that match condition
var state_list = 'deviceCounter.' + name + '.list';
createState(state_list, "");
//Number of devices that match condition
var state_counter = 'deviceCounter.' + name + '.counter';
createState(state_counter, 0);
function countDevs(obj) {
var d_count = 0;
var d_list = [];
selector.each(function (id, i) {
var status = getState(id).val;
var obj = getObject(id);
if ((typeof val_to_count === 'function' && val_to_count(status)) ||
(typeof val_to_count === 'boolean' && val_to_count == status)) {
//Get parent object
var roomsX = getObject(id,'rooms');
//var functionsX = getObject(id,'functions'); // function behind room
//var parentChannelId = id.slice(id.lastIndexOf("."));
//var parentDevicelId = id.split(".").slice(0,-2).join(".");
//var deviceObject = getObject(parentDevicelId);
//remember device name & functions
d_list.push(roomsX.enumNames)//, functionsX.enumNames);
}
++d_count; // Zählt die Anzahl der vorhandenen Geräte unabhängig vom Status
});
log(name + "(" + d_count + "): " + d_list);
setState(state_list, d_list.join('<br>'));
setState(state_counter, d_list.length);
setState(state_total, d_count);
}
//Re-evaluate when a device status changes
selector.on(function(obj) { // bei Zustandänderung *. LOWBAT in allen Gewerken
countDevs(obj);
});
//Re-evaluate each 1/4 hour
schedule("*/15 * * * *", countDevs);
//Evaluate for the first time
countDevs();
Alles anzeigen