Automation weirdness

I’m just testing an automation and getting a weird result. This is from my automation.yaml file:

- id: '1547063496825'
  alias: Light Test
  trigger:
  - entity_id: binary_sensor.lounge
    from: Clear
    platform: state
    to: Detected
  condition: []
  action:
    service: light.toggle
  data:
    entity_id:
      - light.big_lamp

When I trigger this, several different lights toggle their state. If I trigger it a second time, some of the lights that got toggled with the first trigger then toggle back but not all of them. It seems to be the Ikea ones that don’t toggle back. I’m having strange things happen with the Ikea lights anyway but that’s not what I’m asking about here.

But why, when I’ve specified one light, do lots of different lights - it might be all lights - toggle their state.

Also, nothing happens when I trigger this sensor. I have to click on the hamburger menu on the Overview page and trigger it from there

I think your data line needs to be indented

… and indent everything below the data line. Or simply get rid of the data line (and leave the rest the same), which isn’t required here.

I think as it is at present, it will simply run light.toggle, which will apply to the whole light domain. Which seems to explain what you are seeing. BTW this has changed in 0.85.

To be clear, you need to change the action part to:

  action:
    service: light.toggle
    data:
      entity_id:
        - light.big_lamp

or:

  action:
    service: light.toggle
    entity_id:
      - light.big_lamp

or even better:

  action:
    service: light.toggle
    entity_id: light.big_lamp

As you had it, data was part of the automation (like alias, trigger, condition & action), not the service.

Thanks guys. So, this is what I have right now:

- id: '1547110913347'
  alias: Loo light
  trigger:
  - entity_id: binary_sensor.loo_door
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
    service: light.turn_on
    entity_id: light.loo_light
- id: '1547191832307'
  alias: Loo Light Off After 10 minutes
  trigger:
  - entity_id: light.loo_light
    for: '10'
    platform: state
    to: 'on'
  condition: []
  action:
    service: light.turn_off
    entity_id: light.loo_light

The light seems to come on when I trigger it manually, but not when the loo door gets opened. I’m not sure about the from/to with this though. It’s a sensor on the door rather than a movement sensor. On the dashboard, it shows as “Clear” and “Detected” and I tried that as well.

binary_sensor’s are always 'on' or 'off', even if they are displayed differently in the frontend. Check the state on the States page. Besides, if the light is not coming on when the door opens, then it should come on when the door closes, assuming, of course, that binary_sensor.loo_door is actually changing in response to the door opening and closing.

BTW, the for: parameter accepts time in several different formats. But the one you’ve used is seconds. Do you really want it to turn off after only 10 seconds? If you wanted 10 minutes, you could use a value of 600, or 00:10:00, or:

for:
  minutes: 10
1 Like

Thank you for that. What I did initiially was use the creator and then tinkered with it by hand based on some posts I saw and examples in the documentation. I think the best thing would be for me to learn the syntax and not use the form at all. The form to create automations and scripts isn’t intuitive to me at all.

1 Like

Ok, just in case, I set up a new automation with a motion sensor and different lamp:

- id: 'big_lamp'
  alias: Big lamp
  trigger:
  - entity_id: binary_sensor.lounge
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
    service: light.turn_on
    entity_id: light.big_lamp
- id: 'big_lamp_off'
  alias: Big Lamp Off After 10 seconds
  trigger:
  - entity_id: light.big_lamp
    for: '10'
    platform: state
    to: 'on'
  condition: []
  action:
    service: light.turn_off
    entity_id: light.big_lamp

I can wave at the sensor, see it flash green to tell me it’s seen me until my arms fall off. Sometimes it’ll show up in the log, sometimes not. If I trigger the automation manually, the above works. Light comes on…10 seconds… light goes off.

If there was some sort of lag with my Hive sensor talking to HA, or a lag with HA listening, could that explain it? Bearing in mind, HA is talking to the Hive sensor through an API.

If I could just get this working,

The issue must be between the physical sensor and HA’s binary_sensor that represents it. Given what you’ve said, if the binary_sensor (entity in HA) changes to 'on' (and it was 'off' right before that), then the automation should trigger (assuming the automation itself is on.) If I were you’d I’d go to the States page and find the binary_sensor of interest. Then I would try to make it change (by operating the door, or waving your hand in front of the sensor) and see if/when the binary_sensor changes. As soon as the HA binary_sensor entity changes it will show immediately on that page.

Maybe it’s possible that the binary_sensor is going to some other state between 'off' and 'on'. Not likely, but it is possible, e.g., to go to 'unknown' temporarily. You might try removing the from: 'off' part of the trigger.

So, I gave up. Went and made a coffee, walked back into the lounge and the big lamp came on for 10 seconds then went off.

Will it do it again? Nope.

I think it’s the Hive sensors doing something weird. If you wave your hand in front of it, the light flashes. But it won’t do it again for 30 seconds or so. Like you say, it’s like it goes into some sort of unknown state. I tried removing the from: 'off' but no difference.

Not sure what you mean by the states page. Do you mean [myipaddress]:8123/states? The only place I see the sensors is in the little badges. If I click on one, a modal pops up with the timeline but that doesn’t like like it’s real time.

I reckon it’s this dodgy Hive gear. I’m slowly getting rid of it. I’m going to look round for some different sensors and try something else.

Thank you for all your help on this!

I mean this:

image

Then you’ll see something like this:

image

The (second) State column shows the current state of the entity, and will update dynamically as the entity updates. If you click the box with the arrow on the left, you’ll get a more info dialog, which is I think what you were referring to. Besides the timeline, it will also show the current state (although the words used are probably different from on and off.)

