Automation condition google_maps

Hello everyone, I apologize for my English but I have a problem with an automation. the condition battery_charger is not seen, in practice if I remove the automation is performed, otherwise it locks them. I tried both ways and now I’m with the # otherwise it does not go …

  • alias: “batteria_scarica_alex on”
    trigger:

    • platform: template
      value_template: ‘{{ states.device_tracker.google_maps_code.attributes.battery_level | int < states.input_number.min_battery_alarm_phone.state | int }}’
    • platform: time
      at: ‘22:00:00’
      condition:
      condition: and
      conditions:
      • condition: state
        entity_id: device_tracker.google_maps_code
        state: ‘home’

      • condition: template
        value_template: ‘{% if states.device_tracker.google_maps_code.attributes.battery_charging == “False” %}True{% else %}False{% endif %}’

      • condition: template
        value_template: “{{ states.device_tracker.google_maps_code.attributes.battery_charging == ‘False’ }}”

    action:

    • service: switch.turn_on
      entity_id: switch.batteria_scarica_alex

When posting YAML code, please follow the instructions at the top of the page so that it is formatted correctly. Otherwise it is very difficult to read and see what might be wrong.

But I think your problem is the battery_charging attribute is a boolean, but you’re comparing it to a string. That won’t work. Just use it as is. For example:

condition: template
value_template: "{{ not state_attr('device_tracker.google_maps_code', 'battery_charging') }}"

This condition will be satisfied if battery_charging is false.

I apologize for the mistake, now it works.
I ask you if you can tell me how to do in case of true.

  - alias: "batteria_scarica_alex off"
    trigger:
      - platform: template
        value_template: '{{ states.device_tracker.google_maps_code.attributes.battery_level | int > states.input_number.min_battery_alarm_phone.state | int }}'
      - platform: template
        value_template: "{{ state_attr(' device_tracker.google_maps_code ', 'battery_charging') }}"
    action:
      - service: switch.turn_off
        entity_id: switch.batteria_scarica_alex

thank you so much
ps
where can I find these answers so I can study them?

You can check it here:

In case you want to check for a state attribute being true use: is_state_attr

Sorry for the late reply.

This won’t work:

      - platform: template
        value_template: "{{ state_attr(' device_tracker.google_maps_code ', 'battery_charging') }}"

At the very least because you quoted the entity_id incorrectly. It should be:

      - platform: template
        value_template: "{{ state_attr('device_tracker.google_maps_code', 'battery_charging') }}"

Next question is do you want this as a condition or a trigger? Right now it’s a trigger, which means the automation will run if either battery_level goes above min_battery_alarm_phone, or if battery_charging becomes true. I tend to doubt that’s what you want.

@AlmostSerious,

You actually don’t need to do that if the attribute is already a boolean. In fact, you’d have to be careful using is_state_attr with a “boolean” attribute, because it might actually be a real boolean, or it might be a string representation of a boolean. I.e., this will always work:

{{ state_attr(entity_id, attribute) }}

Whereas only one of the following would work, depending on whether the attribute is a real boolean or just a string representation of one (and even then you’d have to worry about letter case – e.g., is it ‘true’ or ‘True’ or ‘TRUE’ …):

{{ is_state_attr(entity_id, attribute, true) }}
{{ is_state_attr(entity_id, attribute, 'true') }}

These two are not the same, so it matters which you’d use.

hi,
in fact it does not work,
now attached the code with the modification of space

  - alias: "batteria_scarica_alex on"
    trigger:
      - platform: time_pattern
        minutes: /5
    condition:
      condition: and
      conditions:
        - condition: template
          value_template: '{{ states.device_tracker.google_maps_code.attributes.battery_level | int < states.input_number.min_battery_alarm_phone.state | int }}'
        - condition: state 
          entity_id: device_tracker.google_maps_code
          state: 'home'
        - condition: template
          value_template: "{{ not state_attr(' device_tracker.google_maps_code ', 'battery_charging') }}"
    action:
      - service: switch.turn_on
        entity_id: switch.batteria_scarica_alex

  - alias: "batteria_scarica_alex off"
    trigger:
      - platform: time_pattern
        minutes: /5
    condition:
      condition: or
      conditions:
        - condition: template
          value_template: "{{ state_attr('device_tracker.google_maps_code', 'battery_charging') }}"
        - condition: template
          value_template: '{{ states.device_tracker.google_maps_code.attributes.battery_level | int > states.input_number.min_battery_alarm_phone.state | int }}'
        - condition: template
          value_template: "{{ not states('device_tracker.google_maps_code', 'home') }}"
    action:
      - service: switch.turn_off
        entity_id: switch.batteria_scarica_alex

Are you saying it still does not work the way you want?

If so, maybe we should take a step back so you can explain what you are ultimately trying to achieve. In general automations should not need to use a periodic time trigger. Normally you should use triggers corresponding to the events that should cause the action to run. Whenever I see an automation with a periodic trigger and a bunch of conditions that’s usually a “red flag.”

Hi,
I do not know if it works now with the latest changes, I have to rehearse.
I arrived at this configuration because of the thousand tests …
we would have to rewrite everything …

Um, ok. Not sure I follow.

In any case, you still have an entity_id improperly quoted:

          value_template: "{{ not state_attr(' device_tracker.google_maps_code ', 'battery_charging') }}"