ualex73 / monitor_docker

Monitor Docker containers from Home Assistant
Apache License 2.0
267 stars 34 forks source link

Minimum Up_time sensor #116

Closed rafalolb closed 10 months ago

rafalolb commented 1 year ago

Hi,

I want to check last container restart from my all containers.

Can you add sensor.docker_min_up_time sensor?

Regards.

RoboMagus commented 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.

rafalolb commented 10 months ago

Thank you for reply.

I copy this code to my configuration.yamI, but I have a problem, sensor is still unavailable :(

RoboMagus commented 10 months ago

You might want to check the logs for any information that might be usefull here.

rafalolb commented 10 months ago

Template variable error: 'ns1' is undefined

RoboMagus commented 10 months ago

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.

rafalolb commented 10 months ago

Now is better, but I don't see minimal uptime value Screenshot_20230811-184842_Home Assistant~2

rafalolb commented 10 months ago

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.

rafalolb commented 10 months ago

I modify one line of your script and now looks like better:

{{ ns.entity_id.replace('sensor.docker_','').replace('_up_time','') }}

Photo Screenshot_20230811-192625_Home Assistant~2

Tnx for help

rafalolb commented 10 months ago

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"

rafalolb commented 10 months ago

Now I created template condition, but something is wrong:

{{ (now().timestamp() - (15*60)) < (state_attr('sensor.docker_min_uptime' , 'Uptime') ) }}

Error message:

rafalolb commented 10 months ago

Screenshot_20230812-030058_Home Assistant~2

RoboMagus commented 10 months ago

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') ) }}
rafalolb commented 10 months ago

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.