Basic automation: presence detected by PIR > turn LED on

I’m still doing some experiments and test with my ESP8266 via REST API.
I added temperature and humidity sensors and a switch to toggle ON/OFF a simple led.
Yesterday I succesfully connected a PIR sensor and wrote code to detect presence or not and respective REST API which GET returns this JSON:

{"presence_detected" : true} 

Now I wanto to create a simple automation with PIR sensor and the led to better understand automations, triggers, actions, etc.
Something like “PIR detect presence > turn lights on” (in this case lights is the LED).
Can someone help me? I read documentation but I didn’t understand very well how it works.

Actually I have (partial configuration):

sensor:
  - platform: rest
    name: sensor_test_motion
    resource: !secret api_sensor_sensor_test_motion
    value_template: '{{ value_json.presence_detected }}'

switch:
  - platform: rest
    name: switch_test
    resource: !secret api_simple_switch
    body_on: '{"active": true}'
    is_on_template: '{{value_json.is_active}}'

Not sure if this will help you but this is what I used for the Bruh multisensor: It reads the state of the PIR sensor.

- alias: Turn on Bedroom Motion Light
  trigger:
    platform: state
    entity_id: sensor.bedroom_motion_sensor
    state: 'motion detected'
  action:
    service: homeassistant.turn_on
    entity_id: light.sn2_led

- alias: Turn off Bedroom Motion Light
  trigger:
    platform: state
    entity_id: sensor.bedroom_motion_sensor
    state: 'standby'
  action:
    service: homeassistant.turn_off
    entity_id: light.sn2_led

I think I have a little bit confusion about some configuration elements and terms.
For example:

  1. what a sensor state is exactly? Is its latest value retrivied by HASS? Or it could be overrided and set a custom value into HASS according which sensor value?
    Example:
    I have a temperature sensor: as far as I understand it’s state is the temperature in degrees.
    Can I set a custom state like this? (pseudo-code)
    state = if( temperature > 30 ){ "hot" } else {"cold"}

On your trigger, it was reported:

state: 'motion detected'

so as far as I understand you have a sensor which return that state?
In my case it should be “true” or “false”?

  1. in the ‘action’ section on:

    action:
    service: homeassistant.turn_on
    entity_id: light.sn2_led

The ‘light.sn2_led’ is a switch?

  1. STATE is the current state of any object. It could be a sensor or a switch, or really anything.
    You CAN set the state of objects, but if an object has a default method for getting its state, then that would over-ride anything you write.

FOR EXAMPLE
If you have a temperature sensor that gets its value from a physical sensor, you COULD over-write that value, but the next update from the physical sensor would over-write your value (and those updates come unbelievably fast).
If you really want a sensor that has values of “hot”, “cold” based on the temperature of your physical temperature sensor, then create a new sensor (they don’t cost anything!) Make your new sensor a template sensor and set it’s value to “hot” or “cold” based upon the state of the temperature sensor.

{%if states.sensor.garage_temperature.state | int > 30 %}
hot
{%else%}
cold
{%endif%}
  1. light.sn2_led is in the light domain.
    light objects function somewhat differently than switch objects, but both domains have a turn_on service.

So, in my case, the PIR sensor state is retrieved with GET API, which return JSON:
{"presence_detected" : true}
so the state should be true or false right? (how to code it into yaml? true or "true"?)

So my automation will be something similar to:

automation:
  - alias: 'Turn LED on on motion'
    trigger:
      - platform: state
        entity_id: sensor.sensor_test_motion
        state: true
    action:
      service: homeassistant.turn_on
      switch.switch_test

Here is one of mine:

- alias: Turn Off Office Lights
  trigger:
    platform: state
    entity_id: binary_sensor.homer_in_use
    state: 'false'
  action:
    - service: homeassistant.turn_off
      entity_id: switch.office1
    - service: homeassistant.turn_off
      entity_id: switch.office2

With code poste by @treno automation seems works fine.

BTW I cannot understand this two issues:

  1. i must escape true/false values as string
  2. the values detected by the PIR sensor JSON are ‘True’ and ‘False’ and not ‘true’ and ‘false’

The JSON contains a boolean, and not a string, in fact it is
{"presence_detected" : true}
not
{"presence_detected" : "true"}
Should I return a string as state instead of bool?

To get a good understanding of how this works, go to the template tool under developer tools and type something like this
{{states.binary_sensor.garage_motion_detected.state == 'false'}}

