Automation For Bed Sensors

I made a couple of bed sensors a few days ago, using MQTT. They are working fine. I am now trying to get a fan to come on if I get in bed, HA senses I’m in bed. The sensor reads In Bed (or) Not In Bed.

my automation looks like this

- alias: Turn on Dees Fan when in bed
  trigger:
    platform: state
    entity_id: sensor.dee_in_bed
    from: 'Not_In_Bed'
    to: 'In_Bed'
  action:
    service: homeassistant.turn_on
    entity_id: switch.bedroomfan

Thank you

If the sensor reads ‘Not in Bed’ then your trigger shouldn’t have underscores in the to and from. So:

  trigger:
    platform: state
    entity_id: sensor.dee_in_bed
    from: 'Not In Bed'
    to: 'In Bed'

Your action should be the following:

  action:
    service: switch.turn_on
    entity_id: switch.bedroomfan

@bradyn12 thanks for the quick replay I changed all that and it still didn’t work I even changed the words to just one word thinking maybe it don’t like spaces.

OK I got it working, I was looking at my sketch and seen that I was just sending a string of words to be published NOT an, on or off so I fixed that now it all works.

Now I need to know how I can set a delay like if I’m out of bed for more then a hour.

Thanks

OK I got everything working just thought I would show what I had to do thanks for the help @bradyn12

 - alias: Turn on Dees Fan when in bed
  trigger:
    platform: state
    entity_id: sensor.dee_in_bed
    from: '0'
    to: '1'
  action:
    service: switch.turn_on
    entity_id: switch.bedroomfan

- alias: Turn off Dees Fan when not in bed
  trigger:
    platform: state
    entity_id: sensor.dee_in_bed
    from: '1'
    to: '0'
  action:
    - delay: 01:00:00
    - service: switch.turn_off
      entity_id: switch.bedroomfan

@bradyn12 I’m having a problem with the fan, I was hoping with a delay of a hour, if I got out of bed and returned to bed before the hour was up the fan would stay on,

But after the hour I got out of bed the fan turns off even if I’m back in bed within a few minutes.
I think I maybe have to add a condition but have never used them, and not sure of the right way to write it.

Thanks Dee

Hey @Dee, I guess what you are looking for is this. So your second automation should look like this:

- alias: Turn off Dees Fan when not in bed
  trigger:
    platform: state
    entity_id: sensor.dee_in_bed
    from: '1'
    to: '0'
    for:
      hours: 1
  action:
    - service: switch.turn_off
      entity_id: switch.bedroomfan

So the delay is before the action

@Malte Thank you that worked, I put in minutes in to test it. all worked great Thanks

Right! That way the automation is not triggered if the state changes during that one hour and it is only triggered if the state changed to ‘0’ for that one hour. In your automation beforehand the automation was triggered anyways but it wouldn’t wait for a status change within that one hour delay because as I said the automation was already triggered.
Hope that made it a little clearer :slight_smile:

1 Like