-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombustiveis_proxima_semana
1 lines (1 loc) · 9.46 KB
/
combustiveis_proxima_semana
1
[{"id":"fd40960938c238f3","type":"function","z":"5fc4e22a5f1f2df8","g":"17f74ecb1b50943d","name":"extract_date_info","func":"var monthsPT = [\"janeiro\", \"fevereiro\", \"março\", \"abril\", \"maio\", \"junho\",\n \"julho\", \"agosto\", \"setembro\", \"outubro\", \"novembro\", \"dezembro\"];\n\nfunction extractDate(text) {\n var regex = new RegExp(`(\\\\d{1,2})(?: de (${monthsPT.join(\"|\")}))? a (\\\\d{1,2}) de (${monthsPT.join(\"|\")})`, 'i');\n var match = text.match(regex);\n \n if (match) {\n return `${parseInt(match[1], 10)}${match[2] ? ' de ' + match[2].toLowerCase() : ''} a ${parseInt(match[3], 10)} de ${match[4].toLowerCase()}`;\n }\n \n return null;\n}\n\nmsg.payload = { week: msg.payload.map(extractDate).find(Boolean) || null };\n\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":570,"y":580,"wires":[["189da94c0b49cf92"]]},{"id":"dc4dd67fcd1de1cf","type":"inject","z":"5fc4e22a5f1f2df8","g":"17f74ecb1b50943d","name":"friday_update","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"0 10-17 * * 5","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":190,"y":520,"wires":[["a82aaf679307a0e7"]]},{"id":"a82aaf679307a0e7","type":"http request","z":"5fc4e22a5f1f2df8","g":"17f74ecb1b50943d","name":"precocombustiveis.pt","method":"GET","ret":"txt","paytoqs":"body","url":"https://precocombustiveis.pt/proxima-semana/","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":210,"y":580,"wires":[["80d83a1cebe0bc59","6b63b649e7fd37b1"]]},{"id":"6b63b649e7fd37b1","type":"html","z":"5fc4e22a5f1f2df8","g":"17f74ecb1b50943d","name":"prices","property":"payload","outproperty":"payload","tag":"div.dal.col-12.col-lg > p","ret":"text","as":"single","x":400,"y":520,"wires":[["3a16acb2b016d548"]]},{"id":"80d83a1cebe0bc59","type":"html","z":"5fc4e22a5f1f2df8","g":"17f74ecb1b50943d","name":"week","property":"payload","outproperty":"payload","tag":"div.dal.col-12.col-lg > h2","ret":"text","as":"single","x":400,"y":580,"wires":[["fd40960938c238f3"]]},{"id":"3a16acb2b016d548","type":"function","z":"5fc4e22a5f1f2df8","g":"17f74ecb1b50943d","name":"relevant_info","func":"const fuelTypes = [\"gasóleo\", \"gasolina\"];\n\nfunction cleanString(str) {\n return str.normalize(\"NFD\").replace(/[\\u0300-\\u036f]/g, \"\").toLowerCase();\n}\n\nmsg.payload = msg.payload\n .map(item => removeFirstSentence(cleanString(item)))\n .find(item => fuelTypes.some(fuel => item.includes(cleanString(fuel)))) || null;\n\nreturn msg;\n\nfunction removeFirstSentence(text) {\n return text.replace(/^[^\\.]*\\.\\s*/, \"\");\n}\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":520,"wires":[["bbdc708480fdb663"]]},{"id":"bbdc708480fdb663","type":"function","z":"5fc4e22a5f1f2df8","g":"17f74ecb1b50943d","name":"filter_info","func":"const text = msg.payload;\n\nconst regexDiesel = /gasoleo.*?\\)/;\nconst regexGasoline = /gasolina.*?\\)/;\n\nconst resultDiesel = text.match(regexDiesel);\nconst resultGasoline = text.match(regexGasoline);\n\nconst results = {};\n\nif (resultDiesel) {\n results.diesel = resultDiesel[0];\n}\n\nif (resultGasoline) {\n results.gasoline = resultGasoline[0];\n}\n\nmsg.payload = Object.keys(results).length > 0 ? results : { message: \"No results found\" };\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":520,"wires":[["4309678ddc7e5921"]]},{"id":"189da94c0b49cf92","type":"join","z":"5fc4e22a5f1f2df8","g":"17f74ecb1b50943d","name":"","mode":"custom","build":"merged","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","useparts":true,"accumulate":false,"timeout":"1","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":750,"y":580,"wires":[["f4f45231806474fd"]]},{"id":"4309678ddc7e5921","type":"function","z":"5fc4e22a5f1f2df8","g":"17f74ecb1b50943d","name":"prices","func":"const fuelTypes = [\"diesel\", \"gasoline\"];\n\nfunction extractPrice(fuelString) {\n const regex = /\\(([-+]?\\d+,\\d+)/;\n const match = fuelString.match(regex);\n\n if (!match) {\n return null;\n }\n\n const fuelValue = parseFloat(match[1].replace(',', '.').replace('+', ''));\n\n return fuelValue;\n}\n\nfuelTypes.forEach(fuel => {\n if (msg.payload[fuel]) {\n msg.payload[fuel] = extractPrice(msg.payload[fuel]);\n }\n});\n\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":870,"y":520,"wires":[["189da94c0b49cf92"]]},{"id":"f4f45231806474fd","type":"function","z":"5fc4e22a5f1f2df8","g":"17f74ecb1b50943d","name":"compare_states","func":"const haStates = global.get('homeassistant').homeAssistant.states;\n\nconst week = haStates['sensor.combustiveis_proxima_semana_data'].state;\nconst diesel = parseFloat(haStates['sensor.combustiveis_proxima_semana_gasoleo'].state) || null;\nconst gasoline = parseFloat(haStates['sensor.combustiveis_proxima_semana_gasolina'].state) || null;\n\nif (\n msg.payload.week === week &&\n msg.payload.diesel === diesel &&\n msg.payload.gasoline === gasoline\n) {\n return null;\n}\n\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":920,"y":580,"wires":[["a77d173d0eab32ee","0a749ecc1068da9d","9d6361b1f58ab15b","57e1a867078ea7c7"]]},{"id":"0a749ecc1068da9d","type":"ha-sensor","z":"5fc4e22a5f1f2df8","g":"17f74ecb1b50943d","name":"sensor_fuel_costs_forecast_diesel","entityConfig":"687e491bfde5492c","version":0,"state":"payload.diesel","stateType":"msg","attributes":[],"inputOverride":"allow","outputProperties":[],"x":1200,"y":520,"wires":[[]],"server":""},{"id":"a77d173d0eab32ee","type":"ha-sensor","z":"5fc4e22a5f1f2df8","g":"17f74ecb1b50943d","name":"sensor_fuel_costs_forecast_gasoline","entityConfig":"b5f76f5abcda25b8","version":0,"state":"payload.gasoline","stateType":"msg","attributes":[],"inputOverride":"allow","outputProperties":[],"x":1210,"y":580,"wires":[[]],"server":""},{"id":"9d6361b1f58ab15b","type":"ha-sensor","z":"5fc4e22a5f1f2df8","g":"17f74ecb1b50943d","name":"sensor_fuel_costs_forecast_data","entityConfig":"55104c54cf5eb360","version":0,"state":"payload.week","stateType":"msg","attributes":[],"inputOverride":"allow","outputProperties":[],"x":1200,"y":640,"wires":[[]],"server":""},{"id":"57e1a867078ea7c7","type":"function","z":"5fc4e22a5f1f2df8","g":"17f74ecb1b50943d","name":"message","func":"let data = msg.payload;\n\nlet increaseEmoji = \"🔴\";\nlet decreaseEmoji = \"🟢\";\nlet noChangeEmoji = \"🟡\";\n\nlet message = `🚙 *Combustíveis de ${data.week}*\\n\\n`;\n\nif (data.diesel === 0) {\n message += `${noChangeEmoji} *Gasóleo*: Sem Alteração\\n\\n`;\n} else {\n let dieselValue = data.diesel > 0 ? `+${data.diesel.toString().replace('.', ',')}€` : `${data.diesel.toString().replace('.', ',')}€`;\n let dieselEmoji = data.diesel > 0 ? increaseEmoji : decreaseEmoji;\n message += `${dieselEmoji} *Gasóleo*: ${dieselValue}\\n\\n`;\n}\n\nif (data.gasoline === 0) {\n message += `${noChangeEmoji} *Gasolina*: Sem Alteração`;\n} else {\n let gasolineValue = data.gasoline > 0 ? `+${data.gasoline.toString().replace('.', ',')}€` : `${data.gasoline.toString().replace('.', ',')}€`;\n let gasolineEmoji = data.gasoline > 0 ? increaseEmoji : decreaseEmoji;\n message += `${gasolineEmoji} *Gasolina*: ${gasolineValue}`;\n}\n\nmsg.settings = msg.settings || {};\nmsg.settings.message = message;\n\nreturn msg;\n","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":940,"y":700,"wires":[["22f5922fc5c8a106","9249fb7d67a91876"]]},{"id":"687e491bfde5492c","type":"ha-entity-config","server":"","deviceConfig":"53d4e6d4bb0391cb","name":"sensor_fuel_costs_forecast_diesel","version":"6","entityType":"sensor","haConfig":[{"property":"name","value":"Combustíveis Próxima Semana Gasóleo"},{"property":"icon","value":"mdi:gas-station"},{"property":"entity_picture","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":"monetary"},{"property":"unit_of_measurement","value":"EUR"},{"property":"state_class","value":"measurement"}],"resend":true,"debugEnabled":false},{"id":"b5f76f5abcda25b8","type":"ha-entity-config","server":"","deviceConfig":"53d4e6d4bb0391cb","name":"sensor_fuel_costs_forecast_gasoline","version":"6","entityType":"sensor","haConfig":[{"property":"name","value":"Combustíveis Próxima Semana Gasolina"},{"property":"icon","value":"mdi:gas-station"},{"property":"entity_picture","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":"monetary"},{"property":"unit_of_measurement","value":"EUR"},{"property":"state_class","value":"measurement"}],"resend":true,"debugEnabled":false},{"id":"55104c54cf5eb360","type":"ha-entity-config","server":"","deviceConfig":"53d4e6d4bb0391cb","name":"sensor_fuel_costs_forecast_data","version":"6","entityType":"sensor","haConfig":[{"property":"name","value":"Combustíveis Próxima Semana Data"},{"property":"icon","value":"mdi:calendar-week"},{"property":"entity_picture","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""}],"resend":true,"debugEnabled":false},{"id":"53d4e6d4bb0391cb","type":"ha-device-config","name":"Combustíveis Próxima Semana","hwVersion":"","manufacturer":"joaopedros2","model":"precocombustiveis.pt","swVersion":"","server":""}]