Hi folks. I have 2 RF buttons beside my bed which trigger existing automations:
‘Goodnight’ turns off all lights in the flat, and plays white noise on my bedside Google Home display.
‘Good morning’ switches off white noise, announces the current weather, with temperatures (from sensors inside and out) using TTS, and plays my favourite radio station on all speakers.
What I want to do is to add an announcement of the time I’ve spent in bed i.e. between pressing button 1 and button 2: “You’ve been in bed for six hours and 34 minutes” or whatever.
I’ve looked at history stats, timer, and other stuff but am stuck. Thanks in advance for any help.
Having run both scripts, I had 2 timestamp numbers to play with (good - no fiddly datetime values to play with), and, clearly, the difference between the numbers was a number of seconds (I didn’t know this before)
So, ‘end’ minus ‘start’ would give me the number of seconds in bed. To create this as an entity, I added the following to configuration.yaml:
platform: template
sensors:
abed_time_seconds:
friendly_name: ‘Abed time in seconds’
value_template: ‘{{ as_timestamp(states.input_datetime.abed_end.state) - as_timestamp(states.input_datetime.abed_start.state) }}’
I restarted of course, then add that entity to Lovelace too - again, just to see the values, which look like this:
Next, I added the ‘start’ script above to my ‘goodnight’ automation, and my ‘end’ script to my ‘good morning’ automation. Both run from buttons beside my bed. As you can see, I was in bed for 8 hours last night.
Finally, I added an extra section to my morning TTS announcement:
sequence:
service: tts.clear_cache
data: {}
service: tts.google_translate_say
data:
entity_id: media_player.all_speakers_2
message: >-
Good Morning David!
It’s
{{states(‘sensor.openweathermap_forecast_condition’)}}.
On the balcony, it’s
{{states(‘sensor.tz3000_1x1vtalf_ts0201_465eabfe_temperature’)}}
and in the lounge
{{states(‘sensor.tz2000_a476raq2_ts0201_temperature’)}}.
There is a
{{states(‘sensor.openweathermap_forecast_precipitation_probability’)}}
percent chance of rain.
You were in bed for
{{ (as_timestamp(states.input_datetime.abed_end.state) -
as_timestamp(states.input_datetime.abed_start.state))/3600 }} hours
mode: single
alias: TTS Weather Report
All that’s left to do is to split the announcement up into hours and minutes, but I’ll worry about that another time