Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace value_json in templates with filters #811

Closed
wants to merge 14 commits into from
Closed

Replace value_json in templates with filters #811

wants to merge 14 commits into from

Conversation

mcdeck
Copy link
Contributor

@mcdeck mcdeck commented Dec 28, 2015

When template support got implemented to replace the json parsing I originally created for the mqtt component I did like the idea of the template approach. However, I disliked the json support because it basically tries to parse every message as json. Supporting other message formats would require additional value_XXX keys in the templates and that seems quite inflexible.

With templates however we can use filters and I figured I'd give that a try to see if this wouldn't be a nicer implementation of json support in messages. I have now implemented two filters json and jsonpath. The idea is to create a set of filters ready for use but also find a way to allow for custom filters created by users (e.g. in their configuration directories maybe?)
With the two filters in this PR you can do something like this in your config:

value_template: "{{ (value|json).val }}"
value_template: "{{ value|jsonpath('val') }}"

This pull request is meant to illustrate the idea - the implementation is for sure far from perfect and is missing tests, but I first wanted to collect feedback to see if investing more time into this made sense or not.

@balloob
Copy link
Member

balloob commented Dec 29, 2015

Adding a way to add custom filters sounds like a good plan. It will allow new components to register filters too. The annotation should probably just live in the template package, no need for an extra file.

The reason I went for the value_json approach instead of value | json is simplicity. Imagine we're trying to extract the value 'hello' from [{"hello": "world"}]:

{{ (value | json).0.hello }}
{{ value_json.0.hello }}

I wouldn't want to add support for other formats but JSON is common enough.

I would not want to add the jsonpath filter. There should be only 1 way to do something and using jsonpath offers the same as a template.

@mcdeck
Copy link
Contributor Author

mcdeck commented Jan 25, 2016

I will create a new PR once I have a working solution

@mcdeck mcdeck closed this Jan 25, 2016
@home-assistant home-assistant locked and limited conversation to collaborators Mar 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants