Lightning Strike Template - Newbie

So I am trying to create my first simple template. If Lightning is > 10 miles from my house return ‘Far’ (because I do not care), else if Lightning is < 10 miles from my house it reports the distance and the time measured.


{% if ("sensor.weatherflow_last_lightning_strike_distance" < "10") -%}
  Far at 
{%- else -%}
  {{ states('sensor.weatherflow_last_lightning_strike_distance')}} at {{states('sensor.weatherflow_last_lightning_strike')}}
{%- endif %}


The template works however I cannot format the time to a readable time (ie. HH:mm). Also, how would I limit this template to only report lightning strikes in the past 60 minutes?

What is the state that this is showing now?

Maybe:

{{ states('sensor.weatherflow_last_lightning_strike').strftime("%H:%M") }}

It depends if it is a datetime object.

UndefinedError: ‘str object’ has no attribute ‘strftime’

The deviceclass of the sensor is

device_class: timestamp

Can you share thee actual state please, like I asked?

It may need this:

{{ as_datetime(states('sensor.weatherflow_last_lightning_strike')).strftime("%H:%M") }}

But I’m still just guessing without this information:

Perfect. Shows the time as HH:mm.

Any idea how to create a double condition so that it will only show the distance and time if the time has been in the last 60 minutes?

Are you sure this works as intended? this is a constant string comparison. You are missing the states() function.

I will have to wait for lightning to strike to find out. How would you write it to work?

{% if (states('sensor.weatherflow_last_lightning_strike_distance') | float(0) > 10) -%}

So you don’t just use the sensor name but get that sensors state, convert it to a number that may have decimals and compare it to a number (not a string that is the textual representation of that number). The 0 parameter for the float filter is the default that is used when the sensor is unavailable.

Edit: I also changed the < to > because you meant far, as you said correctly above your code.

Seeing that he isn’t answering your response, I looked at the code. It appears to be an integer in seconds from epoch.

1 Like

So timestamp_custom() would have been a better fit.

Meh, they both do the same thing. As_datetime should work with the epoch number

Thank you all for your help. It’s working now (almost) perfectly. Now I would like to limit the sensor to only show how many miles away if lightning struck in the last 60 minutes.