2020-09-21 01:58:32 +02:00
# Jinja Transformation Service
2024-09-28 17:46:53 +02:00
Transforms a value using a jinja template.
2020-09-21 01:58:32 +02:00
The main purpose of this transformer is the use in the home assistant discovery. Therfore not all features of the home assistant templating are supported.
Basically on [Processing incoming data ](https://www.home-assistant.io/docs/configuration/templating/#processing-incoming-data )
## Available variables
| Variable | Description |
|------------|------------------------------------|
| value | The incoming value. |
| value_json | The incoming value parsed as JSON. |
## Examples
2022-02-05 20:44:45 +01:00
### Basic Examples
#### Incoming data
2020-09-21 01:58:32 +02:00
Given the value
2022-02-05 20:44:45 +01:00
```json
2020-09-21 01:58:32 +02:00
{"Time":"2019-01-05T22:45:12","AM2301":{"Temperature":4.7,"Humidity":93.7},"TempUnit":"C"}
```
the template
2022-02-05 20:44:45 +01:00
```
{{value_json['AM2301'].Temperature}}`
```
2020-09-21 01:58:32 +02:00
extracts the string `4.7` .
2022-02-05 20:44:45 +01:00
#### Outgoing data
2023-01-09 17:51:12 +01:00
2022-02-05 20:44:45 +01:00
The JINJA transformation can be used to publish simple JSON strings through, for example, the HTTP Binding's `commandTransformation` parameter.
2022-12-01 14:30:50 +01:00
Say we have a String Item which holds the following value:
2022-02-05 20:44:45 +01:00
```
This is my string
```
Adding the following into the `commandTransformation` parameter of your HTTP Thing Channel
```
JINJA:{"msgtype":"m.text", "body":"{{value}}"}
```
will send the following string out of openHAB
```json
{"msgtype":"m.text", "body":"This is my string"}
```
2023-01-09 17:51:12 +01:00
2022-02-05 20:44:45 +01:00
`{{value}}` will be replaced by whatever the value of your Item is.
Note that if using \*.things files you must escape quotation marks, for example:
```
commandTransformation = "JINJA:{\"msgtype\":\"m.text\", \"body\":\"{{value}}\"}"
```
2020-09-21 01:58:32 +02:00
## Further Reading
2024-09-28 17:46:53 +02:00
- Wikipedia on [Jinja ](https://en.wikipedia.org/wiki/Jinja_(template_engine ).
- Home assistant [discovery ](https://www.home-assistant.io/docs/mqtt/discovery/ ).
- Home assistant [templating ](https://www.home-assistant.io/docs/configuration/templating/ ).