image

If you click the title area the box will expand horizontally. You should be able to see the last 24 hours worth of activity.

Ok, another step forward, I think, but it’s looking more and more likely that these sensors are just pants.

If I look at the states page, I can see that the sensor is on. I can then set the state to off using the form and when I wave my hand in front of the sensor, the light comes on for 10s. But the sensor stays in the on state for ages even though it flashes when I wave my hand again.

It took about 5 minutes to return to the off state. Then when I waved my hand, the light didn’t come on.

Click on set state - even though the state is set to off, nothing happens.

Delete the word off from the state field on the form, retype it, click set state and wave my hand - light comes on for 10s.

Set the state on the form to blah, wave my hand - light comes on (bearing in mind I have to “from” in my automation now.

New sensors?

Not sure how to interpret all that, but it sounds like the state is not updating correctly, or at least, as you’d like.

One thing to keep in mind. The States page is showing the states as registered in the “State Machine.” If you “manually” update the state using the form at the top, you’re only changing the state as registered in the State Machine. You’re not actually changing the state of the entity itself. That’s by design. Think of the State Machine as a go-between between the entity and the rest of HA. So the state of an entity in the State Machine can be updated either by the entity itself, or by something else (such as you using the form at the top of the States page.) Automation triggers react to state changes in the State Machine (not the entity directly.) Most entities periodically update their state in the State Machine, although others do that only as necessary. So, if you manually change a state in the State Machine, it depends on the implementation of the entity if that will get overwritten by the entity on a periodic basis, or when it sees a change of the physical thing it represents.

Not sure if that explanation helps. Bottom line - the state you see on the States page is what is driving the automation. If that’s not changing/updating per the physical device, then that’s your problem. Since I’m not familiar with the actual devices you’re using, I really can’t suggest how to fix it if that’s indeed the problem.

Yes, that all makes sense. The system I built myself before I found HA did something similar. So it wrote its current state to a MySQL database on the Pi and my dashboard read from the MySQL, or wrote to it if I was switching something on or off. So the front end never spoke to the devices directly. The Python script just synchronised it all when needed.

When I look at the logs on my Hive home dashboard, it looks like it sees the first trigger as a state change. After that, if you keep moving, it shows the sensor as still in the triggered state with multiple events.

So you end up with something like this:

I wasn’t moving around for 9 minutes, that was probably 2 triggers.

Just playing some more and there’s also a long lag sometimes, even if it does work. I thought I’d broken it when I waved and nothing happened. Then about 20 seconds later the light came on.

Thank you so much for your input on this! I’ve learned a huge amount already from this thread. I think I’m going to move on to getting HADashboard playing nicely :slight_smile:

1 Like

Hi @andyjacobs

It sounds like you have already come to some conclusion regarding this, but I thought I would just add some information on how the Hive component works.

There is no push state from Hive to Home Assistant, everything is cloud polling and pulling current device state information every few minutes from the Hive api.
If not defined, the default poll period is every 2 minutes, which will then every 2 minutes will allow the Hive Home Assistant component to connect to the Hive API and pull the latest state for all your Hive devices (heating, hotwater, lights, sensors), this connection to the Hive API is then triggered by an entity in Home Assistant requesting an update, so probably a sensor as these update every 30 seconds.

For Hive sensors, this is less than ideal, as I guess you could potentially trigger the sensor just after the last HA poll, then the sensor resets back to not triggered all within 2 minutes, which when Home Assistant then does the next pull of data from Hive the sensor still shows as not triggered.
But it could also mean that if you trigger a sensor just after an HA data pull, then keep the sensor triggered for 2 minutes, it would eventually show as triggered in HA 2 minutes later when HA pulls the latest data.

If you haven’t already tried this, you could add the parameter to configuration.yaml to reduce the period between polls to 1 minute and reduce the lag between sensor trigger and HA state update:

 hive:
   username: YOUR_USERNAME
   password: YOUR_PASSWORD
   scan_interval: 1

Thank you for all that. That’s added a lot to my knowledge and ties in with my experience last night when I had time to look at it again. I’d wave at the sensor and nothing would happen. I then went out of the room for a few minutes and the light was on when I came back in.

I’ll give that scan_interval a try but I’m starting to think that mixing Hive sensors with other products isn’t totally practical. I’m still tuning the HA stuff for other things and will persevere but I’ve already started researching other ways of doing motion detection.

I would agree, trying to use Hive sensors in HA automations wont be as good as if using other sensors as you will always have the lag and potential for incorrect readings depending on the timings.
Personally use Aeotec Multisensors and find them to be very quick and accurate, although pretty expensive.
Other Hive products work will as HA will push any changes to Hive instantly, such as turning on a switch or light, or setting temperature on heating, but for status of all devices it relies on a regular pull from HA to Hive.

So those Aeotec sensors. I quite like the look of those. Yes, they’re a bit more than the hive but I’m now aiming for stability, reliability and flexibility so I’d be happy spending that sort of money on something if it’s going to do the job.

They’re Z-wave. Does that mean I’d need some sort of Z-wave hub? I couldn’t find a HA component for Aeotec. Is it fairly easy to get them integrated?

yes, you would need a z-wave hub to connect all your z-wave devices. I use the Aeotec Z-Stick Gen5 which plugs in to a free USB on the Home Assistant server.
Pretty easy to setup with the z-wave component and then pair the new devices with the stick and they then appear in the HA consol