Help with Boolean Sensor and Condition

Hello to everyone!!! i need some help.
Thanks to pnbruckner i have this automation that turn on the boolean sensor:

 - trigger:
    platform: state
    entity_id:
    - sensor.0x00158d0002b1136c_action
    - sensor.0x00158d0002af6ac2_action
  condition:
    condition: template
    value_template: >
      {{ trigger.to_state is not none
         and trigger.to_state.state in ('vibration', 'tilt', 'drop')
         and (trigger.from_state is none
              or trigger.to_state.state != trigger.from_state.state) }}
  action:
    service_template: >
      {% if trigger.to_state.state in ('vibration', 'tilt', 'drop') %}
        input_boolean.turn_on
      {% else %}
        input_boolean.turn_off
      {% endif %}
    data_template:
      entity_id: >
        {% if trigger.entity_id == 'sensor.0x00158d0002b1136c_action' %}
          input_boolean.finestra_studio
        {% else %}
          input_boolean.porta_blindata
        {% endif %}

No i want to do one automation that run when the boolean sensor turn On, wait 10 seconds then turn off the boolean sensor.
I don’t kno how can i do this… using this code not working…

 - trigger:
    platform: state
    entity_id:
    - input_boolean.porta_blindata
    - input_boolean.finestra_studio
  condition:
    condition: template
    value_template: >
      {{ input_boolen is on
       }}
  action:
    service_template: >
        input_boolean.turn_off
    data_template:
      entity_id: >
        {% if trigger.entity_id == 'input_boolean.finestra_studio' %}
          input_boolean.finestra_studio
        {% else %}
          input_boolean.porta_blindata
        {% endif %}

You’ve got some issues with spacing and other things. Try this:

  trigger:
    platform: state
    entity_id:
    - input_boolean.porta_blindata
    - input_boolean.finestra_studio
    to: 'on'
    for: '00:00:10'
  action:
    service: input_boolean.turn_off
    data_template:
      entity_id: >
        {% if trigger.entity_id == 'input_boolean.finestra_studio' %}
          input_boolean.finestra_studio
        {% else %}
          input_boolean.porta_blindata
        {% endif %}

I also don’t know the ultimate goal behind what you’re trying to do, so I’m just answering your question as is.

1 Like

Yeah, I’d think something like this would work:

- trigger:
    platform: state
    entity_id:
    - input_boolean.porta_blindata
    - input_boolean.finestra_studio
    to: 'on'
    for:
      seconds: 10
  action:
    service: input_boolean.turn_off
    data_template:
      entity_id: "{{ trigger.entity_id }}"
1 Like

Ah yeah that action section is better.

Thanks a lot for the help!!
Seems that works well!! :smiley:
i have just one log error in the other automation, can someone help me?
The Log:

2019-10-31 18:26:41 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event automation_triggered[L]: name=Sensori Vibrazione a Boolean, entity_id=automation.sensori_vibrazione_a_boolean>
2019-10-31 18:26:41 INFO (MainThread) [homeassistant.components.automation] Executing Sensori Vibrazione a Boolean
2019-10-31 18:26:41 INFO (MainThread) [homeassistant.helpers.script] Script Sensori Vibrazione a Boolean: Running script
2019-10-31 18:26:41 INFO (MainThread) [homeassistant.helpers.script] Script Sensori Vibrazione a Boolean: Executing step call service
2019-10-31 18:26:41 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=input_boolean, service=turn_on, service_data=entity_id=input_boolean.finestra_studio>
2019-10-31 18:26:41 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event automation_triggered[L]: name=Sensori Vibrazione a Boolean, entity_id=automation.sensori_vibrazione_a_boolean>
2019-10-31 18:26:41 INFO (MainThread) [homeassistant.components.automation] Executing Sensori Vibrazione a Boolean
2019-10-31 18:26:41 INFO (MainThread) [homeassistant.helpers.script] Script Sensori Vibrazione a Boolean: Executing step call service
2019-10-31 18:26:41 ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.sensori_vibrazione_a_boolean. Invalid data for call_service at pos 1: Service  does not match format <domain>.<name>
2019-10-31 18:26:41 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=automation.sensori_vibrazione_a_boolean, old_state=<state automation.sensori_vibrazione_a_boolean=on; last_triggered=2019-10-31T18:25:38.978557+01:00, id=1570087617286, friendly_name=Sensori Vibrazione a Boolean @ 2019-10-31T18:26:25.373345+01:00>, new_state=<state automation.sensori_vibrazione_a_boolean=on; last_triggered=2019-10-31T18:26:41.733601+01:00, id=1570087617286, friendly_name=Sensori Vibrazione a Boolean @ 2019-10-31T18:26:25.373345+01:00>>
2019-10-31 18:26:41 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=input_boolean.finestra_studio, old_state=<state input_boolean.finestra_studio=off @ 2019-10-31T18:25:56.845723+01:00>, new_state=<state input_boolean.finestra_studio=on @ 2019-10-31T18:26:41.765530+01:00>>

The Sensori Vibrazione a Boolean Automation:

