I am trying to get json data into a sensor - state and an attribute .. similar to nordpool but from my own script but I have not been able to get the file or rest sensors to work - the file sensor there is no sensor created and rest sensor no values in the sensor even after updating.
I would appreciate some help to get this working!
json is in www, example of json is below, I checked it with a tool I found in other places and it seems to checkout as well as the path (https://jsonpath.com/)
what I did with file sensor (no sensor ever spawned):
what I did last with rest sensor - sensor spawns but no values are being written (even after updates)
sensor:
- platform: rest
name: extrema_py
resource: http://homeassistant.local:8123/local/extrema_py.json
unit_of_measurement: kWh
value_template: "{{ value_json[0].state }}"
scan_interval: 2592000 # = 30 days x 24hr x 60min x 60s more or less never - will be activated by automation
json_attributes_path: "$"
json_attributes:
- data
I think File has been completely moved into the UI... the integration docs no longer contain any YAML config examples for File sensors. Based on the docs history and this PR it looks like that has been the case for nearly 2 years... and, from what I can see, there is no evidence that json_attributes was supported as a configuration variable when last they could be configured via YAML.
The following example was tested on my system and produced a sensor containing JSON data from a file.
It's a Trigger-based Template Sensor that employs the file.read_file action to get the contents of /config/www/extrema_py.json. It triggers at 00:00:00 and whenever Template entities are reloaded. It can be changed to trigger whenever you prefer.
note: not sure why, but I had to change triggers to trigger and actions to action in order to get through the yaml check, but after these changes and restarting it worked great!
i thought template sensors loaded if one for example through actions did reloaded templates, I suppose you included this as they do not reload, right? or is there some other reason? (doc i found which is a bit unclear to me : Template - Home Assistant )
@ Didgeridrew I understand file sensor method is likely not working, I also noticed the docs were thin.. but found a number of resources explaining what I did should have worked. thanks for confirming!
@all would be nice still find a solution with the rest method as well, as it seems it should be possible as well, but I am happy with the template sensor!!
That is an Event Trigger for triggering the Trigger-based Template Sensor whenever Template entities are reloaded. It is entirely optional; feel free to remove it and use whatever triggers are needed for your application.
Try the following example. I tested it on my system and it reported the state and the data (as an attribute).
The Trigger-based Template Sensor provides more fine-grained control over when the sensor should update its values. The REST Sensor is strictly based on seconds elapsed since it was loaded.
ahh. it was in vscode the extension complained.. and i see now it anyway worked with triggers or trigger.
regarding the rest sensor. I have tested it now and am not getting it working actually. You have the file in www right? I triggered it through action rest.reload .. but maybe that is not working as I thought it should?
regarding template sensors - I wonder, if maybe there is a way to trigger an individual template sensor directly through an action? So far I just create a button helper that i reference as a trigger and then "press the button" in automations - i guess there is a better way?
I have read loads of docs and have not seen what I meant - I meant more like in an automation to trigger this template sensor (eg taras's above) like with an action in the automation gui to trigger template sensors with a target where i could set to this template sensor and it would just trigger.
is there such an action (without creating a button that when pressed will trigger this template sensor)?
You can change the state of the entity used in the template of your sensor under the states tab of the dev tools. Keep in mind, when that sensor again updates your value will be overridden.
ahh.. sorry! I did not understand it and really didn't understand a custom event was a manual event! I am embarrassed to say how many times I have searched and not found any docs and thought I will figure it out later .. today seems to be later. thank you very much!
any idea why the names are not the same or why "fire manual event" is not found in the docs? I would humbly request that it just says in that section "This can be done in the GUI via 'fire manual event'" (if someone should see this that can modify this as it seems I can not suggest it myself via github?)
With this insight I think I am getting it - I guess I would trigger an event with "fire manual event" i think something like this: