How iaunn/thailand-electric-rate works
Project type and purpose
• This is an open‐source Home Assistant integration/utility for fetching Thailand Electricity Authority (PEA) tariff data, storing it as JSON, and computing user electricity costs via Home Assistant sensors.
• Key indicators: a JSON rate file (thailand_electric_rate.json), a sensor configuration (sensors.yaml), a GitHub Actions workflow to update the JSON, and a small shell script (update_ft.sh).
File structure
• .github/workflows/update_json.yml: CI job that runs daily (or on schedule) to update the JSON with current tariffs.
• update_ft.sh: script invoked by the workflow to scrape or fetch new FT (fuel adjustment) rates and commit them.
• thailand_electric_rate.json: the single source of truth for all tariff values (grid_150, grid_250, grid_400, ft, service).
• sensors.yaml: Home Assistant REST & template sensors that consume the JSON from GitHub’s raw URL and compute the final electricity cost.
• readme.md: documentation for setting up in Home Assistant.
README for additional insights
• The README shows YAML snippets for REST platform sensors (p150, p250, p400, ft, service), a template sensor (electricity_cost), and a utility meter (monthly_energy).
• It references PEA tariff documents and explains how the cost calculation tiers work.
a. Main components
• External Tariff Source (PEA website / PDF)
• CI/CD pipeline (GitHub Actions) + update_ft.sh
• GitHub Repository (holding JSON & YAML)
• Home Assistant Instance
• REST Sensor subsystem
• Utility Meter
• Template Sensor (electricity_cost)
• End‑user Dashboard or Automation
b. Relationships & interactions
1. CI job triggers update_ft.sh → fetches latest FT & tariff values from PEA → updates thailand_electric_rate.json → commits & pushes to GitHub.
2. Home Assistant’s REST sensors poll the raw GitHub URL for thailand_electric_rate.json (e.g., every hour).
3. Utility Meter collects monthly energy usage from an existing sensor (sensor.overall_used).
4. Template Sensor reads the five rate sensors + monthly usage to calculate total cost (including ft adjustment and 7% VAT + service charge).
5. The computed value is exposed to dashboards or automations in Home Assistant.
c. Architectural patterns & design principles
• Configuration‑as‑Code: All logic lives in versioned YAML and JSON.
• CI‑Driven Data Refresh: Use GitHub Actions to keep data fresh.
• Separation of Concerns:
• Data acquisition & storage (CI + JSON)
• Data retrieval (REST sensors)
• Data transformation (template sensor)
• Usage metering (utility_meter)
d. Key technologies & libraries
• GitHub Actions (update_json.yml)
• Shell script (update_ft.sh)
• REST sensor platform in Home Assistant
• Template sensor & Utility Meter integration
Capturing essential elements
• Nest Home Assistant subsystems to indicate they all reside in one host.
• Polling intervals (e.g., “REST sensors: every 3600 s,” “CI job: daily”).