I am new to templates and can’t figure out how to make this work. I am scraping a website to get the sunrise time in Bali. The time is showing as 5:11. I need to change the format to 05:11:00 and add 4 hours to make it 09:11:00. What would I need to do this?
What is the actual state of your sensor in the states page?
best to provide a screenshot of that sensor and its attributes.
Something like this:
"0{{ states.sensor.scaper.state[0:1] | int +4 }}{{ states.sensor.scaper.state[1:4] }}:00"
This uses the string and adds four hours, you could also use something to parse it to a timestamp which honestly is safer.
But in this case, being sunrise we can safely assume the time will be single digit hour (?)
the best possible solution that would capture every scenario, but it’s a little complicated:
{% set h, m = '05:11'.split(':') | map('int') %}
{% set t = now().replace(hour=h, minute=m, second=0).timestamp() + 4 * 60 * 60 %}
{{ t | timestamp_custom('%H:%M:%S') }}
If this is a scrape sensor and we assume that value = ‘05:11’…
{% set h, m = value.split(':') | map('int') %}
{% set t = now().replace(hour=h, minute=m, second=0).timestamp() + 4 * 60 * 60 %}
{{ t | timestamp_custom('%H:%M:%S') }}
The other solution in the thread will not work when your time is 4 hours from midnight. You’ll end up with 25 to 28 oclock… which doesn’t make sense.
EDIT: They just added timedelta… so that’s a solution too. Might be a little easier to read?
{% set h, m = '05:11'.split(':') | map('int') %}
{% set t = now().replace(hour=h, minute=m, second=0) + timedelta(hours=4) %}
{{ t.timestamp() | timestamp_custom('%H:%M:%S') }}
Thanks. That works for the sunrise. I am also doing one for sunset and assumed that the solution would work for both. Because the sunset is double digit hour this does not work. What would you recommend for that?
Scraping site for sunset:
- platform: scrape
resource: https://tides4fishing.com/as/west-indonesia/banjuwangi-bali-str
select: "#amanecer_anochecer_datos_puesta_sol div.amanecer_anochecer_datos_salida_puesta_sol2"
name: ReefSunsetBali
scan_interval: 7200
value_template: '{{ (value.split(" h")[0]) }}'
The current state is “17:18”. I am able to change that to read “17:18:00” however I also need to add 4 hours to make it read “21:18:00”. Any ideas how this can be done?
see my post
Thanks petro. I was working on mine when you posted. When I try the timedelta one I get
Error rendering template: UndefinedError: ‘timedelta’ is undefined
You mentioned that it was just added. I’m assuming that I need to update HA for that to work?
are you in 0.115? 0.115 is required for the timedelta. otherwise use the one above it.
I’m on .108.9. I’m going to go ahead and update and will try again.
Oof, be prepared for many breaking changes. You could just use the template above it, the middle one.
{% set h, m = value.split(':') | map('int') %}
{% set t = now().replace(hour=h, minute=m, second=0).timestamp() + 4 * 60 * 60 %}
{{ t | timestamp_custom('%H:%M:%S') }}
Speaking of things that makes no sense…
Sunrise at 21:00-24:00.
Can you please remind me when that last happened in Bali? I keep forgetting…
People won’t come here for a sunrise only solution. As you can already see, he wanted to do it for sunset too. Sorry to offend you but when coding you always want to cover all bases for this reason alone.
Upgrade didn’t break too many things but I will have some work to do to get everything back in order.
I’ve tried both suggested. Timedelta method returns “unknown”. The other method returns the correct format but changes the minuets to 00. Below is what I have tried.
- platform: scrape
resource: https://tides4fishing.com/as/west-indonesia/banjuwangi-bali-str
select: "#amanecer_anochecer_datos_puesta_sol div.amanecer_anochecer_datos_salida_puesta_sol2"
name: ReefSunsetBali
scan_interval: 7200
value_template: >
{% set h, m = '05:11'.split(':') | map('int') %}
{% set t = now().replace(hour=h, minute=m, second=0) + timedelta(hours=4) %}
{{ t.timestamp() | timestamp_custom('%H:%M:%S') }}
and
- platform: scrape
resource: https://tides4fishing.com/as/west-indonesia/banjuwangi-bali-str
select: "#amanecer_anochecer_datos_puesta_sol div.amanecer_anochecer_datos_salida_puesta_sol2"
name: ReefSunsetBali
scan_interval: 7200
value_template: >
{% set h, m = value.split(':') | map('int') %}
{% set t = now().replace(hour=h, minute=m, second=0).timestamp() + 4 * 60 * 60 %}
{{ t | timestamp_custom('%H:%M:%S') }}
Try replacing value with value[:5]
That causes the sensor to disappear.
Just saw your edit. Still not showing as a sensor. I’m attaching my code to verify that I have it done correctly.
- platform: scrape
resource: https://tides4fishing.com/as/west-indonesia/banjuwangi-bali-str
select: "#amanecer_anochecer_datos_salida_sol div.amanecer_anochecer_datos_salida_puesta_sol2"
name: ReefSunriseBali
scan_interval: 7200
value_template: >
{% set h, m = value[:5].split(':') | map('int') %}
{% set t = now().replace(hour=h, minute=m, second=0).timestamp() + 4 * 60 * 60 %}
{{ t | timestamp_custom('%H:%M:%S') }}
Ok, there’s a few issues here.
- The value returned is a long string.
- The 2nd half of the value has a
h
in it. - The first value can be 1 or 2 characters long. For example, if a sunrise happens at 10 am or a sunset happens anytime after 10 am to midnight, there will be 2 characters here instead of 1.
So to account for all that…
{% set h, m = value.strip().split(':') %}
{% set h, m = [ h, m[:2] ] | map('int') %}
{% set t = now().replace(hour=h, minute=m, second=0) + timedelta(hours=4) %}
{{ t.timestamp() | timestamp_custom('%H:%M:%S') }}
As a side note, this should work for anything you scrape off that site that has the format ‘xx:xxxxxxxxxxxxxxxxxxx…’ or 'x.xxxxxxxxxxxxxxxxxxxx…" and it will work if the + 4 hours crosses midnight.
Thanks petro. I am now showing the sensor but getting an unknown state.
I can confirm that, in 0.115, timedelta()
doesn’t exist.
I checked its PR (39608) and it seems like it was included in the code’s dev
branch but not in the release. The documentation doesn’t includetimedelta()
but it does describe the new as_local()
(that one is definitely in 0.115). Hopefully timedelta()
will be included in 0.116.
That would make sense. I’m testing all my stuff against 0.116 dev build and the sensor works as expected.
@justin1977 until 0.116 comes out, use this template:
{% set h, m = value.strip().split(':') %}
{% set h, m = [ h, m[:2] ] | map('int') %}
{% set t = now().replace(hour=h, minute=m, second=0).timestamp() + 4 * 60 * 60 %}
{{ t | timestamp_custom('%H:%M:%S') }}