NEWS
Request ablösen durch httpget
-
@glitzi sagte: Script was ungestellt werden müsste
Welche Version des Javascript-Adapters? Mit neuer Version dürfte das Skript mit
request
so nicht mehr funktionieren. -
v8.3.1, glaube gestern erst ein update gemacht, daher bin ich aktiv geworden
-
@haus-automatisierung sagte in Request ablösen durch httpget:
schedule('*/30 * * * *', () => { httpGet('https://api.forecast.solar/estimate/51,11,23/9,12,54/30/28/12.73', { timeout: 2000 }, (err, response) => { if (err) { console.error(err); } else if (response.statusCode == 200) { const res = JSON.parse(response.data).result.watts; for (const [key, val] of Object.entries(res)) { sendTo('influxdb.0', 'storeState', { id: 'javascript.0.e3dc.PV_Forecast', state: { ts: new Date(key).getTime(), val, ack: true, from: 'javascript.0', q: 0 } }); } } }); });
Funktioniert, Danke!
-
@Alle
Habe heute auch auf JS v8.3.1 upgedated.
Vor gefühlt 10 Jahren habe ich meine ersten und letzten Schritte in JS getan und bin nachwievor jeden Tag vom Ergebnis überrascht
Nun habe ich auch die Aufforderung "request" umzustellen und schaffe es nicht. Bitte um eure Hilfe!
Original, funkt immer noch jedoch mit "httpGet" Warnung:
// Original von Greogorius https://forum.iobroker.net/topic/20314/skripten-des-e-control-spritpreisrechners/188 // schedule("2,32 * * * *", function (){ //jede 2. und 32 Minute // schedule("*/10 * * * *", function (){ //alle 10 Minuten schedule("* * * * *", function (){ //alle Minuten /// console.log("Start Infos holen"); var result = ""; try { require("request")('https://api.e-control.at/sprit/1.0/search/gas-stations/by-address?latitude=48.xxxxxxxx&longitude=16.xxxxxxxxxx&fuelType=DIE&includeClosed=true', function (error, response, result) { // require("httpGet")('https://api.e-control.at/sprit/1.0/search/gas-stations/by-address?latitude=48.xxxxxxx&longitude=16.xxxxxxxxxx&fuelType=DIE&includeClosed=true', function (error, response, result) { console.log(result); createState("javascript.0.scriptDatenPunkte.Sprit_AT.Parser.JSON", 0,{type: 'mixed',name: 'JSON', read: true, write: true}); // setState("a_andreas.0.eigene_dp.Spritpreisrechner.JSON", result); //ich wollte die Ergebnisse in einer anderen Struktur haben, geht aber nicht weil über JS nur unter JS möglich setState("javascript.0.scriptDatenPunkte.Sprit_AT.Parser.JSON", result); }).on("error", function (e) {console.error(e);}); } catch (e) { console.error(e); } console.debug("request: " + 'https://api.e-control.at/sprit/1.0/search/gas-stations/by-address?latitude=48.xxxxxxx&longitude=16.xxxxxxxx&fuelType=DIE&includeClosed=true'); /// console.log("Ende Infos holen"); }); on({id: 'javascript.0.scriptDatenPunkte.Sprit_AT.Parser.JSON'/*Sprit JSON*/, change: 'any'}, function(obj) // on({id: 'a_andreas.0.sys_variablen.Objekt_JSON'/*Sprit JSON*/, change: 'any'}, function(obj) //funktioniert vor selbst URL abholen // on({id: 'parser.0.Spritpreisrechner'/*Sprit JSON*/, change: 'any'}, function(obj) { var index = 0; // var price = "undefined"; var gasStation = JSON.parse(obj.state.val); if (gasStation.length === 0) return; /// console.log((String('Start Durchlauf'))); for (index = 0; index < gasStation.length; index++) { createState("javascript.0.scriptDatenPunkte.Sprit_AT.Tankstelle_"+index+".Index", 0,{type: 'number', name: 'ID', read: true, write: true}); setState("javascript.0.scriptDatenPunkte.Sprit_AT.Tankstelle_"+index+".Index", gasStation[index].id); createState("javascript.0.scriptDatenPunkte.Sprit_AT.Tankstelle_"+index+".name", 0,{type: 'string', name: 'Name', read: true, write: true}); setState("javascript.0.scriptDatenPunkte.Sprit_AT.Tankstelle_"+index+".name", gasStation[index].name); createState("javascript.0.scriptDatenPunkte.Sprit_AT.Tankstelle_"+index+".Preis", 0,{type: 'string', name: 'Preis', read: true, write: true}); if ((gasStation[index].prices[0])!=undefined){ setState("javascript.0.scriptDatenPunkte.Sprit_AT.Tankstelle_"+index+".Preis", (gasStation[index].prices[0].amount).toString().replace(".",",")); }; createState("javascript.0.scriptDatenPunkte.Sprit_AT.Tankstelle_"+index+".PreisNumber", 0,{type: 'number', name: 'Preis', read: true, write: true}); if ((gasStation[index].prices[0])!=undefined){ setState("javascript.0.scriptDatenPunkte.Sprit_AT.Tankstelle_"+index+".PreisNumber", gasStation[index].prices[0].amount); }; createState("javascript.0.scriptDatenPunkte.Sprit_AT.Tankstelle_"+index+".Strasse", 0,{type: 'string', name: 'Strasse', read: true, write: true}); setState("javascript.0.scriptDatenPunkte.Sprit_AT.Tankstelle_"+index+".Strasse", gasStation[index].location.address); createState("javascript.0.scriptDatenPunkte.Sprit_AT.Tankstelle_"+index+".PLZ & Ort", 0,{type: 'string', name: 'PLZ & Ort', read: true, write: true}); setState("javascript.0.scriptDatenPunkte.Sprit_AT.Tankstelle_"+index+".PLZ & Ort", gasStation[index].location.postalCode + " "+ gasStation[index].location.city); createState("javascript.0.scriptDatenPunkte.Sprit_AT.Tankstelle_"+index+".Distanz", 0,{type: 'string', name: 'Luftlinie/km', read: true, write: true}); setState("javascript.0.scriptDatenPunkte.Sprit_AT.Tankstelle_"+index+".Distanz", ((Math.round((gasStation[index].distance)*100)/100).toString().replace(".",","))); createState("javascript.0.scriptDatenPunkte.Sprit_AT.Tankstelle_"+index+".Offen", 0,{type: 'boolean', name: 'Offen', read: true, write: true}); setState("javascript.0.scriptDatenPunkte.Sprit_AT.Tankstelle_"+index+".Offen", gasStation[index].open); } /// console.log((String('Ende Durchlauf'))); /// console.log((String('Start eine Tankstelle finden'))); createState("javascript.0.scriptDatenPunkte.Sprit_AT.Favorit.Preis", 0,{type: 'string', name: 'Preis', read: true, write: true}); createState("javascript.0.scriptDatenPunkte.Sprit_AT.Favorit.Name", 0,{type: 'string', name: 'Name', read: true, write: true}); createState("javascript.0.scriptDatenPunkte.Sprit_AT.Favorit.Adresse", 0,{type: 'string', name: 'Adresse', read: true, write: true}); createState("javascript.0.scriptDatenPunkte.Sprit_AT.Favorit.Offen", 0,{type: 'boolean', name: 'Offen', read: true, write: true}); setState("javascript.0.scriptDatenPunkte.Sprit_AT.Favorit.Preis", "kein Preis verfügbar"); for (index = 0; index < gasStation.length; index++) { if (gasStation[index].id == 1469126) { //Turmoil, Willergasse if ((gasStation[index].prices[0])!=undefined){ setState("javascript.0.scriptDatenPunkte.Sprit_AT.Favorit.Preis", (gasStation[index].prices[0].amount).toString().replace(".",",")); }; setState("javascript.0.scriptDatenPunkte.Sprit_AT.Favorit.Name", gasStation[index].name); setState("javascript.0.scriptDatenPunkte.Sprit_AT.Favorit.Adresse", gasStation[index].location.postalCode + " " + gasStation[index].location.city + ", " + gasStation[index].location.address); setState("javascript.0.scriptDatenPunkte.Sprit_AT.Favorit.Offen", gasStation[index].open); } } /// console.log((String('Ende eine Tankstelle finden'))); } );
Log:
javascript.0 23:35:00.002 warn script.js.common.900-919_Information.904_Spritpreise_AT_von_e-Control: request package is deprecated - please use httpGet (or a stable lib like axios) instead! javascript.0 23:35:00.202 info script.js.common.900-919_Information.904_Spritpreise_AT_von_e-Control: [{"id":33395,"name":"SOCAR Perchtoldsdorf","location":{"address":"Ketzergasse 191a","postalCode":"2380","city":"Perchtoldsdorf","latitude":48.1301731,"longitude":16.2919418},"contact":{"telephone":"4318674570","fax":"4318674804","mail":"office-at@socarenergy.com","website":"https://www.socarenergy.at/"},"openingHours":[{"day":"MO","label":"Montag","order":1,"from":"00:00","to":"24:00"},{"day":"DI","label":"Dienstag","order":2,"from":"00:00","to":"24:00"},{"day":"MI","label":"Mittwoch","order":3,"from":"00:00","to":"24:00"},{"day":"DO","label":"Donnerstag","order":4,"from":"00:00","to":"24:00"},{"day":"FR","label":"Freitag","order":5,"from":"00:00","to":"24:00"},{"day":"SA","label":"Samstag","order":6,"from":"00:00","to":"24:00"},{"day":"SO","label":"Sonntag","order":7,"from":"00:00","to":"24:00"},{"day":"FE","label":"Feiertag","order":8,"from":"00:00","to":"24:00"}],"offerInformation":{"service":false,"selfService":true,"unattended":true},"paymentMethods":{"cash":true,"debitCard":true,"creditCard":true,"others":"UTA, DKV, Socarcard"},"paymentArrangements":{"cooperative":false,"clubCard":false},"position":1,"open":true,"distance":4.248287202581326,"prices":[{"fuelType":"DIE","amount":1.554,"label":"Diesel"}]},{"id":1469126,"name":"Turmöl Quick","location":{"address":"Breitenfurterstraße 473","postalCode":"1230","city":"Wien","latitude":48.137832,"longitude":16.257546},"contact":{"mail":"marketing-box@doppler.at"},"openingHours":[{"day":"MO","label":"Montag","o
Mein diletanischer Versuch:
// Original von Greogorius https://forum.iobroker.net/topic/20314/skripten-des-e-control-spritpreisrechners/188 // schedule("2,32 * * * *", function (){ //jede 2. und 32 Minute // schedule("*/10 * * * *", function (){ //alle 10 Minuten schedule("* * * * *", function (){ //alle Minuten /// console.log("Start Infos holen"); var result = ""; try { // require("request")('https://api.e-control.at/sprit/1.0/search/gas-stations/by-address?latitude=48.xxxxxxx&longitude=16.xxxxxx&fuelType=DIE&includeClosed=true', function (error, response, result) { require("httpGet")('https://api.e-control.at/sprit/1.0/search/gas-stations/by-address?latitude=48.xxxxxxx&longitude=16.xxxxxxxx&fuelType=DIE&includeClosed=true', function (error, response, result) { console.log(result); createState("javascript.0.scriptDatenPunkte.Sprit_AT.Parser.JSON", 0,{type: 'mixed',name: 'JSON', read: true, write: true}); // setState("a_andreas.0.eigene_dp.Spritpreisrechner.JSON", result); //ich wollte die Ergebnisse in einer anderen Struktur haben, geht aber nicht weil über JS nur unter JS möglich setState("javascript.0.scriptDatenPunkte.Sprit_AT.Parser.JSON", result); }).on("error", function (e) {console.error(e);}); } catch (e) { console.error(e); } console.debug("request: " + 'https://api.e-control.at/sprit/1.0/search/gas-stations/by-address?latitude=48.xxxxxxx&longitude=16.xxxxxxx&fuelType=DIE&includeClosed=true'); /// console.log("Ende Infos holen"); }); .............
Log:
javascript.0 23:32:00.008 error script.js.common.900-919_Information.904_Spritpreise_AT_von_e-Control: Error: Cannot find module 'httpGet' javascript.0 23:32:00.008 error at Object.<anonymous> (script.js.common.900-919_Information.904_Spritpreise_AT_von_e-Control:12:9) javascript.0 23:32:00.008 error script.js.common.900-919_Information.904_Spritpreise_AT_von_e-Control: TypeError: require(...) is not a function at Object.<anonymous> (script.js.common.900-919_Information.904_Spritpreise_AT_von_e-Control:13:27) at Job.job (/opt/iobroker/node_modules/iobroker.javascript/lib/sandbox.js:1769:34) at Job.invoke (/opt/iobroker/node_modules/iobroker.javascript/node_modules/node-schedule/lib/Job.js:171:15) at /opt/iobroker/node_modules/iobroker.javascript/node_modules/node-schedule/lib/Invocation.js:268:28 at Timeout._onTimeout (/opt/iobroker/node_modules/iobroker.javascript/node_modules/node-schedule/lib/Invocation.js:228:7) at listOnTimeout (node:internal/timers:569:17) at processTimers (node:internal/timers:512:7)
In den JS Adapter Einstellungen kann ich nix mehr hinzufügen, dachte das wäre die Lösung. Geht aber nicht, oder ich raffe es nicht.
Bitte um Eure Unterstzützung!
-
teste mal das
// Original von Greogorius https://forum.iobroker.net/topic/20314/skripten-des-e-control-spritpreisrechners/188 // schedule("2,32 * * * *", function (){ //jede 2. und 32 Minute // schedule("*/10 * * * *", function (){ //alle 10 Minuten schedule(" * * * * *", function (){ //alle Minuten /// console.log("Start Infos holen"); //var result = ""; try { // require("request")('https://api.e-control.at/sprit/1.0/search/gas-stations/by-address?latitude=48.xxxxxxx&longitude=16.xxxxxx&fuelType=DIE&includeClosed=true', function (error, response, result) { httpGet('https://api.e-control.at/sprit/1.0/search/gas-stations/by-address?latitude=48.11&longitude=16.22&fuelType=DIE&includeClosed=true', function (error, response) { console.log(response.data); createState("javascript.0.scriptDatenPunkte.Sprit_AT.Parser.JSON", 0,{type: 'mixed',name: 'JSON', read: true, write: true}); // setState("a_andreas.0.eigene_dp.Spritpreisrechner.JSON", result); //ich wollte die Ergebnisse in einer anderen Struktur haben, geht aber nicht weil über JS nur unter JS möglich setState("javascript.0.scriptDatenPunkte.Sprit_AT.Parser.JSON", response.data); }) } catch (e) { console.error(e); } // console.debug("request: " + 'https://api.e-control.at/sprit/1.0/search/gas-stations/by-address?latitude=48.11&longitude=16.22&fuelType=DIE&includeClosed=true'); /// console.log("Ende Infos holen"); });
-
@liv-in-sky Top Arbeit!
Ich bedanke mich herzlichst! Läuft ohne Fehlermeldung durch und liefert Daten.
Jetzt kann ich mich ohne Stress mit Blockly auseinandersetzen und schauen ob ich es dort nachbauen kann.
lg, mxa
-
@metaxa sagte in Request ablösen durch httpget:
ohne Stress mit Blockly auseinandersetzen
ist einfacher mit blockly - gibt schon einige beispiele im forum
-
Ich bräuchte hier jetzt auch einmal eure Hilfe.
Bekomme nun auch diesen Fehler: ReferenceError: request is not definedrequest( { url: "http://admin:tester@192.168.1.214/record/current.jpg", encoding: null, }, (error, response, body) => { if (!error && body) { writeFile("vis.0", "/current.jpg", body); } } );
Bist jetzt hat es funktioniert, habe aber keine Ahnung, was ich da machen soll.
-
Schau dir das mal an , ist aus der Doku vom JavaScript Adapter.
httpGet('http://1.2.3.4/image.jpg', { responseType: 'arraybuffer' }, async (err, response) => { if (!err) { writeFile('0_userdata.0', 'test.jpg', response.data, (err) => { if (err) { console.error(err); } }); } else { console.error(err); } });
Sollte für dich passen, musst du nur mit deinen Werten anpassen.
-
@tt-tom Hervorragend, danke dir vielmals. Funktioniert.