Automation to perform a TTS when a Blitzortung lightening strike is detected, but no sooner than 1x a minute

I think I did this correctly, but hoping someone could spot check me. If this is correct, great, I can leave this up for the rest of the community to benefit from.

I am making an automation that will play a chime audio file (stored locally) and then do a Google TTS to tell me when a lightning strike is detected and its distance. The main area of concern is the trigger section. Understanding that there could me a high number of lightning strikes within a 1 hour period, I do not want the automation to trigger more than once a minute.

Here’s my code:

alias: 'Notification: Lightning Strike'
description: ''
trigger:
  - platform: template
    value_template: "{{ (states ('sensor.blitzortung_lightning_counter') | float  % 1) > 0 }}"
condition: []
action:
  - service: media_player.play_media
    data:
      media_content_type: music
      media_content_id: http://<sanitized>/local/audio/lightning-notification-chime.mp3
    target:
      entity_id: media_player.sonos_bedroom
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 500
  - service: tts.google_translate_say
    entity_id: media_player.sonos_bedroom
    data_template:
      message: >
        '"Attention! A lightning strike has been detected {{
        states('sensor.blitzortung_lightning_distance') }} kilometers
        from your location."'
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
mode: single
1 Like

I wouldn’t put a delay at the end. I’d use something like this as a condition:

condition: template
value_template: |-
  {{ as_timestamp(now()) - as_timestamp(state_attr(this.entity_id,
    'last_triggered')) | int(0) > 60 }}

Just a preference so you won’t get ‘already running’ errors in your logs.

otherwise I think it looks fine.

Interesting. I have been able to get the “don’t run if last triggered x” to work in previous automations. I had to take the sloppy route and add a delay at the end. Let me give this another try.

I am getting a “no default value specified” error so I modified it to:

alias: 'Notification: Lightning Strike'
description: ''
trigger:
  - platform: template
    value_template: "{{ (states ('sensor.blitzortung_lightning_counter') | float(0) % 1) > 0 }}"
condition: []
action:
  - service: media_player.play_media
    data:
      media_content_type: music
      media_content_id: http://<sanitized>/local/audio/lightning-notification-chime.mp3
    target:
      entity_id: media_player.sonos_bedroom
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 500
  - service: tts.google_translate_say
    entity_id: media_player.sonos_bedroom
    data_template:
      message: >
        '"Attention! A lightning strike has been detected {{
        states('sensor.blitzortung_lightning_distance') }} kilometers
        from your location."'
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
mode: single

Let me try it your way with the last_triggered condition.

Awesome! The last_triggered condition is working.

I cannot figure out why my float is not working though. If I change it from a 1 to a 2, it will trigger the automation when the lightening strike count hits 1, 3, 5, etc. When I switch it to 1, it will not trigger the automation at all; regardless of the sensor’s value.

Here is the code will trigger successfully when the lightning strike count hits 1, 3, 5, etc., but not if the automation has last ran within past 60 seconds:

trigger:
  - platform: template
    value_template: >-
      {{ (states ('sensor.blitzortung_lightning_counter') | float % 2 >
      0) }}
condition:
  - condition: template
    value_template: |2-
        {{ as_timestamp(now()) - as_timestamp(state_attr('automation.notification_lightning_strike',
          'last_triggered')) | int(0) > 60 }}

Any thoughts to allow this to run every time the lightning counter increases by one (assuming the automation has not last ran in the past 60 seconds)?

{{ states ('sensor.blitzortung_lightning_counter') }}
THis I think is better though.

  - platform: state
    entity_id:
      - sensor.blitzortung_lightning_counter
    not_from:
      - unknown
      - unavailable
    not_to: 0

If you simply do this, it should trigger with each strike (every time the state changes). the condition will prevent it unless it was past 1 minute.

On a side note, I live in the lightning capital lol. This would be a LOONNNNGGG sleepless night for me.

also you can use ‘this.entity_id’ so you don’t need to worry about the names changing ever.

{{ as_timestamp(now()) - as_timestamp(state_attr(this.entity_id,
          'last_triggered')) | int(0) > 60 }}
1 Like

Ah! Great tip on this.entity_id, I thought it was a placeholder when I first read it.

Whenever I try {{ states ('sensor.blitzortung_lightning_counter') }} it only triggers the automation when the value goes from 0 to a number greater than 0. It will only retrigger the automation after the value goes back to 0.

I saw your edit while typing this as well. That’s another great tip about excluding unknown or unavailable. I have several other automations that pop up in my logs because of that. I got a good laugh out of your lightning capital comment. I am definitely going to scale this back once I get it working well. Wife would not be pleased with this going off all through the night.

Also, I don’t want to waste anyone’s time. I would love to get this working as originally intended, but as a last resort, I could just change the duration that it counts lightning strikes to 15 minutes. That would basically give me an alert once every 15 minutes (if the storm is still occurring) because the counter will reset back to 0.

Apologies Rob, I misread your last post. I was still using a value template. When switching it to - platform: state like you suggested, everything is working exactly as expected.

I really appreciate your time today!

For anyone in the future that wants to do something similar, here is the final working code:

alias: 'Notification: Lightning Strike'
description: ''
trigger:
  - platform: state
    entity_id:
      - sensor.blitzortung_lightning_counter
    not_from:
      - unknown
      - unavailable
    not_to: '0'
condition:
  - condition: template
    value_template: |2-
        {{ as_timestamp(now()) - as_timestamp(state_attr(this.entity_id,
          'last_triggered')) | int(0) > 60 }}
action:
  - service: media_player.play_media
    data:
      media_content_type: music
      media_content_id: http://<sanitized>:8123/local/audio/lightning-notification-chime.mp3
    target:
      entity_id: media_player.sonos_bedroom
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 500
  - service: tts.google_translate_say
    entity_id: media_player.sonos_bedroom
    data_template:
      message: >
        '"Attention! A lightning strike has been detected {{
        states('sensor.blitzortung_lightning_distance') }} kilometers
        from your location."'
mode: single

I used your example and added a TTS message that will be pushed to my poolside Apple TV.
I also made mine triggered by distance. This one is 50-60 miles.
I have a second one that is identical except it’s for 20-25 miles.

I have done a proof of work with an iPod mini and both automations are working as expected.

Thanks to everyone who provided the information above. It was very helpful

alias: "Notification: Lightning 50-60km"
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.blitzortung_lightning_counter
    not_from:
      - unknown
      - unavailable
    not_to: "0"
condition:
  - condition: template
    value_template: |2-
        {{ as_timestamp(now()) - as_timestamp(state_attr(this.entity_id,
          'last_triggered')) | int(0) > 60 }}
  - type: is_value
    condition: device
    device_id: 08feb91ae2ca51b4c740a8ee3fdcb0d9
    entity_id: sensor.blitzortung_lightning_distance
    domain: sensor
    above: 49.9
    below: 60
action:
  - device_id: 7ffd2e6bfe6c2f19fb49b8778fd594a4
    domain: mobile_app
    type: notify
    message: Lightning 50-60km
    title: Severe Weather Alert
  - service: tts.cloud_say
    data:
      entity_id: media_player.living_room_mini
      message: "Severe Weather Alert! Lightning is at 50 nautical miles. "
mode: single

That’s actually a good use case. I might implement that for my pool.

1 Like