- id: '1570087617286'
  alias: Sensori Vibrazione a Boolean
  trigger:
  - entity_id:
    - sensor.0x00158d0002b1136c_action
    - sensor.0x00158d0002af6ac2_action
    platform: state
  condition:
  - condition: template
    value_template: "{{ trigger.from_state is none\n   or trigger.to_state is none\n\
      \   or trigger.to_state.state != trigger.from_state.state }}\n"
  action:
    service_template: "{% if trigger.to_state is not none\n      and trigger.to_state.state\
      \ in ('vibration', 'tilt', 'drop') %}\n  input_boolean.turn_on\n{% else %} \
      \ {% endif %}\n"
    data_template:
      entity_id: "{% if trigger.entity_id == 'sensor.0x00158d0002b1136c_action' %}\n\
        \  input_boolean.finestra_studio\n{% else %}\n  input_boolean.porta_blindata\n\
        {% endif %}\n"

Thanks for all your help!! :smiley:

I believe you meant to say input_boolean not boolean sensor. The automation you have enables/disables an input_boolean.

In Home Assistant, sensors are read-only devices whereas an input_boolean can be turned on/off (like in your automation).

The error message is reporting that your automation’s action calls a service with a name that is incorrectly formatted:

Invalid data for call_service at pos 1: Service does not match format .

Look at the service_template. It contains an if-else. What does it produce for the else? The answer is: nothing. That means the service it will call is no service at all and that is invalid (and responsible for generating the error message). A service_template must produce a valid service name.

I’ve reformatted your automation so you can see the source of the problem clearly:

- id: '1570087617286'
  alias: Sensori Vibrazione a Boolean
  trigger:
  - entity_id:
    - sensor.0x00158d0002b1136c_action
    - sensor.0x00158d0002af6ac2_action
    platform: state
  condition:
  - condition: template
    value_template: >
      {{ trigger.from_state is none or trigger.to_state is none
         or trigger.to_state.state != trigger.from_state.state }}
  action:
    service_template: >
      {% if trigger.to_state is not none and trigger.to_state.state in ('vibration', 'tilt', 'drop') %}
         input_boolean.turn_on
      {% else %}
         # some other service name has to go here or the template needs to be restructured
      {% endif %}
    data_template:
      entity_id: >
        {% if trigger.entity_id == 'sensor.0x00158d0002b1136c_action' %}
           input_boolean.finestra_studio
        {% else %}
           input_boolean.porta_blindata
        {% endif %}
1 Like

Thanks a lot Taras, i don’t need anything in the else part so how can i do to tell the automation to do nothing?

The easiest way is just to have another condition in the conditions section so that the automation doesn’t run at all. If you have subsequent actions that need to be executed, you could define an empty script and run that in the else block.

There appears to be a contradiction in the automation.

The condition contains trigger.to_state is none

    value_template: "{{ trigger.from_state is none\n   or trigger.to_state is none\n\
      \   or trigger.to_state.state != trigger.from_state.state }}\n"

The action’s service_template contains trigger.to_state is not none

    service_template: "{% if trigger.to_state is not none\n      and trigger.to_state.state\
      \ in ('vibration', 'tilt', 'drop') %}\n  input_boolean.turn_on\n{% else %} \
      \ {% endif %}\n"

So the condition allows trigger.to_state to be none but the service_template does not. Why does the condition allow it?

I don’t know why, but I need that the automation will run when the sensors stay in Vibration,Tilt or drop state.
I only need this, if sensors stay in Vibration or tilt or drop run the automation, find which sensor has triggered the automation and turn the respective Boolean to ON.
How can I make this automation?
I think something like this:


- id: '1570087617286'
  alias: Sensori Vibrazione a Boolean
  trigger:
  - entity_id:
    - sensor.0x00158d0002b1136c_action
    - sensor.0x00158d0002af6ac2_action
    platform: state
  condition:
  - condition: template
    value_template: >
      {{ trigger.to_state.state in ('vibration', 'tilt', 'drop') }}
  action:
    service_template: >
      
         input_boolean.turn_on

    data_template:
      entity_id: >
        {% if trigger.entity_id == 'sensor.0x00158d0002b1136c_action' %}
           input_boolean.finestra_studio
        {% else %}
           input_boolean.porta_blindata
        {% endif %}

Thanks a lot for all your help!!

Edit: I’m clearly answering a question that has already been answered :slight_smile:

I was just suggesting to move the if statement you have in the service_template section into a condition:

- id: '1570087617286'
  alias: Sensori Vibrazione a Boolean
  trigger:
  - entity_id:
    - sensor.0x00158d0002b1136c_action
    - sensor.0x00158d0002af6ac2_action
    platform: state
  condition:
  - condition: template
    value_template: >
      {{ (trigger.from_state is none)
         or (trigger.to_state is none)
         or (trigger.to_state.state != trigger.from_state.state) }}
  - condition: template
    value_template: >
      {{ (trigger.to_state is not none)
         and trigger.to_state.state in ['vibration', 'tilt', 'drop'] }}
  action:
    service: input_boolean.turn_on
    data_template:
      entity_id: >
        {% if trigger.entity_id == 'sensor.0x00158d0002b1136c_action' %}
           input_boolean.finestra_studio
        {% else %}
           input_boolean.porta_blindata
        {% endif %}

