Output json file

Is it possible to export the status of a sensor at a certain time every day to a json file?

It is bound to be possible. It could be as simple as using a file notification.

You would have to format your message correctly but easy with the file notification as said above. I export to CSV.

But I am saying save the status of a sensor in a json file.

Is this an existing file? Or are we creating a new file. Give an example of the file you want to create. It should be doable.

yes, it can be done, but it makes me a plain text, not a json

You would need to specify the file extension you want when you set up the notification. As shown above, I specified .csv in my setup.

also, you are going to need to specify the format within the file that you want.

If I specify a json file it saves it to me in json, but as plain text

/config/file.json

but that way is not saved in json obviously

How do you specify the format for json in the notify and save it like this?

With this command it works perfectly from the console:

jq '''."2019"."11"."19" = "0.15"''' /media/consumo.json | sponge /media/consumo.json
{
	"2019": {
		"11": {
			"09": "abcde",
			"19": "0.15"
		}
	},
	"2020": {
		"11": {
			"09": "abcde",
			"10": "abcde"
		}
	}
}

But taking that command to homeassistant with template does not do well, it returns an empty json

[email protected] jq '''."{{ now().strftime('%Y') }}"."{{ now().strftime('%m') }}"."{{ now().strftime('%d') }}" = "{{ states('sensor.energy_daily') }}"''' /media/consumo.json | sponge /media/consumo.json

What does this sensor’s state value look like?
You know JSON is just text, right? What makes this file not JSON? Or are we not understanding the problem?

Same as above

 jq '''."2019"."11"."19" = "0.15"''' /media/consumo.json | sponge /media/consumo.json

Ah, my mistake, I thought that was some other command line utility you were running to product some JSON data. So that is literally what is stored in the sensor state? How are we expecting to get JSON out of that? Is that a command line that is supposed to be run to produce JSON?

The idea is to use the jq tool to edit a json file with a data taken from a sensor, by console it works, but since homeassistant launching a shell command no.

Can’t say I’m certain why this isn’t working, but I have two guesses:

  1. Fully qualify the paths of those utilities; perhaps the hass user can’t find them.
  2. Depending on how your YAML is defined, it could be a quoting issue. There are plenty of single and double quotation marks in that line, and there’s plenty of opportunity for something to go awry.

You may want to consult this guide for how to quote and escape characters in YAML.
http://blogs.perl.org/users/tinita/2018/03/strings-in-yaml---to-quote-or-not-to-quote.html

Whitelist the /media directory?

This is a very long work-around, but if you don’t mind using an external python script, you can try this script that I use for similar purposes. I have modified it a bit to fit your purpose. https://pastebin.com/NkeBmHpY. It need to be in your config folder.

You can then call if from a shell command like:

shell_command:
  save_state: python3 save_it.py sun.sun my_file.json

This would save the state of the sun component in json format to a file in the config folder.

The file output would be:

{"attributes": {"azimuth": 326.62, "elevation": -47.16, "friendly_name": "Solen", "next_dawn": "2019-11-20T06:00:59+00:00", "next_dusk": "2019-11-20T15:05:28+00:00", "next_midnight": "2019-11-19T22:33:21+00:00", "next_noon": "2019-11-20T10:33:13+00:00", "next_rising": "2019-11-20T06:49:21+00:00", "next_setting": "2019-11-20T14:17:06+00:00", "rising": false}, "context": {"id": "xxx", "parent_id": null, "user_id": null}, "entity_id": "sun.sun", "last_changed": "2019-11-19T20:59:44.037338+00:00", "last_updated": "2019-11-19T20:59:44.037338+00:00", "state": "below_horizon"}

I use the script when I need to get or set a state from a python script located on another machine.

Hi, I think that doesn’t work for me because I use https and I think you have to put some option to avoid ssl, with curl it is -k, but I don’t know what it would be like in python.

It should work by just changing

response = requests.get(API_URL + sensor_name, headers=HEADERS)

to

response = requests.get(API_URL + sensor_name, headers=HEADERS, verify=False)

This would give you a warning but the request should work.

@mig77angel just going back to your initial script, home assistant doesn’t usually run as root, so can the user running homeassistant write to the /media/consumo.json?