with thanks to the inspriation https://github.com/sunnythaper/python_scripts
OVERVIEW
This script allows you to export your current entity states/attributes into a file!
- original idea is to use the extracted data for later reference, e.g. compare forecast of yesterday with actual of today
- extracted file contains lines related to 'notify', see below and needs to be cleaned before import into sensor
EXAMPLE JSON
{
"entity": ["weather.home"],
"attributes": ["forecast"],
"save_file": true
}
or as a service
service: python_script.extract_entity
data:
entity: weather.forecast_home
attributes:
- forecast
save_file: true
FILE SAVING
The script can save directly to a file in the HASS configuration directory, simply add a file notification declaration exactly as below. The file name is referenced by the script, so do not change the script name. You may change the filename to whatever you wish, however.
notify:
- name: json_file_generator
platform: file
filename: generated_extract.json
timestamp: false
Read file back in HA Remove first two lines from "generated_extract.json", they contain data generated by the Notification process: 'Assistant' and '---'
grep -vE "Assistant|----" generated_json.yaml
Example import
sensor:
- platform: command_line
name: forecast_home
scan_interval: 30000
command: >
echo "$( grep -vE "Assistant|----" generated_extract.json | jq '.' ) "
value_template: "OK"
json_attributes:
- forecast