My envisalink sensors return a last_tripped_time that is in seconds and I’d like convert that to days, hours and minutes. I found a python module that does that simply, but I’m not sure how to integrate it. It works from a python console, but HA doesn’t see it.
An expert programmer needs to actually help you… but I play one on TV.
I know Home Assistant is written in Python 3, not 2.7… If Home Assistant is working then it may not be an issue.
Next, I was reading about humanfriendly… it needs to be imported - not native to python - not sure it’s included with Home Assistant… Again you need a real programmer here. But I did stay at a holiday inn express last night.
Thanks. I did add it with pip and then import. I appended its location to pythonpath, but it kept getting removed. They must have made it fool proof. I dabble in programming for fun, but most of the HA stuff is new to me. I’ve always learned by glomming other people’s work and bending it to my will, but not having much luck here. Everything seems to work despite the apparent wrong python version.
I use the AIO installer on a RPi3. When I type python -V I also get 2.7.9. I dabble in python and wish I could get someone to show me the ropes on where I can help with the project.
I specifically want to change the “off” color of the optimistic true switches from the default “on” blue color. See it’s blue because off is on… When off should not be blue or on at all. lol It’s a over-simplistic programming error that should be spanked.
Well, I’m still stumped. I don’t see where as_timestamp can take a number, assume it’s seconds and convert it as I need, so as a test I replaced that with just the current time and it still didn’t add the last_time_tripped to the new entity. I started looking at humanfriendly from this thread: https://stackoverflow.com/questions/775049/python-time-seconds-to-hms That would solve the conversion, but why can’t I get that attribute in the new entity?
Getting this as the first log entry: “Could not render template Nook Window time, the state is unknown” followed later by, “Could not render template Nook Window time: UndefinedError: ‘homeassistant.core.State object’ has no attribute ‘last_tripped_time’”
Added ‘entity_id: sensor.time’ to fix that, but no help
states.binary_sensor.breakfast_nook_window.attributes.last_tripped_time removed the secondary errors but still no attribute
Works like a champ! I though I was just changing an attribute, which is different from the entity’s state. Also like instantiating an object in Java and changing your copy. Can you enlighten me?
All the template sensor does in this case, thanks to the value_template is change the visual representation of the last_tripped_time of your binary_sensor. It doesn’t do anything to the attribute itself, or the entity the attribute belongs to.
I use very similar sensors in my setup to show the uptime of my Raspberry Pi and the runtime of Home Assistant. So I just took the yaml-code from one of those two sensors, and edited it to suit your scenario.
Does anyone know how to set up the uptime sensor?
It can only show minutes, days or hours. https://home-assistant.io/components/sensor.uptime/
Would really like for that one to show up the same way as the sensor.last_boot or as you have mentioned @fanaticDavid
Thank you, @fanaticDavid but I don`t need the sensor for how long it has been I restartet the pi, I want the time since I last restartet homeassistant.
Before I used this to control it:
- platform: command_line
name: "HA Uptime"
command: echo "$(($(date +%s) - $(date -d "$(head -n1 /config/home-assistant.log | cut -d' ' -f-2)" +%s)))"
scan_interval: 60
value_template: >-
{% set uptime = value | int %}
{% set minutes = ((uptime % 3600) / 60) | int %}
{% set hours = ((uptime % 86400) / 3600) | int %}
{% set days = (uptime / 86400) | int %}
{%- if uptime < 60 -%}
Less than a minute
{%- else -%}
{%- if days > 0 -%}
{%- if days == 1 -%}
1 day
{%- else -%}
{{ days }} days
{%- endif -%}
{%- endif -%}
{%- if hours > 0 -%}
{%- if days > 0 -%}
{{ ', ' }}
{%- endif -%}
{%- if hours == 1 -%}
1 hour
{%- else -%}
{{ hours }} hours
{%- endif -%}
{%- endif -%}
{%- if minutes > 0 -%}
{%- if days > 0 or hours > 0 -%}
{{ ', ' }}
{%- endif -%}
{%- if minutes == 1 -%}
1 minute
{%- else -%}
{{ minutes }} minutes
{%- endif -%}
{%- endif -%}
{%- endif -%}
But I have moved my database to MariaDB, so I can`t use that anymore.
Therefore I just need a template to convert minutes to the same way this did with seconds. I am not sure how.
My bad, I didn’t read the Uptime Sensor page completely so I wrongly assumed it was the host’s uptime being displayed.
In my setup, I use the following command_line sensor for my Home Assistant uptime:
- platform: command_line
name: "HA runtime"
command: echo "$(($(date +%s) - $(date --date="`systemctl show home-assistant.service -p ActiveEnterTimestamp | awk -F'=' '{print $2}'`" "+%s")))"
scan_interval: 60
value_template: >-
{% set uptime = value | int %}
{% set minutes = ((uptime % 3600) / 60) | int %}
{% set hours = ((uptime % 86400) / 3600) | int %}
{% set days = (uptime / 86400) | int %}
{%- if uptime < 60 -%}
less than a minute
{%- else -%}
{%- if days > 0 -%}
{%- if days == 1 -%}
1 day
{%- else -%}
{{ days }} days
{%- endif -%}
{%- endif -%}
{%- if hours > 0 -%}
{%- if days > 0 -%}
{{ ', ' }}
{%- endif -%}
{%- if hours == 1 -%}
1 hour
{%- else -%}
{{ hours }} hours
{%- endif -%}
{%- endif -%}
{%- if minutes > 0 -%}
{%- if days > 0 or hours > 0 -%}
{{ ', ' }}
{%- endif -%}
{%- if minutes == 1 -%}
1 minute
{%- else -%}
{{ minutes }} minutes
{%- endif -%}
{%- endif -%}
{%- endif -%}
My Home Assistant instance is based on the All-in-One Installer, so YMMV.
By the way, I don’t see how you switching to MariaDB would’ve broken your sensor. It seems to be based on the Home Assistant log file, not the SQLite database.
You are completely right! It may be due to the upgrade to 0.60, but I`m not sure.
Anyways, it does just continue add time from the state it had, so it now starts up with over 20 hours.
But the uptime sensor does seem correct, so if I just could just use that data to a similar template all would be good.
It comes with an output of “0.00” minutes, so do you know how to change that to days, hours, minutes?
I use Hassio, so I can not use systemctl.
Gah, I must be tired. I went back one last time after posting this and found my mistake.
Have to set the actual values for minutes and hours:
{% set time = states.sensor.esxi_raw_uptime.state | int %}
{% set minutes = ((time % 360000) / 6000) | int%}
{% set hours = ((time % 8640000) / 360000) | int %}
{% set days = (time / 8640000) | int %}