Date calculation error

Hello,

I am struggling to get the correct date from a sensor.
Mayby someone here can help me out?

The sensor is a scrape sensor the output is : Donderdag 8 februari

How can i convert this in a timestamp so it looks like 08-02-2018?

Thanks!
Sjef

What are you trying to do with the date?

Hi @petro,
I found a way to get the date i want by using a rest sensor instead of a scrape sensor to get the correct json value.

now i want to calculate with the date like this :

'{{ now().strftime("%Y-%m-%d") == (as_timestamp(states.sensor.afvalkalender_datum.state) - (24*3600)) | timestamp_custom("%Y-%m-%d") }}'

But i get this error when i use the (24*3600) in the automation :

Feb 06 18:02:28 HomeAssistant hass[520]: 2018-02-06 18:02:28 ERROR (MainThread) [aiohttp.server] Error handling request
Feb 06 18:02:28 HomeAssistant hass[520]: Traceback (most recent call last):
Feb 06 18:02:28 HomeAssistant hass[520]: File “/srv/homeassistant/lib/python3.5/site-packages/aiohttp/web_protocol.py”, line 410, in start
Feb 06 18:02:28 HomeAssistant hass[520]: resp = yield from self._request_handler(request)
Feb 06 18:02:28 HomeAssistant hass[520]: File “/srv/homeassistant/lib/python3.5/site-packages/aiohttp/web.py”, line 325, in _handle
Feb 06 18:02:28 HomeAssistant hass[520]: resp = yield from handler(request)
Feb 06 18:02:28 HomeAssistant hass[520]: File “/srv/homeassistant/lib/python3.5/site-packages/aiohttp/web_middlewares.py”, line 93, in impl
Feb 06 18:02:28 HomeAssistant hass[520]: return (yield from handler(request))
Feb 06 18:02:28 HomeAssistant hass[520]: File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/http/ban.py”, line 58, in ban_middleware
Feb 06 18:02:28 HomeAssistant hass[520]: return (yield from handler(request))
Feb 06 18:02:28 HomeAssistant hass[520]: File “/usr/lib/python3.5/asyncio/coroutines.py”, line 213, in coro
Feb 06 18:02:28 HomeAssistant hass[520]: res = yield from res
Feb 06 18:02:28 HomeAssistant hass[520]: File “/usr/lib/python3.5/asyncio/coroutines.py”, line 213, in coro
Feb 06 18:02:28 HomeAssistant hass[520]: res = yield from res
Feb 06 18:02:28 HomeAssistant hass[520]: File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/http/init.py”, line 430, in handle
Feb 06 18:02:28 HomeAssistant hass[520]: result = yield from result
Feb 06 18:02:28 HomeAssistant hass[520]: File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/api.py”, line 354, in post
Feb 06 18:02:28 HomeAssistant hass[520]: return tpl.async_render(data.get(‘variables’))
Feb 06 18:02:28 HomeAssistant hass[520]: File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/template.py”, line 131, in async_render
Feb 06 18:02:28 HomeAssistant hass[520]: return self._compiled.render(kwargs).strip()
Feb 06 18:02:28 HomeAssistant hass[520]: File “/srv/homeassistant/lib/python3.5/site-packages/jinja2/environment.py”, line 1008, in render
Feb 06 18:02:28 HomeAssistant hass[520]: return self.environment.handle_exception(exc_info, True)
Feb 06 18:02:28 HomeAssistant hass[520]: File “/srv/homeassistant/lib/python3.5/site-packages/jinja2/environment.py”, line 780, in handle_exception
Feb 06 18:02:28 HomeAssistant hass[520]: reraise(exc_type, exc_value, tb)
Feb 06 18:02:28 HomeAssistant hass[520]: File “/srv/homeassistant/lib/python3.5/site-packages/jinja2/_compat.py”, line 37, in reraise
Feb 06 18:02:28 HomeAssistant hass[520]: raise value.with_traceback(tb)
Feb 06 18:02:28 HomeAssistant hass[520]: File “”, line 8, in top-level template code
Feb 06 18:02:28 HomeAssistant hass[520]: TypeError: unsupported operand type(s) for -: ‘str’ and ‘float’

Found the solution
i used this sensor :

- platform: rest
  name: afvalkalender_datum
  resource: http://inzamelkalender.hvcgroep.nl/push/calendar?postcode=1111XX&huisnummer=999
  value_template: '{{ (value_json.0.dateTime.0.date) }}'

before i used this value template :

  value_template: '{{ as_timestamp(value_json.0.dateTime.0.date) | timestamp_custom("%d-%m-%Y") }}'

Below the value_template to calculate the day to yesterday so i can use it in a automation:

  value_template: '{{ now().strftime("%Y-%m-%d") == (as_timestamp(states.sensor.afvalkalender_datum.state) - (86400)) | timestamp_custom("%Y-%m-%d") }}'

The full automation looks like this:

  • id: Rule_7_Trash
    alias: ‘Rule 7 Trash’
    trigger:
    • platform: time
      hours: 18
      minutes: 00
      seconds: 0
      condition:
    • condition: state
      entity_id: input_boolean.notify_trash
      state: ‘on’
    • condition: template
      value_template: ‘{{ now().strftime(“%Y-%m-%d”) == (as_timestamp(states.sensor.afvalkalender_datum.state) - (86400)) | timestamp_custom(“%Y-%m-%d”) }}’
      action:
    • service: notify.pushover
      data_template:
      message: ‘Het is vandaag - {{ now().strftime(“%Y-%m-%d”) }}. De {{ states.sensor.afvalkalender_type.state }} afvalbak wordt morgen geleegd!.’
      data:
      title: Trash!
      target:
      • “phone2”
      • “phone1”