Also, like Taras mentioned, you check for the from_state and to_state being none several times. Does this normally happen? This is never a check I’ve had to perform. And if trigger.to_state == trigger.from_state then I don’t think the automation would have been triggered at all.

pnbruckner had explained (can’t find the relevant post at the moment) that if you don’t specify a to: and/or from: for a State Trigger, then its open to the possibility of triggering if one of the entity’s attributes changes state.

So if the entity’s state hasn’t changed, just one of its attributes, then its to_state.state remains the same as its from_state.state. If they’re the same then you know the cause of the trigger was some attribute changing. That kind of state-change might not be of interest for automation’s purposes and can be filtered out in the condition.


EDIT
Found it and it’s worth reading through the entire thread. It’s not one of my shining moments. I challenged Phil on the need for something I disparaged as being a ‘belt and suspenders’ approach … and lost the debate but came out wiser (thanks to Phil’s patience).

After re-reading that thread, I realize the condition used in this automation should change to this:

  condition:
    condition: template
    value_template: >
      {{ trigger.from_state and trigger.to_state and
         trigger.from_state.state != trigger.to_state.state and
         trigger.to_state.state in ('vibration', 'tilt', 'drop') }}

The first two tests in the template are to ensure the from_state object and to_state object exist before getting to the third test that checks if both object have the same state. As per Phil’s explanation in the linked thread, trigger.from_state is shorthand for trigger.from_state is not none (i.e. it’s confirming the object exists).

If that condition evaluates to True then the action can use service (not service_template) to simply call input_boolean.turn_on (there will no longer be a need for an if-else).

Putting it all together, we get this:

- id: '1570087617286'
  alias: Sensori Vibrazione a Boolean
  trigger:
  - platform: state
    entity_id:
    - sensor.0x00158d0002b1136c_action
    - sensor.0x00158d0002af6ac2_action
  condition:
    condition: template
    value_template: >
      {{ trigger.from_state and trigger.to_state and
         trigger.from_state.state != trigger.to_state.state and
         trigger.to_state.state in ('vibration', 'tilt', 'drop') }}
  action:
    service: input_boolean.turn_on
    data_template:
      entity_id: >
        {% if trigger.entity_id == 'sensor.0x00158d0002b1136c_action' %}
           input_boolean.finestra_studio
        {% else %}
           input_boolean.porta_blindata
        {% endif %}
1 Like

First, thanks for jumping in and helping out. I helped @Wes93 quite a bit in a private message (with 55 posts), so much of the background is not visible to everyone. Unfortunately he is inconsistent in his use of terminology and often changes things without providing complete details, which makes it more difficult to help. Hence I was ready to pass the baton. :slight_smile:

FWIW, this was (I think) the last suggestion I had provided on turning the input_boolean on and off:

 - trigger:
    platform: state
    entity_id:
    - sensor.0x00158d0002b1136c_action
    - sensor.0x00158d0002af6ac2_action
  condition:
    condition: template
    value_template: >
      {{ trigger.from_state is none
         or trigger.to_state is none
         or trigger.to_state.state != trigger.from_state.state }}
  action:
    service_template: >
      {% if trigger.to_state is not none
            and trigger.to_state.state in ('vibration', 'tilt', 'drop') %}
        input_boolean.turn_on
      {% else %}
        input_boolean.turn_off
      {% endif %}
    data_template:
      entity_id: >
        {% if trigger.entity_id == 'sensor.0x00158d0002b1136c_action' %}
          input_boolean.finestra_studio
        {% else %}
          input_boolean.porta_blindata
        {% endif %}

The idea was if the trigger fired either because the sensor was just created (trigger.from_state is none), or the sensor was just deleted (trigger.to-state is none), or if the actual state string changed (trigger.to_state.state != trigger.from_state.state), then run the action, which would either turn the input_boolean on or off depending on the actual state change.

But because these sensors used in the triggers change so fast, it also causes the input_boolean to change too fast for how he was using it. So he’s trying to come up with a way to turn it on as before, but somehow cause it to stay on long enough so “down the road” automations, etc. work correctly. Hence (I assume) why he changed the original automation to attempt to not turn the input_boolean off, but try to do that another way.

1 Like

Thanks to everyone for the help!!
@pnbruckner sorry for my incorrect use of terminology and for changing things and thank for your patience…
I don’t know very well English… :frowning:
I have changed things because the automation that we have created won’t work so I have used a different approach and for this reason I don’t need that the first automation also turn off the sensor.
Anyway really thanks a lot to everyone for the help I’m a newbie and I need exercise.
@123 when I will come Back at home I will try your automation!!
For everyone sorry for the disturb… :frowning:

Thanks a lot!!!
@123 The automation works very well!!!
Really Thanks!!
Stefano

You’re welcome! Glad to hear it works well for you.

(Balance of my original post has been redacted … because it’s unnecessary)