Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. [Vorlage] Screenshots Überwachungskamera Telegramm

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    [Vorlage] Screenshots Überwachungskamera Telegramm

    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      smarthomeNew last edited by

      Re: [Frage BLOCKLY ] Klingel Bild per Telegram versenden / Snapshot von Cam per Telegram versenden

      Hallo, habe auf Grundlage eines Skripts von Uhula noch einen anderen Ansatz. Hatte das Skript im Einsatz und während ich im Urlaub war wurde mein Telegram geflutet von Screenshots. Ausgelöst wurde bei jeder Biene etc die durchs Bild geflogen ist. Ergebnis war dass keiner mehr die Screenshots angeschaut hat. Nicht Sinn der Sache.
      Hier mein umgebautes, vielleicht hilft es jemandem

      // -------------------------------------------------------------------------
      // Dieses Skript sendet Screenshots der Überwachungskamera per Telegram
      // Ausgelöst wird das ganze nur, wenn keiner zu Hause ist und die Bewegung
      // auch nach x Minuten noch existiert oder die Kamera nach x Minuten
      // offline ist.
      // -------------------------------------------------------------------------
      
      //run only if this object is true
      var oidAllAway = 'alias.0.presence.allAway';
      
      //run only if motion still available after minutes
      var minutesToWait = 1;
      
      //path to save the images
      var path = '/opt/iobroker/iobroker-data/files/_temp/cctv/workshopShelter/';
      //delete images in this path which are older than days
      var deleteOlderThanDays = 7;
      
      //data for this sensor:
      var imageURL =  getState('alias.0.building.0.outdoor.workshopShelter.cctv.links.lowResSnapshot').val;
      var motionOID = 'alias.0.building.0.outdoor.workshopShelter.cctv.motion';
      var cctvAliveOID = 'alias.0.building.0.outdoor.workshopShelter.cctv.alive.alive';
      
      //------------------------------
      //nothing to change after here
      //------------------------------
      //------------------------------
      
      var request = require('request');
      var fs      = require('fs');
      
      var motionTimestamp;
      var motionTimestampStrngNow;
      
      // send image with telegram
      function sendImage (timestamp) {
          var imgPath = path + timestamp + ".jpg"; 
          try {
              var stats = fs.statSync(imgPath);
              var msg = formatDate(stats.birthtime,"DD.MM.YYYY hh:mm:ss");
              sendTo('telegram.0', {
                  text:                   imgPath,
                  caption:                msg, 
                  disable_notification:   true
              });  
              console.log("image '"+ path + timestamp + ".jpg' sent")
          }
          catch(err) { if (err.code != "ENOENT") log(err); }     
      }
      
      function sendMessage () { 
          try {
              var messageText = '!!!!!ACHTUNG!!!!! DER ' + (u_getName(motionOID,0) + ' ' + u_getName(motionOID,-1) + ' ' + u_getName(motionOID,-2)).toUpperCase()  + ' WURDE AUSGELÖST OBWOHL KEINER ZU HAUSE IST!';
              sendTo('telegram', messageText);      
          }
          catch(err) { if (err.code != "ENOENT") log(err); }     
      }
      
      //deletes all files in path older than x days
      function deleteOldFiles() {
          try {
              fs.readdirSync(path).forEach(file => {
                  const isOlder = fs.statSync(path+file).ctime < Date.now() - (deleteOlderThanDays * 24 * 60 * 60 * 1000);
      
                  if (isOlder) {
                      fs.unlinkSync(path+file);
                  } 
      
              })
          }
          catch(err) { if (err.code != "ENOENT") log(err); }
      }
      
      // saveImage
      function saveImage (timestamp) {
          // Bild holen und speichern
          request.get({url: imageURL, encoding: 'binary'}, function (err, response, body) {
              fs.writeFile(path + timestamp + ".jpg", body, 'binary', function(err) {
                  if (err) {
                      log('Error while saving picture: ' + err, 'warn');
                  }
                  else {console.log("image "+ path + timestamp + ".jpg" +" saved");}
              }); 
          });
      }
      
      function run() {
      
          motionTimestamp = Date.now();
          console.log('Motion Detected');
      
          //save motion image
          saveImage(motionTimestamp);
      
          //wait "minutsToWait", check if motion = yes or cam not alive, if yes than send message and images
          setTimeout(function() {
      
              console.log('After '+minutesToWait+ ' minute:');
              console.log("check if motionOID = true or cctvAliveOID = false");
      
              if ( (getState(motionOID).val) || (!(getState(cctvAliveOID).val)) ) {
      
                  console.log('After '+minutesToWait+ ' minutes, there is still motion or cam suddenly not alive. Send image...');
      
                  //send warning message message and initial image
                  sendMessage();
                  sendImage(motionTimestamp);
      
                  //save an actual image
                  motionTimestampStrngNow = Date.now();
                  saveImage(motionTimestampStrngNow);
      
                  // send image after 2 seconds of time buffer (for saving file)
                  setTimeout(function() { sendImage(motionTimestampStrngNow); }, 2000); 
                  
              } else {
                  console.log('Everything fine. End.');
              }
      
          }, (minutesToWait * 60 * 1000) ); // x * 60 * 1000 to convert minutes to milliseconds
          
          //delete files older than x days
          deleteOldFiles();
      }
      
      
      on({id: motionOID, val: true}, function (obj) {
          //if nobody is at home
          if (getState(oidAllAway).val) { 
              run();
          }
      });
      
      
      // manual run for testing porpuses
      run();
      
      

      Erst wird geprüft, ob auch alle Familienmitglieder außer Haus sind.
      Dann wird der Screenshot gespeichert.
      Wenn nach x Minuten immer noch Bewegung ist oder auch die Kamera plötzlich aus ist (also von Einbrechern eventuell beschädigt wurde etc), wird das aufgenommene Bild geschickt und zusätzlich noch ein aktuelles aufgenommen und verschickt.

      1 Reply Last reply Reply Quote 0
      • First post
        Last post

      Support us

      ioBroker
      Community Adapters
      Donate

      927
      Online

      31.9k
      Users

      80.1k
      Topics

      1.3m
      Posts

      1
      1
      164
      Loading More Posts
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes
      Reply
      • Reply as topic
      Log in to reply
      Community
      Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
      The ioBroker Community 2014-2023
      logo