#!/usr/bin/env python3 import json with open('/home/mdares/.node-red/flows.json', 'r') as f: flows = json.load(f) # JavaScript code to add initial load and tab refresh graphs_refresh_code = ''' // Initial load and tab refresh for Graphs scope.refreshGraphData = function() { // Get current filter selection or default to 24h var currentFilter = scope.currentFilter || '24h'; scope.send({ topic: 'fetch-graph-data', action: 'fetch-graph-data', payload: { range: currentFilter } }); }; // Load data immediately on initialization setTimeout(function() { scope.refreshGraphData(); }, 500); // Set up tab refresh interval (every 5 seconds when Graphs tab is visible) scope.graphsRefreshInterval = setInterval(function() { // Check if Graphs tab is visible var graphsElement = document.querySelector('.graphs-wrapper'); if (graphsElement && graphsElement.offsetParent !== null) { scope.refreshGraphData(); } }, 5000); // Cleanup on destroy scope.$on('$destroy', function() { if (scope.graphsRefreshInterval) { clearInterval(scope.graphsRefreshInterval); } }); ''' for node in flows: if node.get('id') == 'f3a4b5c6d7e8f9a0' and node.get('name') == 'Graphs Template': template = node.get('format', '') # Check if refresh code already exists if 'refreshGraphData' in template: print("⚠ Graph refresh code already exists - skipping") else: # Find a good place to insert - look for scope.gotoTab or similar initialization code # If not found, insert before the closing tag script_close = template.rfind('') if script_close != -1: # Insert before closing script tag template = template[:script_close] + '\n' + graphs_refresh_code + '\n' + template[script_close:] node['format'] = template print("✓ Added initial load and tab refresh to Graphs Template") else: print("✗ Could not find tag") break with open('/home/mdares/.node-red/flows.json', 'w') as f: json.dump(flows, f, indent=4) print("✓ flows.json updated")