This works in template editor :
{% set ENERGY = "{{value_json['ENERGY'].Total}}" %}
{{ ENERGY }}
{"name":"powr2 ENERGY Total","stat_t":"~SENSOR","avty_t":"~LWT","pl_avail":"Online","pl_not_avail":"Offline","uniq_id":"193EF5_ENERGY_Total","device":{"identifiers":["193EF5"]},"~":"powr2/tele/","unit_of_meas":"kWh","last_reset_topic":"powr2/tele/","last_reset_value_template":"1970-01-01T00:00:00+00:00","device_class":"energy","state_class":"measurement","val_tpl":"{{ENERGY}}"}
So I try this script :
scripttasmotapow2:
sequence:
- service: mqtt.publish
data:
retain: true
topic: homeassistant/sensor/193EF5_ENERGY_Total/config
payload_template: >
{% set ENERGY = "{{value_json['ENERGY'].Total}}" %}
'{"name":"powr2 ENERGY Total","stat_t":"~SENSOR","avty_t":"~LWT","pl_avail":"Online","pl_not_avail":"Offline","uniq_id":"193EF5_ENERGY_Total","device":{"identifiers":["193EF5"]},"~":"powr2/tele/","unit_of_meas":"kWh","last_reset_topic":"powr2/tele/","last_reset_value_template":"1970-01-01T00:00:00+00:00","device_class":"energy","state_class":"measurement","val_tpl":"{{ENERGY}}"}'
and I get this error :
Logger: homeassistant.components.websocket_api.http.connection
Source: helpers/template.py:395
Integration: Home Assistant WebSocket API (documentation, issues)
First occurred: 09:43:43 (2 occurrences)
Last logged: 09:56:43
[1665545936] UndefinedError: 'value_json' is undefined
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 393, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1398, in _render_with_context
return template.render(**kwargs)
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1304, in render
self.environment.handle_exception()
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 925, in handle_exception
raise rewrite_traceback_stack(source=source)
File "<template>", line 1, in top-level template code
File "/usr/local/lib/python3.9/site-packages/jinja2/sandbox.py", line 303, in getitem
return obj[argument]
jinja2.exceptions.UndefinedError: 'value_json' is undefined
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 185, in handle_call_service
await hass.services.async_call(
File "/usr/src/homeassistant/homeassistant/core.py", line 1491, in async_call
task.result()
File "/usr/src/homeassistant/homeassistant/core.py", line 1526, in _execute_service
await handler.job.target(service_call)
File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 278, in service_handler
await script_entity.async_turn_on(
File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 382, in async_turn_on
await coro
File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 409, in _async_run
return await self.script.async_run(script_vars, context)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1219, in async_run
await asyncio.shield(run.async_run())
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 353, in async_run
await self._async_step(log_exceptions=False)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 371, in _async_step
await getattr(self, handler)()
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 571, in _async_call_service_step
await service_task
File "/usr/src/homeassistant/homeassistant/core.py", line 1491, in async_call
task.result()
File "/usr/src/homeassistant/homeassistant/core.py", line 1526, in _execute_service
await handler.job.target(service_call)
File "/usr/src/homeassistant/homeassistant/components/mqtt/__init__.py", line 495, in async_publish_service
payload = template.Template(payload_template, hass).async_render(
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 395, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'value_json' is undefined