I’m trying to calculate the number of seconds since the last hour, 15 minutes after the hour, …
So I have an input_datetime where I can store this:
- alias: Begin van huidige kwartier
trigger:
- platform: time_pattern
minutes: "/15"
action:
- service: input_datetime.set_datetime
entity_id: input_datetime.start_huidig_kwartier
data_template:
datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
So, on 00, 15, 30 and 45 minutes exactly, the current time is stored. This in working.
Okay, now the more difficult part. In Devtools, Templates I did a test:
{{ (now() - now().replace(hour=state_attr('input_datetime.start_huidig_kwartier', 'hour'), minute=state_attr('input_datetime.start_huidig_kwartier', 'minute'), second=state_attr('input_datetime.start_huidig_kwartier', 'second'))) }}
At 21:32:22 this results in 0:03:22.999988; lets call this ‘piekseconds’
Looks good.
But now I’m stuck. I need to make calculations with it.
I have a sensor (sensor.fluvius_verbruik_energy_kwartier_piek), a float ranging between 0 and 10.
My calculation (and eventually my new sensor that I want to create) is this:
sensor.fluvius_verbruik_energy_kwartier_piek / piekseconds * 900
How do I do that? Since ‘piekseconds’ is a string (I think ?), and I don’t need the complete string, but as in this example: 203 (seconds) 3x60+22+1 (when rounding up).
So my first step is now: convert this (0:03:22.999988) to an integer (203).
Any help much appreciated.