Combining use_trigger_values with other conditions: possible?

So, I am trying to set up an automation, but I can’t get it to work.

The automation code:

- alias: "Run robot vacuum cleaner while sleeping"
  trigger:
    - platform: state
      entity_id: switch.run_robot_vacuum_cleaner_while_sleeping
      state: 'on'
    - platform: state
      entity_id: media_player.shiva
      state: 'off'
    - platform: state
      entity_id: group.living_area
      state: 'off'
    - platform: time
      after: '00:00:00'
    - platform: numeric_state
      entity_id: sensor.hombot_battery
      above: 100
      value_template: "{{ value | int }}"
  condition:
    condition: and
    conditions:
      - condition: use_trigger_values
      - condition: state
        entity_id: sensor.hombot_status
        state: 'CHARGING'
      - condition: template
        value_template: "{{ states.sensor.hombot_last_clean.state.split('/')[0] ~ '-' ~ states.sensor.hombot_last_clean.state.split('/')[1] ~ '-' ~ states.sensor.hombot_last_clean.state.split('/')[2] != (now | string).split(' ')[0] }}"
      - condition: time
        before: '05:00:00'
  action:
    - service: homeassistant.turn_on
      entity_id: script.hombot_clean
    - service: notify.pushover
      data:
        title: "HASS automation"
        message: "Robot vacuum cleaner started running!"

The error I’m currently getting:

