Condition check IP

Hi!

I want a condition to my automation. I have tried a lot of “copy paste coding” but nothing works.

My automation triggers every 10 minutes. I want it to check if my primary DNS is 192.168.1.1 and if not start an action.

I have the trigger and action in place but I don’t get the condition to work.

The entity name of the ping sensor (configuration.yaml) is something like sensor.primDNSHuawei

I would really appreciate if someone could help me.

With DNS you mean your IP adress? If yes, of what - your HA instance? Or a DNS server?

What have you tried so far? Post your code.

Is it this what you are looking for?

This is my code right now:

alias: restart Huawei
description: ""
trigger:
  - platform: time_pattern
    seconds: /5
    enabled: false
condition:
  - condition: template
    value_template: >-
      value_template: "{{ '192.168.1.1' in
      sensor.e5377ts_32_primary_dns_server}}"
action:
  - type: toggle
    device_id: ef2fc9f1d215bbdc1cb62cb8edd51dad
    entity_id: switch.router_udr_poe
    domain: switch
mode: single

I get the DNS information from the Huawei LTE integration.

It appears you have duplicated the text value_template:

alias: restart Huawei
description: ""
trigger:
  - platform: time_pattern
    seconds: /5
    enabled: true
condition:
  - condition: template
    value_template: "{{ '192.168.1.1' in sensor.e5377ts_32_primary_dns_server}}"
action:
  - type: toggle
    device_id: ef2fc9f1d215bbdc1cb62cb8edd51dad
    entity_id: switch.router_udr_poe
    domain: switch
mode: single

It didnt work

I just want to check the IP (of Huawei DNS) and if its not 192.168.1.1 I want to restart a plug.

Maybe add a trailing space before the two closing/right curly braces?

Thank you, it dit not work.

I dont really know if this is the right way to go. I mean it just a copy paste code. But The trigger and action works.

Have you tried your condition template code in the Developer Tools / Template (Jinja) editor?
That’s the best method for debugging these things. I think what you want may be the following:

"{{ '192.168.1.1' in states('sensor.e5377ts_32_primary_dns_server') }}"

Thank you for pointing me to Developer Tools / Template (Jinja) editor.

I get the following error:

You cannot put the full YAML code into the Jinja template editor. Just the value_template: quoted value. Try using the line in my post above and see if it returns either True or False values. It may also help if you place TWO lines in the Template editor to see the actual sensor status value being returned as well as the logical result of your template.

"{{ states('sensor.e5377ts_32_primary_dns_server') }}"

"{{ '192.168.1.1' in states('sensor.e5377ts_32_primary_dns_server') }}"

Thank you very much, it worked in Jinja :slight_smile:

`"{{ states(‘sensor.e5377ts_32_primary_dns_server’) }}"´

But if I just copy the two lines in to my code, i get error. I think I have to post something before that first line.

I only put the two lines for your benefit, to confirm the value being returned.
You only need the second line in your value_template: value.

Thank you very much!!! Now its working.

I hope you will have a nice evning

So glad to help!! It’s always easier when you can bang it off someone else, and talk/think it out. :slight_smile:

1 Like

If I want to convert the template to a “not” condition, how could I do that?

So if 192.168.1.1 is not true the action will start

This code works but the action starts if the IP is 192.168.1.1

alias: restart Huawei
description: ""
trigger:
  - platform: time_pattern
    seconds: /5
    enabled: true
condition:
  - condition: template
    value_template: "{{ '192.168.1.1' in states('sensor.e5377ts_32_primary_dns_server') }}"
  - condition: state
    entity_id: binary_sensor.duckdns
    state: "on"
action:
  - type: toggle
    device_id: ef2fc9f1d215bbdc1cb62cb8edd51dad
    entity_id: switch.router_udr_poe
    domain: switch
mode: single

just guessing here, try adding not before in

also, if the states value is a single value rather than a list of several values, you might want to do != instead of not in

This automation seems to be one of those that needs much finer conditions, better actions , and a better trigger in my opinion.

Ever five seconds is wasteful and unnecessary.
Just use the state. Essentially your condition could be your trigger.

If you toggle a switch every five seconds then the device you are toggeling will probably not have enough time to boot up and report a different value.

Shouldn’t the condition be something like “only if automation was last triggered x minutes go”? To make sure it doesn’t toggle the switch repeatedly all the time?