Closed rafalolb closed 10 months ago
If I understand correctly you'd like to see which container was most recently restarted? That would be quite a specific usecase that's probably best implemented as a template sensor instead of added to this plugin.
An example template:
template:
- sensor:
- name: Docker min uptime
state: >
{%- set ns = namespace(most_recent_uptime=0, entity_id=None) -%}
{%- for s in states.sensor -%}
{%- if s.entity_id.startswith("sensor.docker_") and s.entity_id.endswith("_up_time") -%}
{%- if has_value(s.entity_id) and s.state|as_timestamp > ns.most_recent_uptime -%}
{%- set ns.most_recent_uptime = s.state|as_timestamp -%}
{%- set ns.entity_id = s.entity_id -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{{ ns.entity_id }}
attributes:
uptime: >
{%- set ns = namespace(most_recent_uptime=0, entity_id=None) -%}
{%- for s in states.sensor -%}
{%- if s.entity_id.startswith("sensor.docker_") and s.entity_id.endswith("_up_time") -%}
{%- if has_value(s.entity_id) and s.state|as_timestamp > ns.most_recent_uptime -%}
{%- set ns.most_recent_uptime = s.state|as_timestamp -%}
{%- set ns.entity_id = s.entity_id -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{{ ns.most_recent_uptime|as_datetime }}
This would expose the entity_id
of the most recent restarted container uptime as its state, and contain the time it restarted in the uptime attribute.
Thank you for reply.
I copy this code to my configuration.yamI, but I have a problem, sensor is still unavailable :(
You might want to check the logs for any information that might be usefull here.
Template variable error: 'ns1' is undefined
Thats a lot more helpfull in diagnosing the issue compared to your previous statement.
According to that line it seems like I made a typo in the template code. I've fixed it in the example above.
Now is better, but I don't see minimal uptime value
Ohh sory, I understand your script and you set min uptime time in uptime attribute.
I want to cut state to "homeassistant" only. I want to remove "sensor.docker" from the start and "_Up_time" from the end.
I modify one line of your script and now looks like better:
{{ ns.entity_id.replace('sensor.docker_','').replace('_up_time','') }}
Photo
Tnx for help
Now I want to create automation: if min uptime is more then current time + 15 minutes then sent me messege to my phone: "docker name: 'docker_name' was restarted with 15 minutes ago"
Now I created template condition, but something is wrong:
{{ (now().timestamp() - (15*60)) < (state_attr('sensor.docker_min_uptime' , 'Uptime') ) }}
Error message:
State attributes are case sensitive. Try:
{{ (now().timestamp() - (15*60)) < (state_attr('sensor.docker_min_uptime' , 'uptime') ) }}
Also, there may be cases (especially during startup) where the state is not yet initialized. In which case you'll get similar errors. You should probably check for that before just using the value. E.g. by wrapping the template in a condition that checks for the value:
{{ has_value('sensor.docker_min_uptime') and ((now().timestamp() - (15*60)) < state_attr('sensor.docker_min_uptime' , 'uptime') ) }}
I must convert string to float using as_timestamp:
{{ has_value('sensor.docker_min_uptime') and ((now().timestamp() - (15*60)) < as_timestamp (state_attr('sensor.docker_min_uptime' , 'uptime')) ) }}
Now works fine, tnx so much.
Hi,
I want to check last container restart from my all containers.
Can you add sensor.docker_min_up_time sensor?
Regards.