16-07-26 13:15:31 homeassistant.bootstrap: Error during setup of component automation
Traceback (most recent call last):
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/bootstrap.py", line 150, in _setup_component
    if not component.setup(hass, config):
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/automation/__init__.py", line 105, in setup
    success = (_setup_automation(hass, config_block, name, config) or
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/automation/__init__.py", line 116, in _setup_automation
    action = _process_if(hass, config, config_block, action)
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/automation/__init__.py", line 173, in _process_if
    checks.append(condition.from_config(if_config))
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/helpers/condition.py", line 34, in from_config
    return factory(config, config_validation)
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/helpers/condition.py", line 40, in and_from_config
    config = cv.AND_CONDITION_SCHEMA(config)
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/voluptuous.py", line 355, in __call__
    return self._compiled([], data)
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/voluptuous.py", line 655, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/voluptuous.py", line 489, in validate_mapping
    raise MultipleInvalid(errors)
voluptuous.MultipleInvalid: not a valid value for dictionary value @ data['conditions'][0]['condition']

I wasn’t originally getting this error, when I was using to: instead of state: for my triggers, but then my automation wasn’t firing. Obviously, it won’t fire now either.

I’m not sure if condition: use_trigger_values can be combined with other conditions using condition: and but I sure hope it can. I’ve looked everywhere I could but haven’t found anybody trying this.

I hope I included everything. This is my first topic on this forum, so please let me know if I’ve forgotten something.

Most of those triggers look more like conditions to me.

Do you really want it to start cleaning potentially every time you turn your media off, or the second you turn on the night cleaning switch?

I think a better approach would be to have the trigger be some time, e.g. 2am, and make the rest conditions - that way you only have 1 trigger.

I am not even sure multiple triggers are possible - if you really want that you will probably need to set up multiple automations with different triggers and identical conditions and actions.

I could maybe get rid of 1 or 2 triggers if really necessary, but I would like to keep most as we have irregular bedtimes. It is definitely possible to have more than 1 trigger, though. Example:

I think that you should go 1 way or the other but not both.

right now you use time as a trigger, with use_trigger_values AND as a condition.

i would try to simplefy things. starting with time in 1 place (as trigger or as condition)

Alright, based on the feedback above I decided to rewrite the automation. It now looks like this:

- alias: "Run robot vacuum cleaner while sleeping"
  trigger:
    - platform: time
      after: '00:00:00'
      before: '05:00:00'
  condition:
    condition: and
    conditions:
      - platform: state
        entity_id: switch.run_robot_vacuum_cleaner_while_sleeping
        state: 'on'
      - platform: state
        entity_id: media_player.shiva
        state: 'off'
      - platform: state
        entity_id: group.living_area
        state: 'off'
      - condition: state
        entity_id: sensor.hombot_status
        state: 'CHARGING'
      - platform: numeric_state
        entity_id: sensor.hombot_battery
        above: 100
        value_template: "{{ value | int }}"
      - condition: template
        value_template: "{{ states.sensor.hombot_last_clean.state.split('/')[0] ~ '-' ~ states.sensor.hombot_last_clean.state.split('/')[1] ~ '-' ~ states.sensor.hombot_last_clean.state.split('/')[2] != (now | string).split(' ')[0] }}"
  action:
    - service: homeassistant.turn_on
      entity_id: script.hombot_clean
    - service: notify.pushover
      data:
        title: "HASS automation"
        message: "Robot vacuum cleaner started running!"

The proof will be in the pudding tonight! Eventhough the original automation code made sense in my head, it was probably a little too much to ask HASS to process. And I have to admit: this version looks much cleaner. Thanks, guys!

If I ever see a proper use case for condition: use_trigger_values combined with condition: and, I’ll consider submitting a feature request.

Btw, anybody happen to know how often the time trigger is checked in this case?

EDIT: new error with the changed code: 16-07-26 16:19:46 homeassistant.bootstrap: Invalid config for [automation]: [before] is an invalid option for [automation]. Check: automation->trigger->0->before. (See /home/hass/.homeassistant/automations.yaml:42). Looks like I’ll have to use an interval instead…

A trigger is really meant to be a discrete point in time kind of thing - anything else is a condition.

A trigger is “at 2am” a condition is “between 2am and 6am”.

HA has 1 second resolution so anything time based will be evaluated every second.

i think i would make 3 automations from it.

  1. when mediaplayer turns off, with all the conditions and time between 0 and 5
  2. when livingroom turns off, with all the conditions and time between 0 and 5
  3. when time = 0:00, with all the conditions.

that way it starts at 0:00, when you turn off mediaplayer or the livingroom off, when all conditions are met,

then if you change the time to a few minutes after now, you can test immidiatly, without waiting for the night.

Okay, my last version above was messy…just noticed some copy/paste mistakes, on top of the logical error returned by HASS.

This is my final version, which HASS hasn’t returned any errors for:

- alias: "Run robot vacuum cleaner while sleeping"
  trigger:
    - platform: time
      minutes: '/10'
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: switch.run_robot_vacuum_cleaner_while_sleeping
        state: 'on'
      - condition: time
        before: '05:00:00'
      - condition: state
        entity_id: media_player.shiva
        state: 'off'
      - condition: state
        entity_id: group.living_area
        state: 'off'
      - condition: state
        entity_id: sensor.hombot_status
        state: 'CHARGING'
      - condition: numeric_state
        entity_id: sensor.hombot_battery
        above: 100
        value_template: "{{ value | int }}"
      - condition: template
        value_template: "{{ states.sensor.hombot_last_clean.state.split('/')[0] ~ '-' ~ states.sensor.hombot_last_clean.state.split('/')[1] ~ '-' ~ states.sensor.hombot_last_clean.state.split('/')[2] != (now | string).split(' ')[0] }}"
  action:
    - service: homeassistant.turn_on
      entity_id: script.hombot_clean
    - service: notify.pushover
      data:
        title: "HASS automation"
        message: "Robot vacuum cleaner started running!"

Hopefully, this automation will work this evening. Not sure if I’m completely happy with the logic yet though, I may end up changing it again. But if nothing else, at least it seems I have an automation to fall back to.

1 Like

That looks good to me - the only issue I see is that presumably your battery can’t be above 100% so that condition would always fail? Try 99 instead?

I would also add an after time so the pesky thing doesn’t go running around during the day when your lights are off and it is fully charged and you turn the media off to read a book …

I did originally put in 99 and I may go back to that, just to avoid any trouble. I only put in 100 because the documentation here states that above means >=.

I’ll also add after to my time condition, as you suggested. My template condition (it compares the last run date in a text file on the Hom-Bot to the current date) technically makes it impossible for the Hom-Bot to run before midnight (when the dates will for sure be different), UNLESS it hasn’t run the last time for whatever reason. In that case, the dates will be different before midnight already.

Thanks for your input! :relaxed:

Excellent - seems like you are all set - let us know if it works!

Changes made, no errors…so it definitely seems that way, yes!

I’ll make sure to update this topic with my findings :wink:

seems a nice solution.

i dont think it will ever start before 0:00, because daytime and eveningtime is always after 5:00
i think that if no starttime is mentioned that the starttime is automaticly 0:00 (or it wouldnt make no sence that after is optional :wink: )

Yes, I was also thinking that the time condition is based on midnight. So stating before: '05:00:00' should keep it from running between 5 AM and midnight. But I added after: '00:00:00' anyway, just as an extra security and for improved readability.

1 Like

As promised, here is the final working version of my automation:

- alias: "Run Hombot while sleeping"
  trigger:
    - platform: time
      minutes: '/10'
      seconds: 0
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: switch.run_robot_vacuum_cleaner_while_sleeping
        state: 'on'
      - condition: time
        after: '00:00:00'
        before: '07:00:00'
      - condition: state
        entity_id: media_player.shiva
        state: 'off'
      - condition: state
        entity_id: group.living_area
        state: 'off'
      - condition: state
        entity_id: sensor.hombot_status
        state: 'CHARGING'
      - condition: numeric_state
        entity_id: sensor.hombot_battery
        above: 99
      - condition: template
        value_template: "{{ states.sensor.hombot_last_clean.state.split('/')[0] ~ '-' ~ states.sensor.hombot_last_clean.state.split('/')[1] ~ '-' ~ states.sensor.hombot_last_clean.state.split('/')[2] != (now | string).split(' ')[0] }}"
  action:
    - service: homeassistant.turn_on
      entity_id: script.hombot_clean
    - service: notify.pushover
      data:
        title: "HASS automation"
        message: "Hombot started cleaning!"

I had to make 2 changes to the previous version in order to get it to work correctly:

  • I had to add seconds: 0 under the trigger to keep the automation from triggering up to 60 times every 10 minutes.
  • I had to remove value_template: "{{ value | int }}" from the numeric_state condition as this was keeping my automation from working. Apparently, I was trying to fix something that didn’t need fixing.

I do have an Aeotec Z-Stick and a Door / Window Sensor 6 coming in in a couple of days (yay, finally!). That sensor is likely to replace the time trigger above.

Thanks for all the input, guys.

P.S.: working around the use of use_trigger_values was a good thing in the end, as @balloob stated it’s likely to be removed in the future. The reason for that is because the syntax does not map 1 on 1 which means they have to support two implementations of conditions.

1 Like

@fanaticDavid could you also post your script.hombot_clean as well as they type of cleaner you are using?

script.hombot_clean simply looks like this:

script:
  hombot_clean:
    sequence:
      - service: shell_command.hombot_clean

shell_command.hombot_clean then looks like this:

shell_command:
  hombot_clean: curl http://xxx.xxx.xxx.xx:6260/json.cgi?%7b%22COMMAND%22:%22CLEAN_START%22%7d

I’m using an LG Hom-Bot VR64702LVMT.