Replace value_json in templates with filters #811

wants to merge 14 commits into


None yet

4 participants

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.

mcdeck added some commits Dec 28, 2015
@mcdeck mcdeck Initial version of custom filter support to replace json_value in tem…
@mcdeck mcdeck Merge branch 'dev' of into dev a889748
@mcdeck mcdeck Simplify custom_filter decorator
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.

balloob and others added some commits Jan 16, 2016
@balloob balloob Merge pull request #883 from balloob/dev
@molobrakos @balloob molobrakos fix issue where sensors and switches were duplicated because of compo…
…nent getting initialized twice. closes #913
@balloob balloob Enforce entity attribute types cbb74d5
@balloob balloob Fix MQTT reconnecting 1ceee2d
@balloob balloob Allow forcing MQTT protocol v3.1
@balloob balloob Version bump to 0.11.1
@MartinHjelmare @balloob MartinHjelmare Fix missing binary sensor types
* Add missing binary sensor types to sensor/mysensors.
* Remove unneeded pylint disable.
@balloob balloob Merge pull request #935 from balloob/0.11.1
@mcdeck mcdeck Merge branch 'master' of in…
…to dev
@mcdeck mcdeck Merge branch 'dev' of into dev
@mcdeck mcdeck remove filters directory, move tag to template module
mcdeck commented Jan 25, 2016

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

@mcdeck mcdeck closed this Jan 25, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment