Automation works if triggered manually, but not by toggling it

Hello there,
I have managed to setup a PIR HC-SR 501 motion sensor. Its status gets published on mqtt and it’s read and shown on the main page, but after I added the automation below, it just won’t turn on the light. Here are my config. and automation yaml parts:

Configuration.yaml piece:
sensor 1:

  • platform: mqtt
    state_topic: “home-assistant/fundos/motion”
    name: Sensor de Presença (Fundos)

Automation piece:

  • alias: Ligar luz do quarto
    platform: state
    entity_id: sensor.sensor_de_presenca_fundos
    to: ‘Movimento Detectado’
    • service: homeassistant.turn_on
      entity_id: switch.sonoff_power

If I got to the logbook, I can see that its logs there and if I click on Trigger, it does turn the light on, but not automatically, as there is movement in the room.

Any info will be appreciated!


What is the state shown on the main page when movement is detected?

And it helps immensely if you use the code brackets </> when posting your code so people can check the syntax to make sure its correct.

Try adding initial_state: 'on' to your automations.

1 Like

hi @finity,

It shows Movimento Detectado in the icon, but nothing happens. If I click the automation and click on TRIGGER, it turns on the light, though.
It’s probably not reading the state precisely… But I cant go any futher than that now…

PS: Sorry for the lack of code brackets. Still getting the hang of it around here!


Tried it. Didn’t work. Thanks anyway!


It may not be the only reason why your code isn’t working, it’s hard to diagnose if you don’t use the code formatting. It’s the </> button in the toolbar. But you should add the initial_state anyways because without it, your automation may not be enabled.

It won’t hurt to add that but I don’t think its necessary. I’m pretty sure the default is on unless defined otherwise.

But the next question is what does it say in the states page under that entity I’d when movement is detected?

And you can edit your post to put the code into brackets for proper formatting.

@finity I had problems every time I restarted HA without it. It’s a good practice to include it. Especially when troubleshooting.