openhab / openhab-addons

Add-ons for openHAB
https://www.openhab.org/
Eclipse Public License 2.0
1.88k stars 3.59k forks source link

[mqtt.homeassistant] Add Home Assistant Specific Jinja extensions #15382

Open danieldemus opened 1 year ago

danieldemus commented 1 year ago

I have been trying to set up an AMS reader to submit power readings to openhab via mosquitto.

AMS Reader can provide Home Assistant discovery. Unfortunately all the discovered channels contain a value template along the lines of "val_tpl":"{{ value_json.P3 | is_defined }}". "is_defined" is a HA extension to jinja, so all channels fail when receiving data with: "Executing the JINJA-transformation failed: An error occurred while transformation. UnknownTokenException: Unknown token found: value_json.P3 | is_defined".

There is a list of these extensions at https://www.home-assistant.io/docs/configuration/templating/.

To be able to implement these extensions in the mqtt.homeassistant binding, you need to be able to add java implementations of jinja extensions.

jinjava supports adding custom extensions that implement given interfaces on the Context class, so the main thing missing is a facility in the JinjaTransformationService, or similar place, to read something from f.x. feature.xml for a binding and instantiate and register extensions when constructing the service.

Your Environment

So far this is a somewhat nebulous request submitted to start a discussion.

ssalonen commented 5 months ago

This would be a nice addition. OpenMQTTGateway current works around this issue by having separate compatibility mode for openHAB:

https://github.com/1technophile/OpenMQTTGateway/blob/2645928dbdc4033ce85abb506789a515052ea35a/main/ZmqttDiscovery.ino#L298

(More info https://github.com/1technophile/OpenMQTTGateway/pull/1658)