I need to define a template sensor which monitors a json file. Previously I’d simply define it in yaml file and create a sensor to associate with it.
But as this is deprecated, how do I create a helper template sensor to monitor and read a value from a json file?
The file sensor only reads the last line of a file. Is that any use?
If not, can you receive the json directly from whatever creates the file instead?
If so then use the restful sensor:
This is an example that reads from an API:
- triggers:
- trigger: time_pattern
minutes: /10
actions:
- action: rest_command.get_filaments
response_variable: get_filaments_response
sensor:
- name: Filaments list
unique_id: filaments_list
state: >
{% if get_filaments_response["status"] != 200 %}
unavailable
{% else %}
{{ now() }}
{% endif %}
attributes:
payload: '{{ get_filaments_response["content"] }}'
For a file, you could replace the rest_command by a command_line that does a plain cat my_file.json (rest_command might support the file:/// protocol, but not sure)
I got the impression that File had been deprecated as I had to run a Repair recently and remove the file sensors from configuration.yaml. I’ve used a command_line now instead, still editing configuration.yaml, and that seems to be acceptable to HA.
No it has not been depreciated.
It was removed from YAML IIRC, that’s what they are likely thinking about.
Yes probably this which triggered the Repair. But I’ve not found a way of continuing to use File within the gui.
So is adding command_line into configuration.yaml the correct solution or will that also be deprecated next?
Ah ha brilliant thanks. I hadn’t appreciated that it was now achieved using this. I’ll remove the command_line from configuration.yaml and use this File integration instead.
Thanks
I mean, if you want yaml and command_line works, no reason to change it. IMO
They added an action a couple months ago so you can get the file contents returned in a response variable. It’s not limited in the last line like the sensor.
Nice. I somehow missed or forgot that update.
