{"id":13586303,"url":"https://github.com/crowbarz/ha-sql_json","last_synced_at":"2026-01-17T06:41:16.591Z","repository":{"id":51827968,"uuid":"304573324","full_name":"crowbarz/ha-sql_json","owner":"crowbarz","description":"Updated SQL integration for Home Assistant that supports JSON attributes","archived":false,"fork":false,"pushed_at":"2025-09-17T08:04:30.000Z","size":22,"stargazers_count":9,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-09-17T10:13:07.289Z","etag":null,"topics":["hacs","home-assistant","integration","json","sql"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/crowbarz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-10-16T08:59:17.000Z","updated_at":"2025-09-17T08:02:25.000Z","dependencies_parsed_at":"2024-08-01T16:42:32.388Z","dependency_job_id":null,"html_url":"https://github.com/crowbarz/ha-sql_json","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/crowbarz/ha-sql_json","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crowbarz%2Fha-sql_json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crowbarz%2Fha-sql_json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crowbarz%2Fha-sql_json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crowbarz%2Fha-sql_json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/crowbarz","download_url":"https://codeload.github.com/crowbarz/ha-sql_json/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crowbarz%2Fha-sql_json/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28502816,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-17T04:31:57.058Z","status":"ssl_error","status_checked_at":"2026-01-17T04:31:45.816Z","response_time":85,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacs","home-assistant","integration","json","sql"],"created_at":"2024-08-01T15:05:27.706Z","updated_at":"2026-01-17T06:41:16.577Z","avatar_url":"https://github.com/crowbarz.png","language":"Python","readme":"# sql_json\n\nUpdated SQL integration for Home Assistant that supports JSON attributes.\n\nThis quick mod of the [core SQL integration](https://www.home-assistant.io/integrations/sql/) converts an SQL query result that parse as JSON to an object, allowing the result dataset to be accessed by templates and other integrations, as well as displayed in Lovelace cards.\n\n## Installation using [HACS](https://hacs.xyz/)\n\n1. [Install HACS](https://hacs.xyz/docs/installation/manual) if not already installed\n1. Select **Community \u003e Integrations \u003e +** (in the bottom right corner)\n1. Search for `sql_json` and select **Install this repository in HACS**\n\n## Configuration\n\nThis integration is configured identically to the [core SQL integration](https://www.home-assistant.io/integrations/sql/). Replace the platform with `sql_json` to switch the query to use this integration.\n\nThe query may be a Jinja2 template, which will be rendered whenever the query is executed.\n\nNote that there is a hard limit of [255 characters](https://github.com/home-assistant/core/blob/9ee97cb213d659aa9b6149484c0a42522decba78/homeassistant/core.py#L136) for states in Home Assistant, although attributes may contain [any amount of data as long as it is JSON serialisable](https://developers.home-assistant.io/docs/dev_101_states).\n\n## Accessing JSON result data\n\nJSON result data can be accessed in Jinja2 templates by using `state_attr`. For example:\n\n```jinja\nEntity with most events (last 24 hours): {{ state_attr(state_attr('sensor.recorder_top_events', 'json')[0].entity_id, 'friendly_name') }}\n```\n\nIt is already possible to access data elements within a JSON result with the core SQL integration in templates using the [`from_json` filter](https://www.home-assistant.io/docs/configuration/templating/), however doing this causes the JSON string to be deserialised every time that the template is evaluated which is exponentially inefficient for large query results.\n\n## Configuration example\n\nThe following configuration snippet defines a sensor that the recorder database for the top 10 entities with the most events during the 24 hours prior to the time that the sensor is updated. The automation updates the sensor at 01:30 daily. Set `db_url` to the database URL used for your recorder instance in `secrets.yaml`.\n\nThis configuration has been tested on HA 2023.4.\n\n```yaml\n# Example configuration.yaml\nsensor:\n  - platform: sql_json\n    scan_interval: 2592000  ## 30 days\n    db_url: !secret db_url\n    queries:\n        - name: Recorder Top Events\n          query: |\n            SELECT CONCAT('[', GROUP_CONCAT(event_json), ']') as json\n            FROM (\n              SELECT JSON_OBJECT('entity_id', entity_id, 'count', event_count) AS event_json\n              FROM (\n                SELECT states_meta.entity_id, COUNT(*) AS event_count\n                FROM states\n                LEFT JOIN states_meta ON states.metadata_id = states_meta.metadata_id\n                WHERE last_updated_ts BETWEEN UNIX_TIMESTAMP()-86400 AND UNIX_TIMESTAMP()\n                GROUP BY states.metadata_id\n                ORDER BY event_count DESC\n                LIMIT 10\n              ) AS json_output\n            ) AS json_list;\n        value_template: '{{ value_json[0].count }}'\n        unit_of_measurement: events\n        column: json\n\nautomation:\n  - id: update_recorder_top_events_daily\n    alias: Update Recorder Top Events daily\n    mode: single\n    trigger:\n      platform: time\n      at: 01:30:00\n    action:\n      - service: homeassistant.update_entity\n        entity_id: sensor.recorder_top_events\n\n```\n\nThe entities returned by the SQL query can then be displayed in a Lovelace card:\n\n**NOTE**: requires the [`flex-table-card`](https://github.com/custom-cards/flex-table-card) Lovelace card to be installed.\n\n```yaml\ntype: vertical-stack\ncards:\n  - type: sensor\n    entity: sensor.recorder_top_events\n    graph: line\n    hours_to_show: 720\n    name: Top Events by Entity (30 days)\n    icon: 'mdi:comment-text-multiple-outline'\n  - type: 'custom:flex-table-card'\n    columns:\n      - data: json\n        modify: x.entity_id\n        name: Entity ID\n      - data: json\n        modify: x.count\n        name: events/day\n      - data: json\n        modify: (x.count/24).toFixed(2)\n        name: events/h\n    entities:\n      include: sensor.recorder_top_events\n```\n","funding_links":[],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrowbarz%2Fha-sql_json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrowbarz%2Fha-sql_json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrowbarz%2Fha-sql_json/lists"}