I’m trying to figure out how to fire an automation based on the new wwln lightning component. It creates new entities when there is a strike, which persist for a certain amount of time and then are removed. they have names like “geo_location.lightning_strike_3694317” where the # at the end is different for every strike.
So without knowing the exact entity name ahead of time, how to automate? They appear on the map based on their location. Could you have a zone, and whenever an entity fitting “lightning_strike_*” is in the zone, trigger something?
You could use an event trigger that watches for state_changed events. Then use a condition that requires old_state to be none, and entity_id to start with geo_location.lightning_strike_.
Why not just create a temporary entity on the States page? You can enter whatever you want at the top and click SET STATE. It will just create an entry in the state machine, which is what the automation will react to. Next time you restart HA that entry will be gone.
I create an entity “geo_location.lightning_strike_3694317” with a state of “10” - it appears on my state list correctly, but this boolean doesn’t turn on.
Just to complicate things - I didn’t want to be notified about every strike, so I tried to come up with a system to only get notified by the first one, and if there had been no strikes for 30 minutes, to re-enable notifications. This puts the notification action in a different automation than the initial strike detection. Can you think of a way to add the state of the lightning strike entity to the annoucement, which occurs in a different automation? Set a variable equal to it?
Not sure where you want to use {{ trigger.event.data.new_state.state }}, but now you can.
I assume you’re using input_boolean.lightning_state for another reason, but if not, you can simplify this even more by removing it and the second automation.
Very nice. The one thing this doesn’t do that my original code does is “reset” the timer if a new strike occurs. My initial idea was if there was a strike every 15 minutes, it would only give a single warning, even if this happened all day long. It was only after 30 minutes of no strikes would it allow another announcement. I think your code would allow a new announcement every 30 minutes, even if there was an ongoing storm. That’s why I had the extra booleans in there.
the state of each new entity has the # of miles from your house to the strike, so I wanted that to be included in the announcement, like this:
What would be even better would be if I could save the state in a variable (the # of miles from the strike) and only re-announce if there was one closer (within that 30 minute period).
Ok, I think you can implement the first idea this way. This first automation simply starts, or restarts, the timer whenever there is a new lightning strike. It also saves the state for use in the second automation. (Of course, you need to define input_text.last_strike. I suppose you could also use an input_number instead of an input_text.)
- alias: Lightning Timer Start
initial_state: 'on'
trigger:
platform: event
event_type: state_changed
condition:
- condition: template
value_template: >
{{ trigger.event.data.old_state is none and
trigger.event.data.entity_id.startswith('geo_location.lightning_strike_') }}
action:
- service: input_text.set_value
data_template:
entity_id: input_text.last_strike
value: "{{ trigger.event.data.new_state.state }}"
# This will start, or restart, the timer.
- service: timer.start
data:
entity_id: timer.lightning_timer
duration: '00:30:00'
This second automation triggers only when the timer starts (i.e., timer.start is called while the timer is idle.) I haven’t tested this, but according to the docs, if timer.start is called while the timer is active, it should fire the timer.restarted event instead of timer.started.
- alias: Lightning Announce
initial_state: 'on'
trigger:
# Trigger only when timer starts (i.e., not when it restarts.)
platform: event
event_type: timer.started
event_data:
entity_id: timer.lightning_timer
action:
- service: notify.email3
data_template:
message: 'Skynet reports lightning strike within {{ states('input_text.last_strike') }} miles.'
- service: notify.alexa_media
data_template:
message: 'Skynet reports lightning strike within {{ states('input_text.last_strike') }} miles.'
data:
type: announce
target: [media_player.kitchen_echo_2,media_player.jason_s_spot]
Could I use two variables, input_text.last_strike and closest_strike, and make an extra announcement automation if the last_strike is closer than the closest_strike, and then set closest_strike to be equal to last_strike?
Testing this automation - it’s announcing every new strike. From what I can tell, the timer.started trigger is firing every time the timer is reset with the timer.start service. It’s not registering it as a timer.restarted trigger if timer.start is acting on an active timer.