Help please - Trigger from a time sensor

I am trying to turn on my hotplate based on a time before candle lighting as set in the sensor “jewish_calendar_upcoming_candle_lighting”.

I have tested the trigger code using the “Developer Tools” and the logic transitions from false to true correctly, but when I paste the code into the automation trigger it doesn’t want to trigger.

Any suggestions please as to how to get it to trigger.

Thanks

Mike Paneth

alias: Hotplate ON night
description: Turn hotplate ON at night. Time is set to 3hr before candle
trigger:
  - platform: template
    value_template: >
      "{% set ho = 3 %}
      {(as_timestamp(states('sensor.jewish_calendar_upcoming_candle_lighting')))|int
      - (ho*60*60) < as_timestamp(utcnow())|int}}"
condition: []
action:
  - type: turn_on
    device_id: 837b83506685d815c22c13df77f30eb9
    entity_id: switch.hot1
    domain: switch
  - type: turn_on
    device_id: 837b83506685d815c22c13df77f30eb9
    entity_id: switch.hot1_led
    domain: switch
mode: single

remove the quotes before and after the template:

trigger:
  - platform: template
    value_template: >
      {% set ho = 3 %}
      {(as_timestamp(states('sensor.jewish_calendar_upcoming_candle_lighting')))|int
      - (ho*60*60) < as_timestamp(utcnow())|int}}

the quotes are only required for a single line template. the > symbol denotes a multi-line template so the quotes aren’t used.

Doesn’t want to trigger.

I copied the template into the Developer tools and substituted a timestamp of 60 seconds from now (in place of ho * 60 * 60) and the code executed correctly (false -> true).
However when I did the same experiment by plugging in a new timestamp into both the automation and Developer tools it again executed correctly in Developer tools but did not trigger the automation?

There is no error in the logs.

Any further ideas??

Thanks
Mike
Melbourne Australia

Sorry.

I just had an error come into the log,

Logger: homeassistant
Source: components/template/trigger.py:55
First occurred: 12:32:00 PM (1 occurrences)
Last logged: 12:32:00 PM

Error doing job: Exception in callback async_track_point_in_utc_time.<locals>.run_action() at /usr/src/homeassistant/homeassistant/helpers/event.py:1137
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/asyncio/events.py", line 81, in _run
    self._context.run(self._callback, *self._args)
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 1156, in run_action
    hass.async_run_hass_job(job, utc_point_in_time)
  File "/usr/src/homeassistant/homeassistant/core.py", line 433, in async_run_hass_job
    hassjob.target(*args)
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 1362, in pattern_time_change_listener
    hass.async_run_hass_job(job, dt_util.as_local(now) if local else now)
  File "/usr/src/homeassistant/homeassistant/core.py", line 433, in async_run_hass_job
    hassjob.target(*args)
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 811, in _refresh_from_time
    self._refresh(None, track_templates=track_templates)
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 960, in _refresh
    self.hass.async_run_hass_job(self._job, event, updates)
  File "/usr/src/homeassistant/homeassistant/core.py", line 433, in async_run_hass_job
    hassjob.target(*args)
  File "/usr/src/homeassistant/homeassistant/components/template/trigger.py", line 55, in template_listener
    entity_id = event.data.get("entity_id")
AttributeError: 'NoneType' object has no attribute 'data'

Mike

I just saw that you are missing a ‘{’ in the template.

trigger:
  - platform: template
    value_template: >
      {% set ho = 3 %}
      {{ (as_timestamp(states('sensor.jewish_calendar_upcoming_candle_lighting')))|int
      - (ho*60*60) < as_timestamp(utcnow())|int }}

Thanks. Corrected mistake but still same error ??!!

Mike

What is the state of the ‘sensor.jewish_calendar_upcoming_candle_lighting’?

Attributes
friendly_name: Jewish Calendar Upcoming Candle Lighting
icon: mdi:candle
device_class: timestamp

states(‘sensor.jewish_calendar_upcoming_candle_lighting’) = 2021-01-01 09:26:00+00:00

as_timestamp (states(‘sensor.jewish_calendar_upcoming_candle_lighting’))|int = 1609493160

So I use
as_timestamp(…) as the evening time
less several hours (ho6060)
and when current time is greater (or later) trigger the automation

Mike

I just created the following automation in my set up:

- alias: Hotplate ON night
    description: Turn hotplate ON at night. Time is set to 3hr before candle
    trigger:
      - platform: template
        value_template: >
          {% set update = states('sensor.one_second_update') %}
          {% set ho = 3 %}
          {{(as_timestamp(states('input_datetime.both_date_and_time')))|int - (ho*60) < as_timestamp(now())|int}}
    condition: []
    action:
      - service: input_boolean.turn_on
        entity_id: input_boolean.test
    mode: single

And it worked with no errors.

Obviously, I don’t have your entities so I needed to use my own but the automation syntax is basically the same. I changed the multiplier because I didn’t want to wait hours for it to switch.

The only thing I added was a one second update. If you use now() it will only ever update no more frequently than one minute intervals. So using only that it could be up to 59 seconds off on when the automation triggers.

But I didn’t get the same error you did so there must be something missing from your system that the template is looking for (as hinted at by the “Nonetype” error).

Spent most of the day trying variations but still can’t get it working.

Is there any way to dig deeper into the error message?

Mike

not really.

The error messages are notoriously vague and unreadable to guide you to the issues.

There has been many request to improve thing but nothing has been done so far.

What integration are you using to get the candle lighting time or is it a custom sensor you created?

If I can get that same entity I can try playing around with it on my end and see what I can do.

The integration is Jewish Calendar.

My settings are

jewish_calendar:
  language: hebrew
  diaspora: true
  havdalah_minutes_after_sunset: 0

Mike

Just an FYI, I’m still trying to figure out why this isn’t working.

I get the same error you are getting when you run it. But I haven’t been able to figure out why. Or how to work around it.

It really makes no sense.

Thanks for trying.

I sent an email to the developer and am still waiting a response.

Is there any way to get a better understanding of what the error message means?

Happy New Year
Mike

1 Like

After testing this every way I could think of I’m not sure that its an issue with the integration. I honestly think it might be an issue with HA.

I completely took out the candle sensor and put in a hard coded date timestamp and I still get the same error with the following template:

{{(1609452060 - (61200 + 175)) < as_timestamp(now())}}

It all seems related to the now() function.

but it all works fine in the template editor.

Thanks for your perserverence.

Will you log a bug?

While playing around I see that while now() gives a different result to utcnow() depending on your timezone. However as_timestamp(now()) returns the same value as as_timestamp(utcnow()).

Is this meant to be correct?

Mike

interestingly this has been a known issue for a while. And I had commented on the bug report a few weeks ago but completely forgot about it (and honestly assumed it would have been fixed) until I got a notification this morning from the github issue that there has been activity there. Sucks getting old… :wink:

I saw the same thing and I assume it’s correct but I would have to investigate further to see exactly why.

Time functions have a bunch of little intracacies and it’s possible that the “as_timestamp” function has a directive that always assumes your local timezone in the conversion unless specified otherwise.

I upgraded to 2020.12.2 and the automation now works correctly.

Thank you for your efforts in helping me.

Mike

thanks for that info.

I guess I need to update now. I am running 12.1.