Automation question: trigger mqtt on device_tracker.state change works - still seeing an exception

Hi,

the following automation works for me, but I’d be happy to get with two remaining questions :wink:

  • Is there an easy way to get JAN-11 as ‘01-11’ instead of ‘1-11’?
  • I see an exception in the log and have trouble to understand the root cause. I’m suprised that the automation still seems to work as expected.

MQTT topics producted by the automation:

sensor/presence/alex : {"VALUE": "0", "TS": "2019-1-11 11:1:15" }  
sensor/presence/alex : {"VALUE": "1", "TS": "2019-1-11 12:0:38" }

Exception:

2019-01-11 12:00:40 ERROR (MainThread) [homeassistant.core] Error executing service <ServiceCall notify.ha_email (c:76bbfc0527cf43e18123ac3592d5843a): title=<homeassistant.helpers.template.Template object at 0xaec5ec90>, message=<homeassistant.helpers.template.Template object at 0xaec5e2d0>, data=images=['/usr/lib/python3.6/site-packages/homeassistant/components/camera/demo_0.jpg']>
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/template.py", line 132, in async_render
    return self._compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.6/site-packages/jinja2/sandbox.py", line 385, in getattr
    value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'state' is undefined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1177, in _event_to_service_call
    await service_handler.func(service_call)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/notify/__init__.py", line 114, in async_notify_message
    kwargs[ATTR_MESSAGE] = message.async_render()
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/template.py", line 134, in async_render
    raise TemplateError(err)
homeassistant.exceptions.TemplateError: UndefinedError: 'state' is undefined

Automation:

  ########################################
  - alias: 'Publish presence via mqtt - alex'
  ########################################
    trigger:
      platform: state
      entity_id: device_tracker.alex
    action:
      - service: mqtt.publish
        data:
          topic: "sensor/presence/alex"
          retain: 'true'
          payload_template: '{"VALUE": "{% if is_state("device_tracker.alex", "home") %}1{% else %}0{% endif %}", "TS": "{{now().year}}-{{now().month}}-{{now().day}} {{now().hour}}:{{now().minute}}:{{now().second}}" }'

It probably has something to do with the second lot of double quotation marks.

'{"VALUE": "{% if is_state("....
string     ^start          ^end

You could try like this and see if it helps:

'{"VALUE": "{% if is_state('device_tracker.alex', 'home') %}1{% else %}0{% endif %}", "TS": "{{now().year}}-{{now().month}}-{{now().day}} {{now().hour}}:{{now().minute}}:{{now().second}}" }'

Ok, understood the automation error, my bad… (“notify.ha_email”) The presence event triggers another automoation which contained the issue. Above automation is correct.

So - question #1 might be even easier - any alternative to now().minute that always returns two digits?

:slight_smile: Alex

PS: Thanks tom_i. I don’t think that is an issue, as the mqtt messages created contain the right content. However, I agree that quotations seem difficult to me, in your example the embedding would be interrupted by the quotations around device_tracker.alex

Yeah, you’re right. Man that is one mess of quotation nesting.

Your other problem: Look for the zero padded options here: http://strftime.org/

e.g. {{ now().strftime('%M') }}

1 Like

All solved - thanks!