Timestamp into string variable

Hello,

I’m trying to add a timestamp to the camera.snapshots and send the image to a persistent notification. I would like to define the timestamp only once and put the timestamp in a string variable. Afterwards I would like to reuse that variable to send out the persistent notification.

When doing this I will have a unique photoname and it will not be subject on browsers image cache. I tried using the data_template within the persistent notification but I did not get it working.

The best thing I have right now is creating two camera snapshots, one for just archiving and one to use with the persistent notification.

Any help to get it working with defining the timestamp into a variable and re-using it in the persistent notication is welcome.

another working approach is also welcome

thanks in advance

stijn

- id: '1574603291945'
  alias: '[doorbell] push actions'
  description: ''
  trigger:
  - event_data:
      address: 1/5/4
      data: 1
    event_type: knx_event
    platform: event
  condition: []
  action:
  - data:
      message: Er is iemand aan de deur.
      title: Deurbel
    service: notify.notify
  - data:
      deviceID:
      - ipad_living
      entity_id: camera.cam_voordeur
      large: true
    service: browser_mod.more_info
  - data:
      filename: /config/www/camera/{{ now().strftime("%Y%m%d_%H%M%S") }}_snap_voordeur.jpg
    entity_id: camera.cam_voordeur
    service: camera.snapshot
  - delay: 00:00:02
  - data:
      filename: /config/www/camera/snap_voordeur.jpg
    entity_id: camera.cam_voordeur
    service: camera.snapshot
  - data:
      message: 'Er is iemand aan de deur: [foto voordeur](https://xxxxxxxxxxxxxxxxxxxxxxxxxxx.ui.nabu.casa/local/camera/snap_voordeur.jpg?{{now().second}})'
      title: Deurbel
    service: persistent_notification.create
  - delay: 00:01:00
  - service: browser_mod.close_popup

you can set value of input_datetime and use it for generating timestamps (as I can see you use word timestamp to describe a string representation of date/time, is that correct)?

I just worked up a test automation and used an input text to save the timestamp and it works great for me. As a matter of fact I think I’m going to save it for my own use now. I’ve already got a couple of camera snapshot automations but I’ve been wanting to do something like this for a while but just kept putting it off.

Thanks for giving me the nudge! :slightly_smiling_face:

Here is my automation I used using my own entities and english :wink: But I think you can adapt it back to your own stuff easily enough:

input_text:
  deck_snap_timestamp:
    name: Timestamp for Deck Camera Snapshot test
  
automation:
  - alias: 'gatorade button push test snapshot actions'
    trigger:
      - platform: event
        event_type: gatorade_button_pressed
    action:
      - data:
          message: Someone is in the CR
          title: Gatorade Button Pressed
        service: notify.pushbullet_notify
      # - data:
          # deviceID:
            # - ipad_living
          # entity_id: camera.cam_voordeur
          # large: true
        # service: browser_mod.more_info
      - service: input_text.set_value
        entity_id: input_text.deck_snap_timestamp
        data_template: 
          value: '{{ now().strftime("%Y%m%d_%H%M%S") }}'
      - data:
          filename: /config/www/snapshots/{{ states.input_text.deck_snap_timestamp.state }}_deck.jpg
        entity_id: camera.deck
        service: camera.snapshot
      - delay: 00:00:02
      - data_template:
           message: 'Someone is in the CR: [photo deck](https://xxxxxxxxxxxxxxx.ui.nabu.casa/local/snapshots/{{ states.input_text.deck_snap_timestamp.state }}_deck.jpg)'
          title: Gatorade Button Pressed test
        service: persistent_notification.create
      # - delay: 00:01:00
      # - service: browser_mod.close_popup
1 Like

That is correct. Using an input_datetime instead of a string would be sufficient for what I’m trying to accomplish here.

Thank you finity, this was exactly what I was looking for. I didn’t understand how to use the data_template integration together with a camera snapshot. Now it is clear I need to use a different service for this.

Thank you very much for the example code and good luck with your own integration :slight_smile:

1 Like

I just noticed an error in my code above. I’m kind of surprised that it worked to be honest…

Here is the updated version with a comment on the updated line:

input_text:
  deck_snap_timestamp:
    name: Timestamp for Deck Camera Snapshot test
  
automation:
  - alias: 'gatorade button push test snapshot actions'
    trigger:
      - platform: event
        event_type: gatorade_button_pressed
    action:
      - data:
          message: Someone is in the CR
          title: Gatorade Button Pressed
        service: notify.pushbullet_notify
      # - data:
          # deviceID:
            # - ipad_living
          # entity_id: camera.cam_voordeur
          # large: true
        # service: browser_mod.more_info
      - service: input_text.set_value
        entity_id: input_text.deck_snap_timestamp
        data_template: 
          value: '{{ now().strftime("%Y%m%d_%H%M%S") }}'
      - data_template:     #<------- here is the updated code
          filename: /config/www/snapshots/{{ states.input_text.deck_snap_timestamp.state }}_deck.jpg
        entity_id: camera.deck
        service: camera.snapshot
      - delay: 00:00:02
      - data_template:
           message: 'Someone is in the CR: [photo deck](https://xxxxxxxxxxxxxxx.ui.nabu.casa/local/snapshots/{{ states.input_text.deck_snap_timestamp.state }}_deck.jpg)'
          title: Gatorade Button Pressed test
        service: persistent_notification.create
      # - delay: 00:01:00
      # - service: browser_mod.close_popup