You can play with your values and see the results. `

Hello Corey!

How does your sensor and mqtt declaration look? I’m also using the bruh multisensor.
I followed his example right off. Works fine but I’m also having problems using them with automations.

I did a straight copy as well - sensors.yaml:

- platform: mqtt
  state_topic: "sensornode1"
  name: "SN1 Humidity"
  unit_of_measurement: "%"
  value_template: '{{ value_json.humidity | round(1) }}'

- platform: mqtt
  state_topic: "sensornode1"
  name: "SN1 LDR"
  ##This sensor is not calibrated to actual LUX. Rather, this a map of the input voltage ranging from 0 - 1023.
  unit_of_measurement: "LUX"
  value_template: '{{ value_json.ldr }}'

- platform: mqtt
  state_topic: "sensornode1"
  name: "SN1 PIR"
  value_template: '{{ value_json.motion }}'
  
- platform: mqtt
  state_topic: "sensornode1"
  name: "SN1 Temperature"
  unit_of_measurement: "°F"
  value_template: '{{ value_json.temperature | round(1) }}'

in configuration.yaml:

  sensor_node_cards:  
    name: Sensor Nodes  
    entities:
      - sensor.sn1_temperature
      - sensor.sn1_humidity
      - sensor.sn1_ldr
      - sensor.sn1_pir
      - light.sn1_led

to reference them in an automation:

automation.yaml:

################################
### Bedroom Motion Light
################################

- alias: Turn on Bedroom Motion Light
  trigger:
    platform: state
    entity_id: sensor.sn1_ldr
    state: 'motion detected'
  action:
    service: homeassistant.turn_on
    entity_id: light.sn1_led

- alias: Turn off Bedroom Motion Light
  trigger:
    platform: state
    entity_id: sensor.sn1_ldr
    state: 'standby'
  action:
    service: homeassistant.turn_off
    entity_id: light.sn1_led

I made a package this is gonna be a code wall sorry

homeassistant:
  customize:
    packages.system: &customize
      haaska_hidden: true
      homebridge_hidden: true
      package: 'master'

light:
  - platform: mqtt_json
    name: "mb led"
    state_topic: "torn/master_bed"
    command_topic: "torn/master_bed/set"
    brightness: true
    flash: true
    rgb: true
    optimistic: false
    qos: 0

  - platform: mqtt_json
    name: "gr led"
    state_topic: "torn/game_room"
    command_topic: "torn/game_room/set"
    brightness: true
    flash: true
    rgb: true
    optimistic: false
    qos: 0

group:
  master_room_motion:
    entities:
      - sensor.master_bed_motion
      - sensor.mb_motion_status
      - light.mb_led
      - automation.turn_on_master_bedroom_light_when_there_is_movement
      - automation.turn_off_master_bedroom_light_when_there_is_no_movement

sensor:
  - platform: mqtt
    state_topic: "torn/master_bed"
    name: "master bed motion"
    value_template: '{{ value_json.motion }}'

  - platform: mqtt
    state_topic: "torn/master_bed"
    name: "master bed temperature"
    unit_of_measurement: "°F"
    value_template: '{{ value_json.temperature | round(1) }}'

  - platform: mqtt
    state_topic: "torn/master_bed"
    name: "master bed humidity"
    unit_of_measurement: "%"
    value_template: '{{ value_json.humidity | round(1) }}'

  - platform: template
    sensors:
      mb_motion_status:
        value_template: '{% if states.sensor.master_bed_motion %}
          {% if states.sensor.master_bed_motion.state == "motion detected" %}
            on
          {% else %}
            off
          {% endif %}
          {% else %}
          n/a
          {% endif %}'
automation:
  - alias: Turn on led
    trigger:
      platform: state
      entity_id: sensor.mb_motion_status
      to: 'on'
    condition:
      - condition: template
        value_template: >-
          {%- set hour=(as_timestamp(now())|timestamp_custom("%H"))|int -%}
          {%- if hour >= 10 and hour  <=  22 -%}
            true
          {%- else -%}
            false
          {%- endif %}
    action:
      service: light.turn_on
      entity_id: light.mb_led

  - alias: Turn off led
    trigger:
      platform: state
      entity_id: sensor.mb_motion_status
      to: 'off'
    condition:
      - condition: template
        value_template: >-
          {%- set hour=(as_timestamp(now())|timestamp_custom("%H"))|int -%}
          {%- if hour >= 10 and hour <= 22 -%}
            true
          {%- else -%}
            false
          {%- endif %}
    action:
      service: light.turn_off
      entity_id: light.mb_led

It has 2 led setups in it as I havent moved it to another package
this is what it looks like

The packages I create make views so I can manually test the automations.