It wasn’t easy but in the end: here is my working code:

thanks everybody!

input_text:
  cam_snap_timestamp:
    name: Timestamp for Camera Snapshot

input_datetime:
  date_and_time:
    name: Input with both date and time
    has_date: true
    has_time: true
  cam_date:
    name: date for camera snapshot
    has_date: true
  cam_time:
    name: time for camera snapshot
    has_time: true

- id: '1583006242795'
  alias: '[test] input datetime'
  description: ''
  trigger:
  - at: '21:50:03'
    platform: time
  condition: []
  action:
  - data_template:
      datetime: '{{ now().strftime(''%Y-%m-%d %H:%M:%S'') }}'
    entity_id: input_datetime.date_and_time
    service: input_datetime.set_datetime
  - data_template:
      date: '{{ as_timestamp(now())|timestamp_custom(''%Y-%m-%d'') }}'
    entity_id: input_datetime.cam_date
    service: input_datetime.set_datetime
  - data_template:
      time: '{{ now().strftime(''%H:%M:%S'') }}'
    entity_id: input_datetime.cam_time
    service: input_datetime.set_datetime
  - data_template:
      value: '{{ now().strftime(''%Y%m%d_%H%M%S'') }}'
    entity_id: input_text.cam_snap_timestamp
    service: input_text.set_value


- id: '1574603291945'
  alias: '[doorbell] push actions'
  description: ''
  trigger:
  - event_data:
      address: 1/5/4
      data: 1
    event_type: knx_event
    platform: event
  condition: []
  action:
  - data:
      message: Er is iemand aan de deur.
      title: Deurbel
    service: notify.notify
  - data:
      deviceID:
      - ipad_living
      entity_id: camera.cam_voordeur
      large: true
    service: browser_mod.more_info
  - data_template:
      value: '{{ now().strftime(''%Y%m%d_%H%M%S'') }}'
    entity_id: input_text.cam_snap_timestamp
    service: input_text.set_value
  - data:
      filename: /config/www/camera/{{ states.input_text.cam_snap_timestamp.state }}_snap_voordeur.jpg
    entity_id: camera.cam_voordeur
    service: camera.snapshot
  - data:
      message: 'Er is iemand aan de deur: [foto voordeur](https://xxxxxxxxxxxxxxxxxxxx.ui.nabu.casa/local/camera/{{ states.input_text.cam_snap_timestamp.state }}_snap_voordeur.jpg)'
      title: Deurbel
    service: persistent_notification.create
  - delay: 00:01:00
  - service: browser_mod.close_popup
2 Likes

I don’t see what the purpose of the first automation is (test input datetime)?

Is it just for testing/learning? because you didn’t use it in your camera automation.

While debugging I was stuck using data_templates from within the automations GUI and was starting to implement the datetime, date and time variables to test with. I figured that they could be useful later on as well.

After debugging I came to the conclusion that it had to do with parsing the datetime object and using the correct single quotes, and using them twice… The automation GUI translated this and I reused it directly inside the automation.yaml.

2020-03-01 07_54_10-automations.yaml - config - Visual Studio Code

I don’t know if what I’m using is the best approach but for now it’s the only way I can get everything working.

The only thing odd is that I can’t use the Automations GUI to set the correct data template.

Kind regards,
Stijn

update:
your code syntax is also working now. I must have done something else wrong yesterday.

It both works with single quotes, translated from the GUI or like you wrote it in your updated code above.

The only thing left is the question if we can use this data_template directly from within the automations GUI.

- id: '1583006242795'
  alias: '[test] input text'
  description: ''
  trigger:
  - at: '21:50:03'
    platform: time
  condition: []
  action:
  - service: input_text.set_value
    entity_id: input_text.cam_snap_timestamp
    data_template:
      value: '{{ now().strftime(''%Y%m%d_%H%M%S'') }}'

or

- id: '1583006242795'
  alias: '[test] input text'
  description: ''
  trigger:
  - at: '21:50:03'
    platform: time
  condition: []
  action:
  - service: input_text.set_value
    entity_id: input_text.cam_snap_timestamp
    data_template: 
      value: '{{ now().strftime("%Y%m%d_%H%M%S") }}'

I’m not really sure.

I never use the GUI for writing anything (automations, scripts, lovelace…). I always write it out by hand.

I don’t like the way it formats everything (like in your example) and writing it out by hand is almost as fast for me. Especially when you add in needing to use all of the drop down lists and scrolling thru to find what you want in the long lists. Then, again using your example, there’s the ambiguity of using templates and what syntax the editor is expecting.

It’s just a personal preference tho. But most users who have been around for very long prefer to hand write the yaml. So that says something.

just a fact they have that skill because at those days there was nothing but yaml :wink:
well, add to it the fact that almost all UI editors have their limitations comparing to yaml (and some bugs, too)
the bottom line is one uses UI if that’s what they comfortable with and something else if it limits them.
I don’t like generated automations and I almost hate Lovelace-sorted configs… but have to live with it :frowning:

And that’s what it says…

:slightly_smiling_face: